From 7c2ca2c62ec3526c733403e5ee1c071a7dd97bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Sat, 30 Jan 2021 19:30:46 +0100 Subject: [PATCH] Add the {{list}} template helper --- adapter/handlebars/handlebars.go | 3 ++- adapter/handlebars/handlebars_test.go | 12 +++++++++++ adapter/handlebars/helpers/concat.go | 3 +-- adapter/handlebars/helpers/list.go | 29 +++++++++++++++++++++++++++ core/note/format.go | 15 +++----------- core/note/format_test.go | 20 ++++-------------- 6 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 adapter/handlebars/helpers/list.go diff --git a/adapter/handlebars/handlebars.go b/adapter/handlebars/handlebars.go index 6fb59c9..60cc353 100644 --- a/adapter/handlebars/handlebars.go +++ b/adapter/handlebars/handlebars.go @@ -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) diff --git a/adapter/handlebars/handlebars_test.go b/adapter/handlebars/handlebars_test.go index d4817fb..c630b5f 100644 --- a/adapter/handlebars/handlebars_test.go +++ b/adapter/handlebars/handlebars_test.go @@ -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, diff --git a/adapter/handlebars/helpers/concat.go b/adapter/handlebars/helpers/concat.go index bc4f39f..2437907 100644 --- a/adapter/handlebars/helpers/concat.go +++ b/adapter/handlebars/helpers/concat.go @@ -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 }) diff --git a/adapter/handlebars/helpers/list.go b/adapter/handlebars/helpers/list.go new file mode 100644 index 0000000..cab9432 --- /dev/null +++ b/adapter/handlebars/helpers/list.go @@ -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 + }) +} diff --git a/core/note/format.go b/core/note/format.go index aea01cb..0dffff4 100644 --- a/core/note/format.go +++ b/core/note/format.go @@ -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"}} diff --git a/core/note/format_test.go b/core/note/format_test.go index 5346a04..f2eec7f 100644 --- a/core/note/format_test.go +++ b/core/note/format_test.go @@ -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"}}