hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

init.go (4931B)

    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 strings
   15 
   16 import (
   17 	"github.com/gohugoio/hugo/deps"
   18 	"github.com/gohugoio/hugo/tpl/internal"
   19 )
   20 
   21 const name = "strings"
   22 
   23 func init() {
   24 	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
   25 		ctx := New(d)
   26 
   27 		ns := &internal.TemplateFuncsNamespace{
   28 			Name:    name,
   29 			Context: func(args ...any) (any, error) { return ctx, nil },
   30 		}
   31 
   32 		ns.AddMethodMapping(ctx.Chomp,
   33 			[]string{"chomp"},
   34 			[][2]string{
   35 				{`{{chomp "<p>Blockhead</p>\n" | safeHTML }}`, `<p>Blockhead</p>`},
   36 			},
   37 		)
   38 
   39 		ns.AddMethodMapping(ctx.CountRunes,
   40 			[]string{"countrunes"},
   41 			[][2]string{},
   42 		)
   43 
   44 		ns.AddMethodMapping(ctx.RuneCount,
   45 			nil,
   46 			[][2]string{},
   47 		)
   48 
   49 		ns.AddMethodMapping(ctx.CountWords,
   50 			[]string{"countwords"},
   51 			[][2]string{},
   52 		)
   53 
   54 		ns.AddMethodMapping(ctx.Count,
   55 			nil,
   56 			[][2]string{
   57 				{`{{"aabab" | strings.Count "a" }}`, `3`},
   58 			},
   59 		)
   60 
   61 		ns.AddMethodMapping(ctx.Contains,
   62 			nil,
   63 			[][2]string{
   64 				{`{{ strings.Contains "abc" "b" }}`, `true`},
   65 				{`{{ strings.Contains "abc" "d" }}`, `false`},
   66 			},
   67 		)
   68 
   69 		ns.AddMethodMapping(ctx.ContainsAny,
   70 			nil,
   71 			[][2]string{
   72 				{`{{ strings.ContainsAny "abc" "bcd" }}`, `true`},
   73 				{`{{ strings.ContainsAny "abc" "def" }}`, `false`},
   74 			},
   75 		)
   76 
   77 		ns.AddMethodMapping(ctx.FindRE,
   78 			[]string{"findRE"},
   79 			[][2]string{
   80 				{
   81 					`{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}`,
   82 					`[go]`,
   83 				},
   84 			},
   85 		)
   86 
   87 		ns.AddMethodMapping(ctx.HasPrefix,
   88 			[]string{"hasPrefix"},
   89 			[][2]string{
   90 				{`{{ hasPrefix "Hugo" "Hu" }}`, `true`},
   91 				{`{{ hasPrefix "Hugo" "Fu" }}`, `false`},
   92 			},
   93 		)
   94 
   95 		ns.AddMethodMapping(ctx.ToLower,
   96 			[]string{"lower"},
   97 			[][2]string{
   98 				{`{{lower "BatMan"}}`, `batman`},
   99 			},
  100 		)
  101 
  102 		ns.AddMethodMapping(ctx.Replace,
  103 			[]string{"replace"},
  104 			[][2]string{
  105 				{
  106 					`{{ replace "Batman and Robin" "Robin" "Catwoman" }}`,
  107 					`Batman and Catwoman`,
  108 				},
  109 				{
  110 					`{{ replace "aabbaabb" "a" "z" 2 }}`,
  111 					`zzbbaabb`,
  112 				},
  113 			},
  114 		)
  115 
  116 		ns.AddMethodMapping(ctx.ReplaceRE,
  117 			[]string{"replaceRE"},
  118 			[][2]string{
  119 				{
  120 					`{{ replaceRE "a+b" "X" "aabbaabbab" }}`,
  121 					`XbXbX`,
  122 				},
  123 				{
  124 					`{{ replaceRE "a+b" "X" "aabbaabbab" 1 }}`,
  125 					`Xbaabbab`,
  126 				},
  127 			},
  128 		)
  129 
  130 		ns.AddMethodMapping(ctx.SliceString,
  131 			[]string{"slicestr"},
  132 			[][2]string{
  133 				{`{{slicestr "BatMan" 0 3}}`, `Bat`},
  134 				{`{{slicestr "BatMan" 3}}`, `Man`},
  135 			},
  136 		)
  137 
  138 		ns.AddMethodMapping(ctx.Split,
  139 			[]string{"split"},
  140 			[][2]string{},
  141 		)
  142 
  143 		ns.AddMethodMapping(ctx.Substr,
  144 			[]string{"substr"},
  145 			[][2]string{
  146 				{`{{substr "BatMan" 0 -3}}`, `Bat`},
  147 				{`{{substr "BatMan" 3 3}}`, `Man`},
  148 			},
  149 		)
  150 
  151 		ns.AddMethodMapping(ctx.Trim,
  152 			[]string{"trim"},
  153 			[][2]string{
  154 				{`{{ trim "++Batman--" "+-" }}`, `Batman`},
  155 			},
  156 		)
  157 
  158 		ns.AddMethodMapping(ctx.TrimLeft,
  159 			nil,
  160 			[][2]string{
  161 				{`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`},
  162 			},
  163 		)
  164 
  165 		ns.AddMethodMapping(ctx.TrimPrefix,
  166 			nil,
  167 			[][2]string{
  168 				{`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`},
  169 				{`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`},
  170 			},
  171 		)
  172 
  173 		ns.AddMethodMapping(ctx.TrimRight,
  174 			nil,
  175 			[][2]string{
  176 				{`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`},
  177 			},
  178 		)
  179 
  180 		ns.AddMethodMapping(ctx.TrimSuffix,
  181 			nil,
  182 			[][2]string{
  183 				{`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`},
  184 				{`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`},
  185 			},
  186 		)
  187 
  188 		ns.AddMethodMapping(ctx.Title,
  189 			[]string{"title"},
  190 			[][2]string{
  191 				{`{{title "Bat man"}}`, `Bat Man`},
  192 				{`{{title "somewhere over the rainbow"}}`, `Somewhere Over the Rainbow`},
  193 			},
  194 		)
  195 
  196 		ns.AddMethodMapping(ctx.FirstUpper,
  197 			nil,
  198 			[][2]string{
  199 				{`{{ "hugo rocks!" | strings.FirstUpper }}`, `Hugo rocks!`},
  200 			},
  201 		)
  202 
  203 		ns.AddMethodMapping(ctx.Truncate,
  204 			[]string{"truncate"},
  205 			[][2]string{
  206 				{`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`},
  207 				{`{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}`, `With <a href="/markdown">Markdown …</a>`},
  208 			},
  209 		)
  210 
  211 		ns.AddMethodMapping(ctx.Repeat,
  212 			nil,
  213 			[][2]string{
  214 				{`{{ "yo" | strings.Repeat 4 }}`, `yoyoyoyo`},
  215 			},
  216 		)
  217 
  218 		ns.AddMethodMapping(ctx.ToUpper,
  219 			[]string{"upper"},
  220 			[][2]string{
  221 				{`{{upper "BatMan"}}`, `BATMAN`},
  222 			},
  223 		)
  224 
  225 		return ns
  226 	}
  227 
  228 	internal.AddTemplateFuncsNamespace(f)
  229 }