mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-18 22:41:37 +00:00
parent
eefa0703cb
commit
0ab23eb5a8
4 changed files with 79 additions and 51 deletions
78
tpl/strings/init.go
Normal file
78
tpl/strings/init.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
// 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 (
|
||||
"github.com/spf13/hugo/deps"
|
||||
"github.com/spf13/hugo/tpl/internal"
|
||||
)
|
||||
|
||||
const name = "strings"
|
||||
|
||||
func init() {
|
||||
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
|
||||
ctx := New(d)
|
||||
|
||||
examples := [][2]string{
|
||||
{`{{chomp "<p>Blockhead</p>\n" }}`, `<p>Blockhead</p>`},
|
||||
{
|
||||
`{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}`,
|
||||
`[go]`},
|
||||
{`{{ hasPrefix "Hugo" "Hu" }}`, `true`},
|
||||
{`{{ hasPrefix "Hugo" "Fu" }}`, `false`},
|
||||
{`{{lower "BatMan"}}`, `batman`},
|
||||
{
|
||||
`{{ replace "Batman and Robin" "Robin" "Catwoman" }}`,
|
||||
`Batman and Catwoman`},
|
||||
{
|
||||
`{{ "http://gohugo.io/docs" | replaceRE "^https?://([^/]+).*" "$1" }}`,
|
||||
`gohugo.io`},
|
||||
{`{{slicestr "BatMan" 0 3}}`, `Bat`},
|
||||
{`{{slicestr "BatMan" 3}}`, `Man`},
|
||||
{`{{substr "BatMan" 0 -3}}`, `Bat`},
|
||||
{`{{substr "BatMan" 3 3}}`, `Man`},
|
||||
{`{{title "Bat man"}}`, `Bat Man`},
|
||||
{`{{ trim "++Batman--" "+-" }}`, `Batman`},
|
||||
{`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`},
|
||||
{`{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}`, `With <a href="/markdown">Markdown …</a>`},
|
||||
{`{{upper "BatMan"}}`, `BATMAN`},
|
||||
}
|
||||
|
||||
return &internal.TemplateFuncsNamespace{
|
||||
Name: name,
|
||||
Context: func() interface{} { return ctx },
|
||||
Aliases: map[string]interface{}{
|
||||
"chomp": ctx.Chomp,
|
||||
"countrunes": ctx.CountRunes,
|
||||
"countwords": ctx.CountWords,
|
||||
"findRE": ctx.FindRE,
|
||||
"hasPrefix": ctx.HasPrefix,
|
||||
"lower": ctx.ToLower,
|
||||
"replace": ctx.Replace,
|
||||
"replaceRE": ctx.ReplaceRE,
|
||||
"slicestr": ctx.SliceString,
|
||||
"split": ctx.Split,
|
||||
"substr": ctx.Substr,
|
||||
"title": ctx.Title,
|
||||
"trim": ctx.Trim,
|
||||
"truncate": ctx.Truncate,
|
||||
"upper": ctx.ToUpper,
|
||||
},
|
||||
Examples: examples,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal.AddTemplateFuncsNamespace(f)
|
||||
}
|
|
@ -29,7 +29,6 @@ import (
|
|||
"github.com/spf13/hugo/tpl/inflect"
|
||||
"github.com/spf13/hugo/tpl/os"
|
||||
"github.com/spf13/hugo/tpl/safe"
|
||||
hstrings "github.com/spf13/hugo/tpl/strings"
|
||||
"github.com/spf13/hugo/tpl/time"
|
||||
"github.com/spf13/hugo/tpl/transform"
|
||||
"github.com/spf13/hugo/tpl/urls"
|
||||
|
@ -49,7 +48,6 @@ type templateFuncster struct {
|
|||
inflect *inflect.Namespace
|
||||
os *os.Namespace
|
||||
safe *safe.Namespace
|
||||
strings *hstrings.Namespace
|
||||
time *time.Namespace
|
||||
transform *transform.Namespace
|
||||
urls *urls.Namespace
|
||||
|
@ -71,7 +69,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
|
|||
inflect: inflect.New(),
|
||||
os: os.New(deps),
|
||||
safe: safe.New(),
|
||||
strings: hstrings.New(deps),
|
||||
time: time.New(),
|
||||
transform: transform.New(deps),
|
||||
urls: urls.New(deps),
|
||||
|
|
|
@ -27,6 +27,7 @@ import (
|
|||
// Init the namespaces
|
||||
_ "github.com/spf13/hugo/tpl/lang"
|
||||
_ "github.com/spf13/hugo/tpl/math"
|
||||
_ "github.com/spf13/hugo/tpl/strings"
|
||||
)
|
||||
|
||||
// Get retrieves partial output from the cache based upon the partial name.
|
||||
|
@ -87,7 +88,6 @@ func (t *templateFuncster) initFuncMap() {
|
|||
"inflect": t.inflect.Namespace,
|
||||
"os": t.os.Namespace,
|
||||
"safe": t.safe.Namespace,
|
||||
"strings": t.strings.Namespace,
|
||||
//"time": t.time.Namespace,
|
||||
"transform": t.transform.Namespace,
|
||||
"urls": t.urls.Namespace,
|
||||
|
@ -98,9 +98,6 @@ func (t *templateFuncster) initFuncMap() {
|
|||
"apply": t.collections.Apply,
|
||||
"base64Decode": t.encoding.Base64Decode,
|
||||
"base64Encode": t.encoding.Base64Encode,
|
||||
"chomp": t.strings.Chomp,
|
||||
"countrunes": t.strings.CountRunes,
|
||||
"countwords": t.strings.CountWords,
|
||||
"default": compare.Default,
|
||||
"dateFormat": t.time.Format,
|
||||
"delimit": t.collections.Delimit,
|
||||
|
@ -108,14 +105,12 @@ func (t *templateFuncster) initFuncMap() {
|
|||
"echoParam": t.collections.EchoParam,
|
||||
"emojify": t.transform.Emojify,
|
||||
"eq": compare.Eq,
|
||||
"findRE": t.strings.FindRE,
|
||||
"first": t.collections.First,
|
||||
"ge": compare.Ge,
|
||||
"getCSV": t.data.GetCSV,
|
||||
"getJSON": t.data.GetJSON,
|
||||
"getenv": t.os.Getenv,
|
||||
"gt": compare.Gt,
|
||||
"hasPrefix": t.strings.HasPrefix,
|
||||
"highlight": t.transform.Highlight,
|
||||
"htmlEscape": t.transform.HTMLEscape,
|
||||
"htmlUnescape": t.transform.HTMLUnescape,
|
||||
|
@ -130,7 +125,6 @@ func (t *templateFuncster) initFuncMap() {
|
|||
"jsonify": t.encoding.Jsonify,
|
||||
"last": t.collections.Last,
|
||||
"le": compare.Le,
|
||||
"lower": t.strings.ToLower,
|
||||
"lt": compare.Lt,
|
||||
"markdownify": t.transform.Markdownify,
|
||||
"md5": t.crypto.MD5,
|
||||
|
@ -150,8 +144,6 @@ func (t *templateFuncster) initFuncMap() {
|
|||
"relURL": t.urls.RelURL,
|
||||
"relLangURL": t.urls.RelLangURL,
|
||||
"relref": t.urls.RelRef,
|
||||
"replace": t.strings.Replace,
|
||||
"replaceRE": t.strings.ReplaceRE,
|
||||
"safeCSS": t.safe.CSS,
|
||||
"safeHTML": t.safe.HTML,
|
||||
"safeHTMLAttr": t.safe.HTMLAttr,
|
||||
|
@ -166,17 +158,10 @@ func (t *templateFuncster) initFuncMap() {
|
|||
"shuffle": t.collections.Shuffle,
|
||||
"singularize": t.inflect.Singularize,
|
||||
"slice": t.collections.Slice,
|
||||
"slicestr": t.strings.SliceString,
|
||||
"sort": t.collections.Sort,
|
||||
"split": t.strings.Split,
|
||||
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
|
||||
"substr": t.strings.Substr,
|
||||
"time": t.time.AsTime,
|
||||
"title": t.strings.Title,
|
||||
"trim": t.strings.Trim,
|
||||
"truncate": t.strings.Truncate,
|
||||
"union": t.collections.Union,
|
||||
"upper": t.strings.ToUpper,
|
||||
"urlize": t.PathSpec.URLize,
|
||||
"where": t.collections.Where,
|
||||
}
|
||||
|
|
|
@ -127,16 +127,12 @@ absURL: {{ 42 | absURL }}
|
|||
base64Decode 1: {{ "SGVsbG8gd29ybGQ=" | base64Decode }}
|
||||
base64Decode 2: {{ 42 | base64Encode | base64Decode }}
|
||||
base64Encode: {{ "Hello world" | base64Encode }}
|
||||
chomp: {{chomp "<p>Blockhead</p>\n" }}
|
||||
crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
|
||||
dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
|
||||
delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
|
||||
echoParam: {{ echoParam .Params "langCode" }}
|
||||
emojify: {{ "I :heart: Hugo" | emojify }}
|
||||
eq: {{ if eq .Section "blog" }}current{{ end }}
|
||||
findRE: {{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}
|
||||
hasPrefix 1: {{ hasPrefix "Hugo" "Hu" }}
|
||||
hasPrefix 2: {{ hasPrefix "Hugo" "Fu" }}
|
||||
htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
|
||||
htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}
|
||||
htmlUnescape 1: {{htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
|
||||
|
@ -150,7 +146,6 @@ humanize 3: {{ humanize "52" }}
|
|||
humanize 4: {{ humanize 103 }}
|
||||
in: {{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}
|
||||
jsonify: {{ (slice "A" "B" "C") | jsonify }}
|
||||
lower: {{lower "BatMan"}}
|
||||
markdownify: {{ .Title | markdownify}}
|
||||
md5: {{ md5 "Hello world, gophers!" }}
|
||||
print: {{ print "works!" }}
|
||||
|
@ -166,8 +161,6 @@ relLangURL: {{ "index.html" | relLangURL }}
|
|||
relURL 1: {{ "http://gohugo.io/" | relURL }}
|
||||
relURL 2: {{ "mystyle.css" | relURL }}
|
||||
relURL 3: {{ mul 2 21 | relURL }}
|
||||
replace: {{ replace "Batman and Robin" "Robin" "Catwoman" }}
|
||||
replaceRE: {{ "http://gohugo.io/docs" | replaceRE "^https?://([^/]+).*" "$1" }}
|
||||
safeCSS: {{ "Bat&Man" | safeCSS | safeCSS }}
|
||||
safeHTML: {{ "Bat&Man" | safeHTML | safeHTML }}
|
||||
safeHTML: {{ "Bat&Man" | safeHTML }}
|
||||
|
@ -177,18 +170,9 @@ seq: {{ seq 3 }}
|
|||
sha1: {{ sha1 "Hello world, gophers!" }}
|
||||
sha256: {{ sha256 "Hello world, gophers!" }}
|
||||
singularize: {{ "cats" | singularize }}
|
||||
slicestr: {{slicestr "BatMan" 0 3}}
|
||||
slicestr: {{slicestr "BatMan" 3}}
|
||||
sort: {{ slice "B" "C" "A" | sort }}
|
||||
strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
|
||||
substr: {{substr "BatMan" 0 -3}}
|
||||
substr: {{substr "BatMan" 3 3}}
|
||||
title: {{title "Bat man"}}
|
||||
time: {{ (time "2015-01-21").Year }}
|
||||
trim: {{ trim "++Batman--" "+-" }}
|
||||
truncate: {{ "this is a very long text" | truncate 10 " ..." }}
|
||||
truncate: {{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}
|
||||
upper: {{upper "BatMan"}}
|
||||
union: {{ union (slice 1 2 3) (slice 3 4 5) }}
|
||||
urlize: {{ "Bat Man" | urlize }}
|
||||
`
|
||||
|
@ -200,16 +184,12 @@ absURL: http://mysite.com/hugo/42
|
|||
base64Decode 1: Hello world
|
||||
base64Decode 2: 42
|
||||
base64Encode: SGVsbG8gd29ybGQ=
|
||||
chomp: <p>Blockhead</p>
|
||||
crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
|
||||
dateFormat: Wednesday, Jan 21, 2015
|
||||
delimit: A, B and C
|
||||
echoParam: en
|
||||
emojify: I ❤️ Hugo
|
||||
eq: current
|
||||
findRE: [go]
|
||||
hasPrefix 1: true
|
||||
hasPrefix 2: false
|
||||
htmlEscape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
||||
htmlEscape 2: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
|
||||
htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
|
||||
|
@ -223,7 +203,6 @@ humanize 3: 52nd
|
|||
humanize 4: 103rd
|
||||
in: Substring found!
|
||||
jsonify: ["A","B","C"]
|
||||
lower: batman
|
||||
markdownify: <strong>BatMan</strong>
|
||||
md5: b3029f756f98f79e7f1b7f1d1f0dd53b
|
||||
print: works!
|
||||
|
@ -239,8 +218,6 @@ relLangURL: /hugo/en/index.html
|
|||
relURL 1: http://gohugo.io/
|
||||
relURL 2: /hugo/mystyle.css
|
||||
relURL 3: /hugo/42
|
||||
replace: Batman and Catwoman
|
||||
replaceRE: gohugo.io
|
||||
safeCSS: Bat&Man
|
||||
safeHTML: Bat&Man
|
||||
safeHTML: Bat&Man
|
||||
|
@ -250,18 +227,9 @@ seq: [1 2 3]
|
|||
sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
|
||||
sha256: 6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46
|
||||
singularize: cat
|
||||
slicestr: Bat
|
||||
slicestr: Man
|
||||
sort: [A B C]
|
||||
strings.TrimPrefix: , world!
|
||||
substr: Bat
|
||||
substr: Man
|
||||
title: Bat Man
|
||||
time: 2015
|
||||
trim: Batman
|
||||
truncate: this is a ...
|
||||
truncate: With <a href="/markdown">Markdown …</a>
|
||||
upper: BATMAN
|
||||
union: [1 2 3 4 5]
|
||||
urlize: bat-man
|
||||
`
|
||||
|
|
Loading…
Reference in a new issue