2017-04-30 19:52:56 +00:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package collections
|
|
|
|
|
|
|
|
import (
|
2023-02-25 08:24:59 +00:00
|
|
|
"context"
|
|
|
|
|
2017-06-13 16:42:45 +00:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/tpl/internal"
|
2017-04-30 19:52:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const name = "collections"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
|
|
|
|
ctx := New(d)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns := &internal.TemplateFuncsNamespace{
|
2017-04-30 19:52:56 +00:00
|
|
|
Name: name,
|
2023-02-25 08:24:59 +00:00
|
|
|
Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
|
2017-04-30 19:52:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.After,
|
|
|
|
[]string{"after"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Apply,
|
|
|
|
[]string{"apply"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2018-11-05 17:50:11 +00:00
|
|
|
ns.AddMethodMapping(ctx.Complement,
|
|
|
|
[]string{"complement"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ slice "a" "b" "c" "d" "e" "f" | complement (slice "b" "c") (slice "d" "e") }}`, `[a f]`},
|
2018-11-05 17:50:11 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-11-06 12:04:11 +00:00
|
|
|
ns.AddMethodMapping(ctx.SymDiff,
|
|
|
|
[]string{"symdiff"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ slice 1 2 3 | symdiff (slice 3 4) }}`, `[1 2 4]`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.Delimit,
|
|
|
|
[]string{"delimit"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ delimit (slice "A" "B" "C") ", " " and " }}`, `A, B and C`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Dictionary,
|
|
|
|
[]string{"dict"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.EchoParam,
|
|
|
|
[]string{"echoParam"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ echoParam .Params "langCode" }}`, `en`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.First,
|
|
|
|
[]string{"first"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2017-08-19 11:16:00 +00:00
|
|
|
ns.AddMethodMapping(ctx.KeyVals,
|
|
|
|
[]string{"keyVals"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ keyVals "key" "a" "b" }}`, `key: [a b]`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.In,
|
|
|
|
[]string{"in"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Index,
|
|
|
|
[]string{"index"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Intersect,
|
|
|
|
[]string{"intersect"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.IsSet,
|
|
|
|
[]string{"isSet", "isset"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Last,
|
|
|
|
[]string{"last"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Querify,
|
|
|
|
[]string{"querify"},
|
|
|
|
[][2]string{
|
|
|
|
{
|
|
|
|
`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
|
2020-08-28 02:34:45 +00:00
|
|
|
`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`,
|
|
|
|
},
|
2017-05-01 16:40:34 +00:00
|
|
|
{
|
|
|
|
`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
|
2020-08-28 02:34:45 +00:00
|
|
|
`<a href="https://www.google.com?page=3&q=test">Search</a>`,
|
|
|
|
},
|
2021-03-06 18:38:10 +00:00
|
|
|
{
|
|
|
|
`{{ slice "foo" 1 "bar" 2 | querify | safeHTML }}`,
|
|
|
|
`bar=2&foo=1`,
|
|
|
|
},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Shuffle,
|
|
|
|
[]string{"shuffle"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Slice,
|
|
|
|
[]string{"slice"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Sort,
|
|
|
|
[]string{"sort"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Union,
|
|
|
|
[]string{"union"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Where,
|
|
|
|
[]string{"where"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2018-09-10 07:48:10 +00:00
|
|
|
ns.AddMethodMapping(ctx.Append,
|
|
|
|
[]string{"append"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2018-09-08 11:00:36 +00:00
|
|
|
ns.AddMethodMapping(ctx.Group,
|
|
|
|
[]string{"group"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.Seq,
|
|
|
|
[]string{"seq"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ seq 3 }}`, `[1 2 3]`},
|
|
|
|
},
|
|
|
|
)
|
2018-07-06 12:12:10 +00:00
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.NewScratch,
|
|
|
|
[]string{"newScratch"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ $scratch := newScratch }}{{ $scratch.Add "b" 2 }}{{ $scratch.Add "b" 2 }}{{ $scratch.Get "b" }}`, `4`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-29 19:03:38 +00:00
|
|
|
ns.AddMethodMapping(ctx.Uniq,
|
|
|
|
[]string{"uniq"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ slice 1 2 3 2 | uniq }}`, `[1 2 3]`},
|
|
|
|
},
|
|
|
|
)
|
2017-05-01 16:40:34 +00:00
|
|
|
|
2019-07-11 05:12:44 +00:00
|
|
|
ns.AddMethodMapping(ctx.Merge,
|
|
|
|
[]string{"merge"},
|
|
|
|
[][2]string{
|
2020-08-28 02:34:45 +00:00
|
|
|
{
|
|
|
|
`{{ dict "title" "Hugo Rocks!" | collections.Merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") | sort }}`,
|
|
|
|
`[Yes, Hugo Rocks! Hugo Rocks!]`,
|
|
|
|
},
|
|
|
|
{
|
2022-11-25 05:24:18 +00:00
|
|
|
`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") | sort }}`,
|
2020-08-28 02:34:45 +00:00
|
|
|
`[Yes, Hugo Rocks! Hugo Rocks!]`,
|
|
|
|
},
|
|
|
|
{
|
2022-11-25 05:24:18 +00:00
|
|
|
`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") (dict "extra" "For reals!") | sort }}`,
|
2020-08-28 02:34:45 +00:00
|
|
|
`[Yes, Hugo Rocks! For reals! Hugo Rocks!]`,
|
|
|
|
},
|
2019-07-11 05:12:44 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
return ns
|
2017-04-30 19:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal.AddTemplateFuncsNamespace(f)
|
|
|
|
}
|