github.com/whatlly/hugo@v0.47.1/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 "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 ...interface{}) interface{} { return ctx }, 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.FindRE, 55 []string{"findRE"}, 56 [][2]string{ 57 { 58 `{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}`, 59 `[go]`}, 60 }, 61 ) 62 63 ns.AddMethodMapping(ctx.HasPrefix, 64 []string{"hasPrefix"}, 65 [][2]string{ 66 {`{{ hasPrefix "Hugo" "Hu" }}`, `true`}, 67 {`{{ hasPrefix "Hugo" "Fu" }}`, `false`}, 68 }, 69 ) 70 71 ns.AddMethodMapping(ctx.ToLower, 72 []string{"lower"}, 73 [][2]string{ 74 {`{{lower "BatMan"}}`, `batman`}, 75 }, 76 ) 77 78 ns.AddMethodMapping(ctx.Replace, 79 []string{"replace"}, 80 [][2]string{ 81 { 82 `{{ replace "Batman and Robin" "Robin" "Catwoman" }}`, 83 `Batman and Catwoman`}, 84 }, 85 ) 86 87 ns.AddMethodMapping(ctx.ReplaceRE, 88 []string{"replaceRE"}, 89 [][2]string{}, 90 ) 91 92 ns.AddMethodMapping(ctx.SliceString, 93 []string{"slicestr"}, 94 [][2]string{ 95 {`{{slicestr "BatMan" 0 3}}`, `Bat`}, 96 {`{{slicestr "BatMan" 3}}`, `Man`}, 97 }, 98 ) 99 100 ns.AddMethodMapping(ctx.Split, 101 []string{"split"}, 102 [][2]string{}, 103 ) 104 105 ns.AddMethodMapping(ctx.Substr, 106 []string{"substr"}, 107 [][2]string{ 108 {`{{substr "BatMan" 0 -3}}`, `Bat`}, 109 {`{{substr "BatMan" 3 3}}`, `Man`}, 110 }, 111 ) 112 113 ns.AddMethodMapping(ctx.Trim, 114 []string{"trim"}, 115 [][2]string{ 116 {`{{ trim "++Batman--" "+-" }}`, `Batman`}, 117 }, 118 ) 119 120 ns.AddMethodMapping(ctx.TrimLeft, 121 nil, 122 [][2]string{ 123 {`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`}, 124 }, 125 ) 126 127 ns.AddMethodMapping(ctx.TrimPrefix, 128 nil, 129 [][2]string{ 130 {`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`}, 131 {`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`}, 132 }, 133 ) 134 135 ns.AddMethodMapping(ctx.TrimRight, 136 nil, 137 [][2]string{ 138 {`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`}, 139 }, 140 ) 141 142 ns.AddMethodMapping(ctx.TrimSuffix, 143 nil, 144 [][2]string{ 145 {`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`}, 146 {`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`}, 147 }, 148 ) 149 150 ns.AddMethodMapping(ctx.Title, 151 []string{"title"}, 152 [][2]string{ 153 {`{{title "Bat man"}}`, `Bat Man`}, 154 {`{{title "somewhere over the rainbow"}}`, `Somewhere Over the Rainbow`}, 155 }, 156 ) 157 158 ns.AddMethodMapping(ctx.Truncate, 159 []string{"truncate"}, 160 [][2]string{ 161 {`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`}, 162 {`{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}`, `With <a href="/markdown">Markdown …</a>`}, 163 }, 164 ) 165 166 ns.AddMethodMapping(ctx.Repeat, 167 nil, 168 [][2]string{ 169 {`{{ "yo" | strings.Repeat 4 }}`, `yoyoyoyo`}, 170 }, 171 ) 172 173 ns.AddMethodMapping(ctx.ToUpper, 174 []string{"upper"}, 175 [][2]string{ 176 {`{{upper "BatMan"}}`, `BATMAN`}, 177 }, 178 ) 179 180 return ns 181 182 } 183 184 internal.AddTemplateFuncsNamespace(f) 185 }