| 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 strings |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/deps" |
| 20 |
"github.com/gohugoio/hugo/tpl/internal" |
| 21 |
) |
| 22 |
|
| 23 |
const name = "strings" |
| 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.Chomp, |
| 35 |
[]string{"chomp"}, |
| 36 |
[][2]string{ |
| 37 |
{`{{ chomp "<p>Blockhead</p>\n" | safeHTML }}`, `<p>Blockhead</p>`}, |
| 38 |
}, |
| 39 |
) |
| 40 |
|
| 41 |
ns.AddMethodMapping(ctx.Contains, |
| 42 |
nil, |
| 43 |
[][2]string{ |
| 44 |
{`{{ strings.Contains "abc" "b" }}`, `true`}, |
| 45 |
{`{{ strings.Contains "abc" "d" }}`, `false`}, |
| 46 |
}, |
| 47 |
) |
| 48 |
|
| 49 |
ns.AddMethodMapping(ctx.ContainsAny, |
| 50 |
nil, |
| 51 |
[][2]string{ |
| 52 |
{`{{ strings.ContainsAny "abc" "bcd" }}`, `true`}, |
| 53 |
{`{{ strings.ContainsAny "abc" "def" }}`, `false`}, |
| 54 |
}, |
| 55 |
) |
| 56 |
|
| 57 |
ns.AddMethodMapping(ctx.ContainsNonSpace, |
| 58 |
nil, |
| 59 |
[][2]string{}, |
| 60 |
) |
| 61 |
|
| 62 |
ns.AddMethodMapping(ctx.Count, |
| 63 |
nil, |
| 64 |
[][2]string{ |
| 65 |
{`{{ "aabab" | strings.Count "a" }}`, `3`}, |
| 66 |
}, |
| 67 |
) |
| 68 |
|
| 69 |
ns.AddMethodMapping(ctx.CountRunes, |
| 70 |
[]string{"countrunes"}, |
| 71 |
[][2]string{}, |
| 72 |
) |
| 73 |
|
| 74 |
ns.AddMethodMapping(ctx.CountWords, |
| 75 |
[]string{"countwords"}, |
| 76 |
[][2]string{}, |
| 77 |
) |
| 78 |
|
| 79 |
ns.AddMethodMapping(ctx.Diff, |
| 80 |
nil, |
| 81 |
[][2]string{}, |
| 82 |
) |
| 83 |
|
| 84 |
ns.AddMethodMapping(ctx.FindRE, |
| 85 |
[]string{"findRE"}, |
| 86 |
[][2]string{ |
| 87 |
{ |
| 88 |
`{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." 1 }}`, |
| 89 |
`[go]`, |
| 90 |
}, |
| 91 |
}, |
| 92 |
) |
| 93 |
|
| 94 |
ns.AddMethodMapping(ctx.FindRESubmatch, |
| 95 |
[]string{"findRESubmatch"}, |
| 96 |
[][2]string{ |
| 97 |
{ |
| 98 |
`{{ findRESubmatch §§<a\s*href="(.+?)">(.+?)</a>§§ §§<li><a href="#foo">Foo</a></li> <li><a href="#bar">Bar</a></li>§§ | print | safeHTML }}`, |
| 99 |
"[[<a href=\"#foo\">Foo</a> #foo Foo] [<a href=\"#bar\">Bar</a> #bar Bar]]", |
| 100 |
}, |
| 101 |
}, |
| 102 |
) |
| 103 |
|
| 104 |
ns.AddMethodMapping(ctx.FirstUpper, |
| 105 |
nil, |
| 106 |
[][2]string{ |
| 107 |
{`{{ "hugo rocks!" | strings.FirstUpper }}`, `Hugo rocks!`}, |
| 108 |
}, |
| 109 |
) |
| 110 |
|
| 111 |
ns.AddMethodMapping(ctx.HasPrefix, |
| 112 |
[]string{"hasPrefix"}, |
| 113 |
[][2]string{ |
| 114 |
{`{{ hasPrefix "Hugo" "Hu" }}`, `true`}, |
| 115 |
{`{{ hasPrefix "Hugo" "Fu" }}`, `false`}, |
| 116 |
}, |
| 117 |
) |
| 118 |
|
| 119 |
ns.AddMethodMapping(ctx.HasSuffix, |
| 120 |
[]string{"hasSuffix"}, |
| 121 |
[][2]string{ |
| 122 |
{`{{ hasSuffix "Hugo" "go" }}`, `true`}, |
| 123 |
{`{{ hasSuffix "Hugo" "du" }}`, `false`}, |
| 124 |
}, |
| 125 |
) |
| 126 |
|
| 127 |
ns.AddMethodMapping(ctx.Repeat, |
| 128 |
nil, |
| 129 |
[][2]string{ |
| 130 |
{`{{ "yo" | strings.Repeat 4 }}`, `yoyoyoyo`}, |
| 131 |
}, |
| 132 |
) |
| 133 |
|
| 134 |
ns.AddMethodMapping(ctx.Replace, |
| 135 |
[]string{"replace"}, |
| 136 |
[][2]string{ |
| 137 |
{ |
| 138 |
`{{ replace "Batman and Robin" "Robin" "Catwoman" }}`, |
| 139 |
`Batman and Catwoman`, |
| 140 |
}, |
| 141 |
{ |
| 142 |
`{{ replace "aabbaabb" "a" "z" 2 }}`, |
| 143 |
`zzbbaabb`, |
| 144 |
}, |
| 145 |
}, |
| 146 |
) |
| 147 |
|
| 148 |
ns.AddMethodMapping(ctx.ReplacePairs, |
| 149 |
nil, |
| 150 |
[][2]string{ |
| 151 |
{ |
| 152 |
`{{ "aab" | strings.ReplacePairs "a" "b" "b" "c" }}`, |
| 153 |
`bbc`, |
| 154 |
}, |
| 155 |
{ |
| 156 |
`{{ "aab" | strings.ReplacePairs (slice "a" "b" "b" "c") }}`, |
| 157 |
`bbc`, |
| 158 |
}, |
| 159 |
}, |
| 160 |
) |
| 161 |
|
| 162 |
ns.AddMethodMapping(ctx.ReplaceRE, |
| 163 |
[]string{"replaceRE"}, |
| 164 |
[][2]string{ |
| 165 |
{ |
| 166 |
`{{ replaceRE "a+b" "X" "aabbaabbab" }}`, |
| 167 |
`XbXbX`, |
| 168 |
}, |
| 169 |
{ |
| 170 |
`{{ replaceRE "a+b" "X" "aabbaabbab" 1 }}`, |
| 171 |
`Xbaabbab`, |
| 172 |
}, |
| 173 |
}, |
| 174 |
) |
| 175 |
|
| 176 |
ns.AddMethodMapping(ctx.RuneCount, |
| 177 |
nil, |
| 178 |
[][2]string{}, |
| 179 |
) |
| 180 |
|
| 181 |
ns.AddMethodMapping(ctx.SliceString, |
| 182 |
[]string{"slicestr"}, |
| 183 |
[][2]string{ |
| 184 |
{`{{ slicestr "BatMan" 0 3 }}`, `Bat`}, |
| 185 |
{`{{ slicestr "BatMan" 3 }}`, `Man`}, |
| 186 |
}, |
| 187 |
) |
| 188 |
|
| 189 |
ns.AddMethodMapping(ctx.Split, |
| 190 |
[]string{"split"}, |
| 191 |
[][2]string{}, |
| 192 |
) |
| 193 |
|
| 194 |
ns.AddMethodMapping(ctx.Substr, |
| 195 |
[]string{"substr"}, |
| 196 |
[][2]string{ |
| 197 |
{`{{ substr "BatMan" 0 -3 }}`, `Bat`}, |
| 198 |
{`{{ substr "BatMan" 3 3 }}`, `Man`}, |
| 199 |
}, |
| 200 |
) |
| 201 |
|
| 202 |
ns.AddMethodMapping(ctx.Title, |
| 203 |
[]string{"title"}, |
| 204 |
[][2]string{ |
| 205 |
{`{{ title "Bat man" }}`, `Bat Man`}, |
| 206 |
{`{{ title "somewhere over the rainbow" }}`, `Somewhere Over the Rainbow`}, |
| 207 |
}, |
| 208 |
) |
| 209 |
|
| 210 |
ns.AddMethodMapping(ctx.ToLower, |
| 211 |
[]string{"lower"}, |
| 212 |
[][2]string{ |
| 213 |
{`{{ lower "BatMan" }}`, `batman`}, |
| 214 |
}, |
| 215 |
) |
| 216 |
|
| 217 |
ns.AddMethodMapping(ctx.ToUpper, |
| 218 |
[]string{"upper"}, |
| 219 |
[][2]string{ |
| 220 |
{`{{ upper "BatMan" }}`, `BATMAN`}, |
| 221 |
}, |
| 222 |
) |
| 223 |
|
| 224 |
ns.AddMethodMapping(ctx.Trim, |
| 225 |
[]string{"trim"}, |
| 226 |
[][2]string{ |
| 227 |
{`{{ trim "++Batman--" "+-" }}`, `Batman`}, |
| 228 |
}, |
| 229 |
) |
| 230 |
|
| 231 |
ns.AddMethodMapping(ctx.TrimLeft, |
| 232 |
nil, |
| 233 |
[][2]string{ |
| 234 |
{`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`}, |
| 235 |
}, |
| 236 |
) |
| 237 |
|
| 238 |
ns.AddMethodMapping(ctx.TrimPrefix, |
| 239 |
nil, |
| 240 |
[][2]string{ |
| 241 |
{`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`}, |
| 242 |
{`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`}, |
| 243 |
}, |
| 244 |
) |
| 245 |
|
| 246 |
ns.AddMethodMapping(ctx.TrimRight, |
| 247 |
nil, |
| 248 |
[][2]string{ |
| 249 |
{`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`}, |
| 250 |
}, |
| 251 |
) |
| 252 |
|
| 253 |
ns.AddMethodMapping(ctx.TrimSpace, |
| 254 |
nil, |
| 255 |
[][2]string{}, |
| 256 |
) |
| 257 |
|
| 258 |
ns.AddMethodMapping(ctx.TrimSuffix, |
| 259 |
nil, |
| 260 |
[][2]string{ |
| 261 |
{`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`}, |
| 262 |
{`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`}, |
| 263 |
}, |
| 264 |
) |
| 265 |
|
| 266 |
ns.AddMethodMapping(ctx.Truncate, |
| 267 |
[]string{"truncate"}, |
| 268 |
[][2]string{ |
| 269 |
{`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`}, |
| 270 |
{`{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}`, `With <a href="/markdown">Markdown …</a>`}, |
| 271 |
}, |
| 272 |
) |
| 273 |
|
| 274 |
return ns |
| 275 |
} |
| 276 |
|
| 277 |
internal.AddTemplateFuncsNamespace(f) |
| 278 |
} |