| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package transform |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/deps" |
| 20 |
"github.com/gohugoio/hugo/tpl/internal" |
| 21 |
) |
| 22 |
|
| 23 |
const name = "transform" |
| 24 |
|
| 25 |
func init() { |
| 26 |
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { |
| 27 |
ctx := New(d) |
| 28 |
|
| 29 |
ns := &internal.TemplateFuncsNamespace{ |
| 30 |
Name: name, |
| 31 |
Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil }, |
| 32 |
} |
| 33 |
|
| 34 |
ns.AddMethodMapping(ctx.CanHighlight, |
| 35 |
nil, |
| 36 |
[][2]string{}, |
| 37 |
) |
| 38 |
|
| 39 |
ns.AddMethodMapping(ctx.Emojify, |
| 40 |
[]string{"emojify"}, |
| 41 |
[][2]string{ |
| 42 |
{`{{ "I :heart: Hugo" | emojify }}`, `I ❤️ Hugo`}, |
| 43 |
}, |
| 44 |
) |
| 45 |
|
| 46 |
ns.AddMethodMapping(ctx.Highlight, |
| 47 |
[]string{"highlight"}, |
| 48 |
[][2]string{}, |
| 49 |
) |
| 50 |
|
| 51 |
ns.AddMethodMapping(ctx.HighlightCodeBlock, |
| 52 |
nil, |
| 53 |
[][2]string{}, |
| 54 |
) |
| 55 |
|
| 56 |
ns.AddMethodMapping(ctx.HTMLEscape, |
| 57 |
[]string{"htmlEscape"}, |
| 58 |
[][2]string{ |
| 59 |
{ |
| 60 |
`{{ htmlEscape "Cathal Garvey & The Sunshine Band <[email protected]>" | safeHTML }}`, |
| 61 |
`Cathal Garvey & The Sunshine Band <[email protected]>`, |
| 62 |
}, |
| 63 |
{ |
| 64 |
`{{ htmlEscape "Cathal Garvey & The Sunshine Band <[email protected]>" }}`, |
| 65 |
`Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;`, |
| 66 |
}, |
| 67 |
{ |
| 68 |
`{{ htmlEscape "Cathal Garvey & The Sunshine Band <[email protected]>" | htmlUnescape | safeHTML }}`, |
| 69 |
`Cathal Garvey & The Sunshine Band <[email protected]>`, |
| 70 |
}, |
| 71 |
}, |
| 72 |
) |
| 73 |
|
| 74 |
ns.AddMethodMapping(ctx.HTMLToMarkdown, |
| 75 |
nil, |
| 76 |
[][2]string{}, |
| 77 |
) |
| 78 |
|
| 79 |
ns.AddMethodMapping(ctx.HTMLUnescape, |
| 80 |
[]string{"htmlUnescape"}, |
| 81 |
[][2]string{ |
| 82 |
{ |
| 83 |
`{{ htmlUnescape "Cathal Garvey & The Sunshine Band <[email protected]>" | safeHTML }}`, |
| 84 |
`Cathal Garvey & The Sunshine Band <[email protected]>`, |
| 85 |
}, |
| 86 |
{ |
| 87 |
`{{ "Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;" | htmlUnescape | htmlUnescape | safeHTML }}`, |
| 88 |
`Cathal Garvey & The Sunshine Band <[email protected]>`, |
| 89 |
}, |
| 90 |
{ |
| 91 |
`{{ "Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;" | htmlUnescape | htmlUnescape }}`, |
| 92 |
`Cathal Garvey & The Sunshine Band <[email protected]>`, |
| 93 |
}, |
| 94 |
{ |
| 95 |
`{{ htmlUnescape "Cathal Garvey & The Sunshine Band <[email protected]>" | htmlEscape | safeHTML }}`, |
| 96 |
`Cathal Garvey & The Sunshine Band <[email protected]>`, |
| 97 |
}, |
| 98 |
}, |
| 99 |
) |
| 100 |
|
| 101 |
ns.AddMethodMapping(ctx.Markdownify, |
| 102 |
[]string{"markdownify"}, |
| 103 |
[][2]string{ |
| 104 |
{`{{ .Title | markdownify }}`, `<strong>BatMan</strong>`}, |
| 105 |
}, |
| 106 |
) |
| 107 |
|
| 108 |
ns.AddMethodMapping(ctx.Plainify, |
| 109 |
[]string{"plainify"}, |
| 110 |
[][2]string{ |
| 111 |
{`{{ plainify "Hello <strong>world</strong>, gophers!" }}`, `Hello world, gophers!`}, |
| 112 |
}, |
| 113 |
) |
| 114 |
|
| 115 |
ns.AddMethodMapping(ctx.PortableText, |
| 116 |
nil, |
| 117 |
[][2]string{}, |
| 118 |
) |
| 119 |
|
| 120 |
ns.AddMethodMapping(ctx.Remarshal, |
| 121 |
nil, |
| 122 |
[][2]string{ |
| 123 |
{`{{ "title = \"Hello World\"" | transform.Remarshal "json" | safeHTML }}`, "{\n \"title\": \"Hello World\"\n}\n"}, |
| 124 |
}, |
| 125 |
) |
| 126 |
|
| 127 |
ns.AddMethodMapping(ctx.ToMath, |
| 128 |
nil, |
| 129 |
[][2]string{}, |
| 130 |
) |
| 131 |
|
| 132 |
ns.AddMethodMapping(ctx.Unmarshal, |
| 133 |
[]string{"unmarshal"}, |
| 134 |
[][2]string{ |
| 135 |
{`{{ "hello = \"Hello World\"" | transform.Unmarshal }}`, "map[hello:Hello World]"}, |
| 136 |
{`{{ "hello = \"Hello World\"" | resources.FromString "data/greetings.toml" | transform.Unmarshal }}`, "map[hello:Hello World]"}, |
| 137 |
}, |
| 138 |
) |
| 139 |
|
| 140 |
ns.AddMethodMapping(ctx.XMLEscape, |
| 141 |
nil, |
| 142 |
[][2]string{ |
| 143 |
{ |
| 144 |
`{{ transform.XMLEscape "<p>abc</p>" }}`, |
| 145 |
`<p>abc</p>`, |
| 146 |
}, |
| 147 |
}, |
| 148 |
) |
| 149 |
|
| 150 |
return ns |
| 151 |
} |
| 152 |
|
| 153 |
internal.AddTemplateFuncsNamespace(f) |
| 154 |
} |