Skip to content

Commit

Permalink
Add toSentence helper
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Nov 3, 2023
1 parent 4aa6132 commit 0c9be0a
Show file tree
Hide file tree
Showing 4 changed files with 56 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 @@ -15,5 +15,6 @@ The Go template has access to the following helper functions:
* `fromMultiYAML`. Turns a string of multiple YAML documents (separated with `---`) into a slice of maps.
* `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".
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.
15 changes: 15 additions & 0 deletions cmd/render/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var templateFuncs = template.FuncMap{
"fromMultiYAML": fromMultiYAML,
"toYAML": toYAML,
"parseTime": parseTime,
"toSentence": toSentence,
}

// fromMultiYAML turns a string of multiple YAML documents
Expand Down Expand Up @@ -40,3 +41,17 @@ func toYAML(unmarshalled any) (string, error) {
func parseTime(layout, t string) (time.Time, error) {
return time.Parse(layout, t)
}

// 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".
func toSentence(items []string) string {
switch len(items) {
case 0:
return ""
case 1:
return items[0]
default:
return strings.Join(items[0:len(items)-1], ", ") + " and " + items[len(items)-1]
}
}
39 changes: 39 additions & 0 deletions cmd/render/template_funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"testing"
)

func TestToSentence(t *testing.T) {
tests := []struct {
items []string
want string
}{
{
items: []string{},
want: "",
},
{
items: []string{"Earth"},
want: "Earth",
},
{
items: []string{"Earth", "Wind"},
want: "Earth and Wind",
},
{
items: []string{"Earth", "Wind", "Fire"},
want: "Earth, Wind and Fire",
},
}

for i, tc := range tests {
t.Run(fmt.Sprintf("slice with %d items", i), func(t *testing.T) {
got := toSentence(tc.items)
if got != tc.want {
t.Errorf("Want %q, got %q", tc.want, got)
}
})
}
}
1 change: 1 addition & 0 deletions docs/render.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The Go template has access to the following helper functions:
* `fromMultiYAML`. Turns a string of multiple YAML documents (separated with `---`) into a slice of maps.
* `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".

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.

Expand Down

0 comments on commit 0c9be0a

Please sign in to comment.