mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-24 21:56:05 +02:00
tpl: Add docshelper for template funcs
And fix some other minor related issues. Updates #3418
This commit is contained in:
@@ -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}}`, `<strong>BatMan</strong>`},
|
||||
{`{{ plainify "Hello <strong>world</strong>, gophers!" }}`, `Hello world, gophers!`},
|
||||
{
|
||||
`htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}`,
|
||||
`htmlEscape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
{
|
||||
`htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}`,
|
||||
`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 <cathal@foo.bar>`},
|
||||
{
|
||||
`htmlUnescape 2: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape | safeHTML}}`,
|
||||
`htmlUnescape 2: Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
{
|
||||
`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 <cathal@foo.bar>" | htmlUnescape | safeHTML }}`,
|
||||
`htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
{
|
||||
`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 <cathal@foo.bar>" | safeHTML}}`,
|
||||
`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
{
|
||||
`{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}`,
|
||||
`Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`},
|
||||
{
|
||||
`{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }}`,
|
||||
`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.HTMLUnescape,
|
||||
[]string{"htmlUnescape"},
|
||||
[][2]string{
|
||||
{
|
||||
`{{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}`,
|
||||
`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
{
|
||||
`{{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape | safeHTML}}`,
|
||||
`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`},
|
||||
{
|
||||
`{{"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}}`, `<strong>BatMan</strong>`},
|
||||
},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Plainify,
|
||||
[]string{"plainify"},
|
||||
[][2]string{
|
||||
{`{{ plainify "Hello <strong>world</strong>, gophers!" }}`, `Hello world, gophers!`},
|
||||
},
|
||||
)
|
||||
|
||||
return ns
|
||||
|
||||
}
|
||||
|
||||
internal.AddTemplateFuncsNamespace(f)
|
||||
|
Reference in New Issue
Block a user