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