init.go (2152B)
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 compare
15
16 import (
17 "github.com/gohugoio/hugo/deps"
18 "github.com/gohugoio/hugo/langs"
19 "github.com/gohugoio/hugo/tpl/internal"
20 )
21
22 const name = "compare"
23
24 func init() {
25 f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
26 if d.Language == nil {
27 panic("language must be set")
28 }
29
30 ctx := New(langs.GetLocation(d.Language), false)
31
32 ns := &internal.TemplateFuncsNamespace{
33 Name: name,
34 Context: func(args ...any) (any, error) { return ctx, nil },
35 }
36
37 ns.AddMethodMapping(ctx.Default,
38 []string{"default"},
39 [][2]string{
40 {`{{ "Hugo Rocks!" | default "Hugo Rules!" }}`, `Hugo Rocks!`},
41 {`{{ "" | default "Hugo Rules!" }}`, `Hugo Rules!`},
42 },
43 )
44
45 ns.AddMethodMapping(ctx.Eq,
46 []string{"eq"},
47 [][2]string{
48 {`{{ if eq .Section "blog" }}current-section{{ end }}`, `current-section`},
49 },
50 )
51
52 ns.AddMethodMapping(ctx.Ge,
53 []string{"ge"},
54 [][2]string{
55 {`{{ if ge hugo.Version "0.80" }}Reasonable new Hugo version!{{ end }}`, `Reasonable new Hugo version!`},
56 },
57 )
58
59 ns.AddMethodMapping(ctx.Gt,
60 []string{"gt"},
61 [][2]string{},
62 )
63
64 ns.AddMethodMapping(ctx.Le,
65 []string{"le"},
66 [][2]string{},
67 )
68
69 ns.AddMethodMapping(ctx.Lt,
70 []string{"lt"},
71 [][2]string{},
72 )
73
74 ns.AddMethodMapping(ctx.Ne,
75 []string{"ne"},
76 [][2]string{},
77 )
78
79 ns.AddMethodMapping(ctx.Conditional,
80 []string{"cond"},
81 [][2]string{
82 {`{{ cond (eq (add 2 2) 4) "2+2 is 4" "what?" | safeHTML }}`, `2+2 is 4`},
83 },
84 )
85
86 return ns
87 }
88
89 internal.AddTemplateFuncsNamespace(f)
90 }