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