From 690b0f8ff5795318dfa3834a5a75d6623e7d934a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 1 May 2017 18:40:34 +0200 Subject: tpl: Add docshelper for template funcs And fix some other minor related issues. Updates #3418 --- tpl/transform/init.go | 99 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 37 deletions(-) (limited to 'tpl/transform') diff --git a/tpl/transform/init.go b/tpl/transform/init.go index 98994c0e6..3483d1306 100644 --- a/tpl/transform/init.go +++ b/tpl/transform/init.go @@ -24,47 +24,72 @@ func init() { f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { ctx := New(d) - examples := [][2]string{ - {`{{ "I :heart: Hugo" | emojify }}`, `I ❤️ Hugo`}, - {`{{ .Title | markdownify}}`, `BatMan`}, - {`{{ plainify "Hello world, gophers!" }}`, `Hello world, gophers!`}, - { - `htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band " | safeHTML}}`, - `htmlEscape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, - { - `htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band "}}`, - `htmlEscape 2: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`}, - { - `htmlUnescape 1: {{htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}`, - `htmlUnescape 1: Cathal Garvey & The Sunshine Band `}, - { - `htmlUnescape 2: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape | safeHTML}}`, - `htmlUnescape 2: Cathal Garvey & The Sunshine Band `}, - { - `htmlUnescape 3: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape }}`, - `htmlUnescape 3: Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, - { - `htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band " | htmlUnescape | safeHTML }}`, - `htmlUnescape 4: Cathal Garvey & The Sunshine Band `}, - { - `htmlUnescape 5: {{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlEscape | safeHTML }}`, - `htmlUnescape 5: Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, - } - - return &internal.TemplateFuncsNamespace{ + ns := &internal.TemplateFuncsNamespace{ Name: name, Context: func() interface{} { return ctx }, - Aliases: map[string]interface{}{ - "emojify": ctx.Emojify, - "highlight": ctx.Highlight, - "htmlEscape": ctx.HTMLEscape, - "htmlUnescape": ctx.HTMLUnescape, - "markdownify": ctx.Markdownify, - "plainify": ctx.Plainify, - }, - Examples: examples, } + ns.AddMethodMapping(ctx.Emojify, + []string{"emojify"}, + [][2]string{ + {`{{ "I :heart: Hugo" | emojify }}`, `I ❤️ Hugo`}, + }, + ) + + ns.AddMethodMapping(ctx.Highlight, + []string{"highlight"}, + [][2]string{}, + ) + + ns.AddMethodMapping(ctx.HTMLEscape, + []string{"htmlEscape"}, + [][2]string{ + { + `{{ htmlEscape "Cathal Garvey & The Sunshine Band " | safeHTML}}`, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + { + `{{ htmlEscape "Cathal Garvey & The Sunshine Band "}}`, + `Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`}, + { + `{{ htmlEscape "Cathal Garvey & The Sunshine Band " | htmlUnescape | safeHTML }}`, + `Cathal Garvey & The Sunshine Band `}, + }, + ) + + ns.AddMethodMapping(ctx.HTMLUnescape, + []string{"htmlUnescape"}, + [][2]string{ + { + `{{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}`, + `Cathal Garvey & The Sunshine Band `}, + { + `{{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape | safeHTML}}`, + `Cathal Garvey & The Sunshine Band `}, + { + `{{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape }}`, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + { + `{{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlEscape | safeHTML }}`, + `Cathal Garvey & The Sunshine Band <cathal@foo.bar>`}, + }, + ) + + ns.AddMethodMapping(ctx.Markdownify, + []string{"markdownify"}, + [][2]string{ + {`{{ .Title | markdownify}}`, `BatMan`}, + }, + ) + + ns.AddMethodMapping(ctx.Plainify, + []string{"plainify"}, + [][2]string{ + {`{{ plainify "Hello world, gophers!" }}`, `Hello world, gophers!`}, + }, + ) + + return ns + } internal.AddTemplateFuncsNamespace(f) -- cgit v1.2.3