2017-04-30 16:41:13 +00:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
2023-02-25 08:24:59 +00:00
|
|
|
"context"
|
|
|
|
|
2017-06-13 16:42:45 +00:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/tpl/internal"
|
2017-04-30 16:41:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const name = "strings"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
|
|
|
|
ctx := New(d)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns := &internal.TemplateFuncsNamespace{
|
2017-04-30 16:41:13 +00:00
|
|
|
Name: name,
|
2023-02-25 08:24:59 +00:00
|
|
|
Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
|
2017-04-30 16:41:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.Chomp,
|
|
|
|
[]string{"chomp"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ chomp "<p>Blockhead</p>\n" | safeHTML }}`, `<p>Blockhead</p>`},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.CountRunes,
|
|
|
|
[]string{"countrunes"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2018-06-04 17:47:03 +00:00
|
|
|
ns.AddMethodMapping(ctx.RuneCount,
|
|
|
|
nil,
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.CountWords,
|
|
|
|
[]string{"countwords"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
2020-07-03 08:34:40 +00:00
|
|
|
ns.AddMethodMapping(ctx.Count,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ "aabab" | strings.Count "a" }}`, `3`},
|
2020-07-03 08:34:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-02-23 08:36:11 +00:00
|
|
|
ns.AddMethodMapping(ctx.Contains,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ strings.Contains "abc" "b" }}`, `true`},
|
|
|
|
{`{{ strings.Contains "abc" "d" }}`, `false`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.ContainsAny,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ strings.ContainsAny "abc" "bcd" }}`, `true`},
|
|
|
|
{`{{ strings.ContainsAny "abc" "def" }}`, `false`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.FindRE,
|
|
|
|
[]string{"findRE"},
|
|
|
|
[][2]string{
|
|
|
|
{
|
2023-01-17 08:35:16 +00:00
|
|
|
`{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." 1 }}`,
|
2020-08-28 02:59:20 +00:00
|
|
|
`[go]`,
|
|
|
|
},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-01-17 08:35:16 +00:00
|
|
|
ns.AddMethodMapping(ctx.FindRESubmatch,
|
|
|
|
[]string{"findRESubmatch"},
|
|
|
|
[][2]string{
|
|
|
|
{
|
|
|
|
`{{ findRESubmatch §§<a\s*href="(.+?)">(.+?)</a>§§ §§<li><a href="#foo">Foo</a></li> <li><a href="#bar">Bar</a></li>§§ | print | safeHTML }}`,
|
|
|
|
"[[<a href=\"#foo\">Foo</a> #foo Foo] [<a href=\"#bar\">Bar</a> #bar Bar]]",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.HasPrefix,
|
|
|
|
[]string{"hasPrefix"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ hasPrefix "Hugo" "Hu" }}`, `true`},
|
|
|
|
{`{{ hasPrefix "Hugo" "Fu" }}`, `false`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-03-01 22:29:33 +00:00
|
|
|
ns.AddMethodMapping(ctx.HasSuffix,
|
|
|
|
[]string{"hasSuffix"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ hasSuffix "Hugo" "go" }}`, `true`},
|
|
|
|
{`{{ hasSuffix "Hugo" "du" }}`, `false`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.ToLower,
|
|
|
|
[]string{"lower"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ lower "BatMan" }}`, `batman`},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Replace,
|
|
|
|
[]string{"replace"},
|
|
|
|
[][2]string{
|
|
|
|
{
|
|
|
|
`{{ replace "Batman and Robin" "Robin" "Catwoman" }}`,
|
2020-08-28 02:59:20 +00:00
|
|
|
`Batman and Catwoman`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`{{ replace "aabbaabb" "a" "z" 2 }}`,
|
|
|
|
`zzbbaabb`,
|
|
|
|
},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.ReplaceRE,
|
|
|
|
[]string{"replaceRE"},
|
2020-08-28 14:29:26 +00:00
|
|
|
[][2]string{
|
|
|
|
{
|
|
|
|
`{{ replaceRE "a+b" "X" "aabbaabbab" }}`,
|
|
|
|
`XbXbX`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`{{ replaceRE "a+b" "X" "aabbaabbab" 1 }}`,
|
|
|
|
`Xbaabbab`,
|
|
|
|
},
|
|
|
|
},
|
2017-05-01 16:40:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.SliceString,
|
|
|
|
[]string{"slicestr"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ slicestr "BatMan" 0 3 }}`, `Bat`},
|
|
|
|
{`{{ slicestr "BatMan" 3 }}`, `Man`},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Split,
|
|
|
|
[]string{"split"},
|
|
|
|
[][2]string{},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Substr,
|
|
|
|
[]string{"substr"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ substr "BatMan" 0 -3 }}`, `Bat`},
|
|
|
|
{`{{ substr "BatMan" 3 3 }}`, `Man`},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ns.AddMethodMapping(ctx.Trim,
|
|
|
|
[]string{"trim"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ trim "++Batman--" "+-" }}`, `Batman`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-08-18 07:12:04 +00:00
|
|
|
ns.AddMethodMapping(ctx.TrimLeft,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-08-14 23:30:04 +00:00
|
|
|
ns.AddMethodMapping(ctx.TrimPrefix,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`},
|
|
|
|
{`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-08-18 07:12:04 +00:00
|
|
|
ns.AddMethodMapping(ctx.TrimRight,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-08-14 23:30:04 +00:00
|
|
|
ns.AddMethodMapping(ctx.TrimSuffix,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`},
|
|
|
|
{`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.Title,
|
|
|
|
[]string{"title"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ title "Bat man" }}`, `Bat Man`},
|
|
|
|
{`{{ title "somewhere over the rainbow" }}`, `Somewhere Over the Rainbow`},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-09-07 07:05:25 +00:00
|
|
|
ns.AddMethodMapping(ctx.FirstUpper,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "hugo rocks!" | strings.FirstUpper }}`, `Hugo rocks!`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.Truncate,
|
|
|
|
[]string{"truncate"},
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`},
|
|
|
|
{`{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}`, `With <a href="/markdown">Markdown …</a>`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-06-03 06:55:37 +00:00
|
|
|
ns.AddMethodMapping(ctx.Repeat,
|
|
|
|
nil,
|
|
|
|
[][2]string{
|
|
|
|
{`{{ "yo" | strings.Repeat 4 }}`, `yoyoyoyo`},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 16:40:34 +00:00
|
|
|
ns.AddMethodMapping(ctx.ToUpper,
|
|
|
|
[]string{"upper"},
|
|
|
|
[][2]string{
|
2022-11-25 05:24:18 +00:00
|
|
|
{`{{ upper "BatMan" }}`, `BATMAN`},
|
2017-05-01 16:40:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return ns
|
2017-04-30 16:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal.AddTemplateFuncsNamespace(f)
|
|
|
|
}
|