shithub: hugo

Download patch

ref: 0ab23eb5a87efd7db96e11fb3665f0d30336217c
parent: eefa0703cb1f9935ebc12b8dc91d6955663df4ac
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 30 14:41:13 EDT 2017

tpl/strings: Make it a package that stands on its own

See #3042

--- /dev/null
+++ b/tpl/strings/init.go
@@ -1,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)
+}
--- a/tpl/tplimpl/templateFuncster.go
+++ b/tpl/tplimpl/templateFuncster.go
@@ -29,7 +29,6 @@
 	"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 @@
 	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 @@
 		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),
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -27,6 +27,7 @@
 	// 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 @@
 		"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 @@
 		"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,7 +105,6 @@
 		"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,
@@ -115,7 +111,6 @@
 		"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 @@
 		"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 @@
 		"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 @@
 		"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,
 	}
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -127,7 +127,6 @@
 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 " }}
@@ -134,9 +133,6 @@
 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 <[email protected]>" | safeHTML}}
 htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <[email protected]>"}}
 htmlUnescape 1: {{htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;" | safeHTML}}
@@ -150,7 +146,6 @@
 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 @@
 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 @@
 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,7 +184,6 @@
 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
@@ -207,9 +190,6 @@
 echoParam: en
 emojify: I ❤️ Hugo
 eq: current
-findRE: [go]
-hasPrefix 1: true
-hasPrefix 2: false
 htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;
 htmlEscape 2: Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;[email protected]&amp;gt;
 htmlUnescape 1: Cathal Garvey & The Sunshine Band <[email protected]>
@@ -223,7 +203,6 @@
 humanize 4: 103rd
 in: Substring found!
 jsonify: ["A","B","C"]
-lower: batman
 markdownify: <strong>BatMan</strong>
 md5: b3029f756f98f79e7f1b7f1d1f0dd53b
 print: works!
@@ -239,8 +218,6 @@
 relURL 1: http://gohugo.io/
 relURL 2: /hugo/mystyle.css
 relURL 3: /hugo/42
-replace: Batman and Catwoman
-replaceRE: gohugo.io
 safeCSS: Bat&amp;Man
 safeHTML: Bat&Man
 safeHTML: Bat&Man
@@ -250,18 +227,9 @@
 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
 `