template

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2024 License: MIT Imports: 11 Imported by: 2

Documentation

Overview

Package template provides templating functionality.

Usage

import (
    "fmt"
    gotemplate "text/template"

    "github.com/terraform-docs/terraform-docs/print"
    "github.com/terraform-docs/terraform-docs/template"
    "github.com/terraform-docs/terraform-docs/terraform"
)

const mainTpl =`
{{- if .Config.Sections.Header -}}
    {{- with .Module.Header -}}
        {{ colorize "\033[90m" . }}
    {{ end -}}
    {{- printf "\n\n" -}}
{{ end -}}`

func render(config *print.Config, module *terraform.Module) (string, error) {
    tt := template.New(config, &template.Item{
        Name:      "main",
        Text:      mainTpl,
        TrimSpace: true,
    })

    tt := template.New(config, items...)
    tt.CustomFunc(gotemplate.FuncMap{
        "colorize": func(color string, s string) string {
            reset := "\033[0m"
            if !config.Settings.Color {
                color = ""
                reset = ""
            }
            return fmt.Sprintf("%s%s%s", color, s, reset)
        },
    })

    return tt.Render("main", module)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertMultiLineText

func ConvertMultiLineText(s string, isTable bool, isHeader bool, showHTML bool) string

ConvertMultiLineText converts a multi-line text into a suitable Markdown representation.

func ConvertOneLineCodeBlock

func ConvertOneLineCodeBlock(s string) string

ConvertOneLineCodeBlock converts a multi-line code block into a one-liner. Line breaks are replaced with single space.

func CreateAnchorAsciidoc

func CreateAnchorAsciidoc(prefix string, value string, anchor bool, escape bool) string

CreateAnchorAsciidoc creates HTML anchor for AsciiDoc format.

func CreateAnchorMarkdown

func CreateAnchorMarkdown(prefix string, value string, anchor bool, escape bool) string

CreateAnchorMarkdown creates HTML anchor for Markdown format.

func EscapeCharacters

func EscapeCharacters(s string, escape bool, escapePipe bool) string

EscapeCharacters escapes characters which have special meaning in Markdown into their corresponding literal.

func GenerateIndentation

func GenerateIndentation(base int, extra int, char string) string

GenerateIndentation generates indentation of Markdown and AsciiDoc headers with base level of provided 'settings.IndentLevel' plus any extra level needed for subsection (e.g. 'Required Inputs' which is a subsection of 'Inputs' section)

func NormalizeURLs

func NormalizeURLs(s string, escape bool) string

NormalizeURLs runs after escape function and normalizes URL back to the original state. For example any underscore in the URL which got escaped by 'EscapeCharacters' will be reverted back.

func SanitizeAsciidocTable

func SanitizeAsciidocTable(s string, escape bool, html bool) string

SanitizeAsciidocTable converts passed 'string' to suitable AsciiDoc representation for a table. (including line-break, illegal characters, code blocks etc).

func SanitizeDocument

func SanitizeDocument(s string, escape bool, html bool) string

SanitizeDocument converts passed 'string' to suitable Markdown or AsciiDoc representation for a document. (including line-break, illegal characters, code blocks etc).

func SanitizeMarkdownTable

func SanitizeMarkdownTable(s string, escape bool, html bool) string

SanitizeMarkdownTable converts passed 'string' to suitable Markdown representation for a table. (including line-break, illegal characters, code blocks etc).

func SanitizeName

func SanitizeName(name string, escape bool) string

SanitizeName escapes underscore character which have special meaning in Markdown.

func SanitizeSection

func SanitizeSection(s string, escape bool, html bool) string

SanitizeSection converts passed 'string' to suitable Markdown or AsciiDoc representation for a document. (including line-break, illegal characters, code blocks etc). This is in particular being used for header and footer.

IMPORTANT: SanitizeSection will never change the line-endings and preserve them as they are provided by the users.

Types

type Item

type Item struct {
	Name      string
	Text      string
	TrimSpace bool
}

Item represents a named templated which can reference other named templated too.

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template represents a new Template with given name and content to be rendered with provided settings with use of built-in and custom functions.

func New

func New(config *print.Config, items ...*Item) *Template

New returns new instance of Template.

func (*Template) CustomFunc

func (t *Template) CustomFunc(funcs gotemplate.FuncMap)

CustomFunc adds new custom functions to the template if functions with the same names didn't exist.

func (Template) Funcs

func (t Template) Funcs() gotemplate.FuncMap

Funcs return available template out of the box and custom functions.

func (*Template) Render

func (t *Template) Render(name string, module *terraform.Module) (string, error)

Render template with given Module struct.

func (*Template) RenderContent

func (t *Template) RenderContent(name string, data interface{}) (string, error)

RenderContent template with given data. It can contain anything but most probably it will only contain terraform.Module and print.generator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL