Skip to content

Commit

Permalink
Adding lower, upper and title functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan De Marino authored and detro committed Jun 29, 2022
1 parent 1998ecc commit f34937b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ using the following data fields and functions:
| Function | Description |
|-----------------|---------------------------------------------------------------------------------------------------|
| `codefile` | Create a Markdown code block with the content of a file. Path is relative to the repository root. |
| `lower` | Equivalent to [`strings.ToLower`](https://pkg.go.dev/strings#ToLower). |
| `plainmarkdown` | Render Markdown content as plaintext. |
| `prefixlines` | Add a prefix to all (newline-separated) lines in a string. |
| `split` | Split string into sub-strings, by a given separator (ex. `split .Name "_"`). |
| `title` | Equivalent to [`strings.ToLower`](https://pkg.go.dev/strings#ToTitle). |
| `tffile` | A special case of the `codefile` function, designed for Terraform files (i.e. `.tf`). |
| `trimspace` | Equivalent to [`strings.TrimSpace`](https://pkg.go.dev/strings#TrimSpace). |
| `upper` | Equivalent to [`strings.ToLower`](https://pkg.go.dev/strings#ToUpper). |

## Disclaimer

Expand Down
21 changes: 13 additions & 8 deletions internal/provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ type (
func newTemplate(name, text string) (*template.Template, error) {
tmpl := template.New(name)

tmpl.Funcs(template.FuncMap(map[string]interface{}{
tmpl.Funcs(map[string]interface{}{
"codefile": tmplfuncs.CodeFile,
"lower": strings.ToLower,
"plainmarkdown": mdplain.PlainMarkdown,
"prefixlines": tmplfuncs.PrefixLines,
"tffile": func(file string) (string, error) {
// TODO: omit comment handling
return tmplfuncs.CodeFile("terraform", file)
},
"trimspace": strings.TrimSpace,
"split": strings.Split,
}))
"split": strings.Split,
"tffile": terraformCodeFile,
"title": strings.ToTitle,
"trimspace": strings.TrimSpace,
"upper": strings.ToUpper,
})

var err error
tmpl, err = tmpl.Parse(text)
Expand All @@ -53,6 +53,11 @@ func newTemplate(name, text string) (*template.Template, error) {
return tmpl, nil
}

func terraformCodeFile(file string) (string, error) {
// TODO: omit comment handling
return tmplfuncs.CodeFile("terraform", file)
}

func renderTemplate(name string, text string, out io.Writer, data interface{}) error {
tmpl, err := newTemplate(name, text)
if err != nil {
Expand Down

0 comments on commit f34937b

Please sign in to comment.