Debian Codegraph @

tpl/strings/init.go

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