Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var FuncHelp = []string{
"indent <size>: add spaces to beginning of each line",
"missing <message>: return message if the text is empty",
}
FuncHelp is help for TemplateFuncMap.
View Source
var TemplateFuncMap = template.FuncMap{ "json": func(v interface{}) string { var b bytes.Buffer enc := json.NewEncoder(&b) enc.SetEscapeHTML(false) if err := enc.Encode(v); err != nil { panic(fmt.Errorf("failed to marshal as JSON: %+v: %w", v, err)) } return strings.TrimSuffix(b.String(), "\n") }, "yaml": func(v interface{}) string { var b bytes.Buffer enc := yaml.NewEncoder(&b) if err := enc.Encode(v); err != nil { panic(fmt.Errorf("failed to marshal as YAML: %+v: %w", v, err)) } return "---\n" + strings.TrimSuffix(b.String(), "\n") }, "indent": func(a ...interface{}) (string, error) { if len(a) == 0 { return "", errors.New("function takes at least one string argument") } if len(a) > 2 { return "", errors.New("function takes at most 2 arguments") } var ok bool size := 2 if len(a) > 1 { if size, ok = a[0].(int); !ok { return "", errors.New("optional first argument must be an integer") } } text := "" if text, ok = a[len(a)-1].(string); !ok { return "", errors.New("last argument must be a string") } return IndentString(size, text), nil }, "missing": func(a ...interface{}) (string, error) { if len(a) == 0 { return "", errors.New("function takes at least one string argument") } if len(a) > 2 { return "", errors.New("function takes at most 2 arguments") } var ok bool message := "<missing>" if len(a) > 1 { if message, ok = a[0].(string); !ok { return "", errors.New("optional first argument must be a string") } } text := "" if text, ok = a[len(a)-1].(string); !ok { return "", errors.New("last argument must be a string") } return MissingString(message, text), nil }, }
TemplateFuncMap is a text/template FuncMap.
Functions ¶
func ExecuteTemplate ¶
ExecuteTemplate executes a text/template template.
func IndentString ¶ added in v0.15.0
IndentString add spaces to beginning of each line.
func MissingString ¶ added in v0.15.0
MissingString returns message if the text is empty.
func PrefixString ¶ added in v0.15.0
PrefixString adds prefix to beginning of each line.
func TrimString ¶ added in v0.15.0
TrimString removes characters from beginning and end.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.