Add the {{list}} template helper

pull/6/head
Mickaël Menu 3 years ago
parent 08e09d94ae
commit 7c2ca2c62e
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -13,8 +13,9 @@ import (
)
func Init(lang string, logger util.Logger, styler style.Styler) {
helpers.RegisterConcat(logger)
helpers.RegisterConcat()
helpers.RegisterDate(logger)
helpers.RegisterList()
helpers.RegisterPrepend(logger)
helpers.RegisterShell(logger)
helpers.RegisterSlug(lang, logger)

@ -103,6 +103,18 @@ func TestPrependHelper(t *testing.T) {
testString(t, "{{#prepend '> '}}A quote on\nseveral lines{{/prepend}}", nil, "> A quote on\n> several lines")
}
func TestListHelper(t *testing.T) {
test := func(items []string, expected string) {
context := map[string]interface{}{"items": items}
testString(t, "{{list items}}", context, expected)
}
test([]string{}, "")
test([]string{"Item 1"}, " ‣ Item 1\n")
test([]string{"Item 1", "Item 2"}, " ‣ Item 1\n ‣ Item 2\n")
test([]string{"Item 1", "Item 2", "Item 3"}, " ‣ Item 1\n ‣ Item 2\n ‣ Item 3\n")
test([]string{"An item\non several\nlines\n"}, " ‣ An item\n on several\n lines\n")
}
func TestSlugHelper(t *testing.T) {
// inline
testString(t,

@ -2,7 +2,6 @@ package helpers
import (
"github.com/aymerick/raymond"
"github.com/mickael-menu/zk/util"
)
// RegisterConcat registers a {{concat}} template helper which concatenates two
@ -10,7 +9,7 @@ import (
//
// {{concat '> ' 'A quote'}} -> "> A quote"
//
func RegisterConcat(logger util.Logger) {
func RegisterConcat() {
raymond.RegisterHelper("concat", func(a, b string) string {
return a + b
})

@ -0,0 +1,29 @@
package helpers
import (
"strings"
"github.com/aymerick/raymond"
)
// RegisterList registers a {{list}} template helper which formats a slice of
// strings into a bulleted list.
func RegisterList() {
itemify := func(text string) string {
lines := strings.SplitAfter(strings.TrimRight(text, "\n"), "\n")
return " ‣ " + strings.Join(lines, " ")
}
raymond.RegisterHelper("list", func(items []string) string {
res := ""
for _, item := range items {
if item == "" {
continue
}
res += itemify(item) + "\n"
}
return res
})
}

@ -63,27 +63,18 @@ var formatTemplates = map[string]string{
"short": `{{style "title" title}} {{style "path" path}} ({{date created "elapsed"}})
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`,
{{list snippets}}`,
"medium": `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`,
{{list snippets}}`,
"long": `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
Modified: {{date created "short"}}
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`,
{{list snippets}}`,
"full": `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}

@ -21,10 +21,7 @@ func TestDefaultFormat(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, res, `{{style "title" title}} {{style "path" path}} ({{date created "elapsed"}})
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`)
{{list snippets}}`)
}
func TestFormats(t *testing.T) {
@ -42,27 +39,18 @@ func TestFormats(t *testing.T) {
test("short", `{{style "title" title}} {{style "path" path}} ({{date created "elapsed"}})
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`)
{{list snippets}}`)
test("medium", `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`)
{{list snippets}}`)
test("long", `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}
Modified: {{date created "short"}}
{{#each snippets}}
{{prepend " " (concat "‣ " .)}}
{{/each}}
`)
{{list snippets}}`)
test("full", `{{style "title" title}} {{style "path" path}}
Created: {{date created "short"}}

Loading…
Cancel
Save