Add custom template functions
(content string, data *TemplateData)
| 130 | |
| 131 | // Add custom template functions |
| 132 | func executeTemplate(content string, data *TemplateData) (string, error) { |
| 133 | tmpl := template.New("policy").Funcs(template.FuncMap{ |
| 134 | "sanitize": sanitizeName, |
| 135 | "trimSpace": strings.TrimSpace, |
| 136 | "indent": func(spaces int, s string) string { |
| 137 | pad := strings.Repeat(" ", spaces) |
| 138 | return pad + strings.ReplaceAll(s, "\n", "\n"+pad) |
| 139 | }, |
| 140 | }) |
| 141 | |
| 142 | tmpl, err := tmpl.Parse(content) |
| 143 | if err != nil { |
| 144 | return "", fmt.Errorf("template parsing error: %w", err) |
| 145 | } |
| 146 | |
| 147 | var buf bytes.Buffer |
| 148 | if err := tmpl.Execute(&buf, data); err != nil { |
| 149 | return "", fmt.Errorf("template execution error: %w", err) |
| 150 | } |
| 151 | |
| 152 | return buf.String(), nil |
| 153 | } |
| 154 | |
| 155 | func sanitizeName(name string) string { |
| 156 | return strings.ToLower(strings.ReplaceAll(strings.TrimSpace(name), " ", "-")) |