init.go (4915B)
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 collections
15
16 import (
17 "github.com/gohugoio/hugo/deps"
18 "github.com/gohugoio/hugo/tpl/internal"
19 )
20
21 const name = "collections"
22
23 func init() {
24 f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
25 ctx := New(d)
26
27 ns := &internal.TemplateFuncsNamespace{
28 Name: name,
29 Context: func(args ...any) (any, error) { return ctx, nil },
30 }
31
32 ns.AddMethodMapping(ctx.After,
33 []string{"after"},
34 [][2]string{},
35 )
36
37 ns.AddMethodMapping(ctx.Apply,
38 []string{"apply"},
39 [][2]string{},
40 )
41
42 ns.AddMethodMapping(ctx.Complement,
43 []string{"complement"},
44 [][2]string{
45 {`{{ slice "a" "b" "c" "d" "e" "f" | complement (slice "b" "c") (slice "d" "e") }}`, `[a f]`},
46 },
47 )
48
49 ns.AddMethodMapping(ctx.SymDiff,
50 []string{"symdiff"},
51 [][2]string{
52 {`{{ slice 1 2 3 | symdiff (slice 3 4) }}`, `[1 2 4]`},
53 },
54 )
55
56 ns.AddMethodMapping(ctx.Delimit,
57 []string{"delimit"},
58 [][2]string{
59 {`{{ delimit (slice "A" "B" "C") ", " " and " }}`, `A, B and C`},
60 },
61 )
62
63 ns.AddMethodMapping(ctx.Dictionary,
64 []string{"dict"},
65 [][2]string{},
66 )
67
68 ns.AddMethodMapping(ctx.EchoParam,
69 []string{"echoParam"},
70 [][2]string{
71 {`{{ echoParam .Params "langCode" }}`, `en`},
72 },
73 )
74
75 ns.AddMethodMapping(ctx.First,
76 []string{"first"},
77 [][2]string{},
78 )
79
80 ns.AddMethodMapping(ctx.KeyVals,
81 []string{"keyVals"},
82 [][2]string{
83 {`{{ keyVals "key" "a" "b" }}`, `key: [a b]`},
84 },
85 )
86
87 ns.AddMethodMapping(ctx.In,
88 []string{"in"},
89 [][2]string{
90 {`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
91 },
92 )
93
94 ns.AddMethodMapping(ctx.Index,
95 []string{"index"},
96 [][2]string{},
97 )
98
99 ns.AddMethodMapping(ctx.Intersect,
100 []string{"intersect"},
101 [][2]string{},
102 )
103
104 ns.AddMethodMapping(ctx.IsSet,
105 []string{"isSet", "isset"},
106 [][2]string{},
107 )
108
109 ns.AddMethodMapping(ctx.Last,
110 []string{"last"},
111 [][2]string{},
112 )
113
114 ns.AddMethodMapping(ctx.Querify,
115 []string{"querify"},
116 [][2]string{
117 {
118 `{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
119 `bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`,
120 },
121 {
122 `<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
123 `<a href="https://www.google.com?page=3&q=test">Search</a>`,
124 },
125 {
126 `{{ slice "foo" 1 "bar" 2 | querify | safeHTML }}`,
127 `bar=2&foo=1`,
128 },
129 },
130 )
131
132 ns.AddMethodMapping(ctx.Shuffle,
133 []string{"shuffle"},
134 [][2]string{},
135 )
136
137 ns.AddMethodMapping(ctx.Slice,
138 []string{"slice"},
139 [][2]string{
140 {`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
141 },
142 )
143
144 ns.AddMethodMapping(ctx.Sort,
145 []string{"sort"},
146 [][2]string{},
147 )
148
149 ns.AddMethodMapping(ctx.Union,
150 []string{"union"},
151 [][2]string{
152 {`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
153 },
154 )
155
156 ns.AddMethodMapping(ctx.Where,
157 []string{"where"},
158 [][2]string{},
159 )
160
161 ns.AddMethodMapping(ctx.Append,
162 []string{"append"},
163 [][2]string{},
164 )
165
166 ns.AddMethodMapping(ctx.Group,
167 []string{"group"},
168 [][2]string{},
169 )
170
171 ns.AddMethodMapping(ctx.Seq,
172 []string{"seq"},
173 [][2]string{
174 {`{{ seq 3 }}`, `[1 2 3]`},
175 },
176 )
177
178 ns.AddMethodMapping(ctx.NewScratch,
179 []string{"newScratch"},
180 [][2]string{
181 {`{{ $scratch := newScratch }}{{ $scratch.Add "b" 2 }}{{ $scratch.Add "b" 2 }}{{ $scratch.Get "b" }}`, `4`},
182 },
183 )
184
185 ns.AddMethodMapping(ctx.Uniq,
186 []string{"uniq"},
187 [][2]string{
188 {`{{ slice 1 2 3 2 | uniq }}`, `[1 2 3]`},
189 },
190 )
191
192 ns.AddMethodMapping(ctx.Merge,
193 []string{"merge"},
194 [][2]string{
195 {
196 `{{ dict "title" "Hugo Rocks!" | collections.Merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") | sort }}`,
197 `[Yes, Hugo Rocks! Hugo Rocks!]`,
198 },
199 {
200 `{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") | sort }}`,
201 `[Yes, Hugo Rocks! Hugo Rocks!]`,
202 },
203 {
204 `{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") (dict "extra" "For reals!") | sort }}`,
205 `[Yes, Hugo Rocks! For reals! Hugo Rocks!]`,
206 },
207 },
208 )
209
210 return ns
211 }
212
213 internal.AddTemplateFuncsNamespace(f)
214 }