Skip to content

Commit

Permalink
Add keys helper
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Nov 3, 2023
1 parent 0c9be0a commit eb3aed1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/docs/render.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ The Go template has access to the following helper functions:
* `toYAML`. Turns the given object into a YAML string.
* `parseTime`. Parses a string using the specified layout into a `time.Time`. This function is just a wrapper around link:https://pkg.go.dev/time#Parse[time.Parse], see its documentation for details how to specify various layouts. Once parsed, the time can be formatted with `{{$t.Format "<layout>"}}` as per link:https://pkg.go.dev/time#Time.Format[time.Format].
* `toSentence`. Turns a slice into a string enumerating its items. The words are connected with commas, except for the last two words, which are connected with "and".
* `keys`. Returns a slice of all keys of given map.
After the Go template has been rendered, link:https://github.com/asciidoctor/asciidoctor-pdf[asciidoctor-pdf] is used to turn each rendered asciidoc file into a PDF file. The resulting files are placed into the directory specified by `output-dir` (defaulting to `.ods/artifacts/org.opendevstack.pipeline.adoc.pdf` so that created PDFs are preserved as artifacts in Nexus). Theming is possible by specifying the `pdf-theme` parameter as explained in the link:https://docs.asciidoctor.org/pdf-converter/latest/theme/apply-theme/#theme-and-font-directories[Theme and font directories] documentation.
9 changes: 9 additions & 0 deletions cmd/render/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var templateFuncs = template.FuncMap{
"toYAML": toYAML,
"parseTime": parseTime,
"toSentence": toSentence,
"keys": keys,
}

// fromMultiYAML turns a string of multiple YAML documents
Expand Down Expand Up @@ -55,3 +56,11 @@ func toSentence(items []string) string {
return strings.Join(items[0:len(items)-1], ", ") + " and " + items[len(items)-1]
}
}

// keys returns a slice of all keys in map m.
func keys(m map[string]any) (keys []string) {
for k, _ := range m {
keys = append(keys, k)
}
return
}

0 comments on commit eb3aed1

Please sign in to comment.