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