diff --git a/tpl/strings/init.go b/tpl/strings/init.go new file mode 100644 index 000000000..8e3dfdef2 --- /dev/null +++ b/tpl/strings/init.go @@ -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 "
Blockhead
\n" }}`, `Blockhead
`}, + { + `{{ 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 Markdown …`}, + {`{{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) +} diff --git a/tpl/tplimpl/templateFuncster.go b/tpl/tplimpl/templateFuncster.go index e533e0db8..770eaf46e 100644 --- a/tpl/tplimpl/templateFuncster.go +++ b/tpl/tplimpl/templateFuncster.go @@ -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), diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index d29bbbd06..acf7790f0 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -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, } diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index ba9c1b77c..3b2600b1b 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -127,16 +127,12 @@ absURL: {{ 42 | absURL }} base64Decode 1: {{ "SGVsbG8gd29ybGQ=" | base64Decode }} base64Decode 2: {{ 42 | base64Encode | base64Decode }} base64Encode: {{ "Hello world" | base64Encode }} -chomp: {{chomp "Blockhead
\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 BandBlockhead
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 & The Sunshine Band <cathal@foo.bar> htmlUnescape 1: Cathal Garvey & The Sunshine Band