Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var FuncHelp = []string{
"indent <size>: add spaces to beginning of each line",
"missing <message>: return message if the text is empty",
"xml: escape the text for XML, rejecting characters XML 1.0 cannot represent",
}
FuncHelp is help for TemplateFuncMap.
var TemplateFuncMap = template.FuncMap{ "json": func(v any) 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 any) 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 ...any) (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 ...any) (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 }, "xml": func(s string) (string, error) { if !utf8.ValidString(s) { return "", fmt.Errorf("value is not valid UTF-8: %q", s) } for _, r := range s { switch { case r == '\t' || r == '\n' || r == '\r': case r >= 0x20 && r <= 0xD7FF: case r >= 0xE000 && r <= 0xFFFD: case r >= 0x10000 && r <= 0x10FFFF: default: return "", fmt.Errorf("value contains character %U not allowed in XML 1.0: %q", r, s) } } var b bytes.Buffer if err := xml.EscapeText(&b, []byte(s)); err != nil { return "", err } return b.String(), nil }, }
TemplateFuncMap is a text/template FuncMap.
Functions ¶
func ExecuteTemplate ¶
ExecuteTemplate executes a text/template template with the functions in TemplateFuncMap.
func IndentString ¶
IndentString add spaces to beginning of each line.
func MissingString ¶
MissingString returns message if the text is empty.
func PrefixString ¶
PrefixString adds prefix to beginning of each line.
Types ¶
This section is empty.