github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/collections/init.go (about)

     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.EchoParam,
    71  			[]string{"echoParam"},
    72  			[][2]string{
    73  				{`{{ echoParam .Params "langCode" }}`, `en`},
    74  			},
    75  		)
    76  
    77  		ns.AddMethodMapping(ctx.First,
    78  			[]string{"first"},
    79  			[][2]string{},
    80  		)
    81  
    82  		ns.AddMethodMapping(ctx.KeyVals,
    83  			[]string{"keyVals"},
    84  			[][2]string{
    85  				{`{{ keyVals "key" "a" "b" }}`, `key: [a b]`},
    86  			},
    87  		)
    88  
    89  		ns.AddMethodMapping(ctx.In,
    90  			[]string{"in"},
    91  			[][2]string{
    92  				{`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
    93  			},
    94  		)
    95  
    96  		ns.AddMethodMapping(ctx.Index,
    97  			[]string{"index"},
    98  			[][2]string{},
    99  		)
   100  
   101  		ns.AddMethodMapping(ctx.Intersect,
   102  			[]string{"intersect"},
   103  			[][2]string{},
   104  		)
   105  
   106  		ns.AddMethodMapping(ctx.IsSet,
   107  			[]string{"isSet", "isset"},
   108  			[][2]string{},
   109  		)
   110  
   111  		ns.AddMethodMapping(ctx.Last,
   112  			[]string{"last"},
   113  			[][2]string{},
   114  		)
   115  
   116  		ns.AddMethodMapping(ctx.Querify,
   117  			[]string{"querify"},
   118  			[][2]string{
   119  				{
   120  					`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
   121  					`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`,
   122  				},
   123  				{
   124  					`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
   125  					`<a href="https://www.google.com?page=3&amp;q=test">Search</a>`,
   126  				},
   127  				{
   128  					`{{ slice "foo" 1 "bar" 2 | querify | safeHTML }}`,
   129  					`bar=2&foo=1`,
   130  				},
   131  			},
   132  		)
   133  
   134  		ns.AddMethodMapping(ctx.Shuffle,
   135  			[]string{"shuffle"},
   136  			[][2]string{},
   137  		)
   138  
   139  		ns.AddMethodMapping(ctx.Slice,
   140  			[]string{"slice"},
   141  			[][2]string{
   142  				{`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
   143  			},
   144  		)
   145  
   146  		ns.AddMethodMapping(ctx.Sort,
   147  			[]string{"sort"},
   148  			[][2]string{},
   149  		)
   150  
   151  		ns.AddMethodMapping(ctx.Union,
   152  			[]string{"union"},
   153  			[][2]string{
   154  				{`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
   155  			},
   156  		)
   157  
   158  		ns.AddMethodMapping(ctx.Where,
   159  			[]string{"where"},
   160  			[][2]string{},
   161  		)
   162  
   163  		ns.AddMethodMapping(ctx.Append,
   164  			[]string{"append"},
   165  			[][2]string{},
   166  		)
   167  
   168  		ns.AddMethodMapping(ctx.Group,
   169  			[]string{"group"},
   170  			[][2]string{},
   171  		)
   172  
   173  		ns.AddMethodMapping(ctx.Seq,
   174  			[]string{"seq"},
   175  			[][2]string{
   176  				{`{{ seq 3 }}`, `[1 2 3]`},
   177  			},
   178  		)
   179  
   180  		ns.AddMethodMapping(ctx.NewScratch,
   181  			[]string{"newScratch"},
   182  			[][2]string{
   183  				{`{{ $scratch := newScratch }}{{ $scratch.Add "b" 2 }}{{ $scratch.Add "b" 2 }}{{ $scratch.Get "b" }}`, `4`},
   184  			},
   185  		)
   186  
   187  		ns.AddMethodMapping(ctx.Uniq,
   188  			[]string{"uniq"},
   189  			[][2]string{
   190  				{`{{ slice 1 2 3 2 | uniq }}`, `[1 2 3]`},
   191  			},
   192  		)
   193  
   194  		ns.AddMethodMapping(ctx.Merge,
   195  			[]string{"merge"},
   196  			[][2]string{
   197  				{
   198  					`{{ dict "title" "Hugo Rocks!" | collections.Merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") | sort }}`,
   199  					`[Yes, Hugo Rocks! Hugo Rocks!]`,
   200  				},
   201  				{
   202  					`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") | sort }}`,
   203  					`[Yes, Hugo Rocks! Hugo Rocks!]`,
   204  				},
   205  				{
   206  					`{{ merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") (dict "extra" "For reals!") | sort }}`,
   207  					`[Yes, Hugo Rocks! For reals! Hugo Rocks!]`,
   208  				},
   209  			},
   210  		)
   211  
   212  		return ns
   213  	}
   214  
   215  	internal.AddTemplateFuncsNamespace(f)
   216  }