shithub: hugo

Download patch

ref: fc77b6303c8aeda6362d7e2fc5d0fe52067c1a8d
parent: a432c90aee51b2f3ddd92b9c4f4cbe762f67adfc
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 30 18:43:26 EDT 2017

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

See #3042

--- /dev/null
+++ b/tpl/inflect/init.go
@@ -1,0 +1,50 @@
+// 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 inflect
+
+import (
+	"github.com/spf13/hugo/deps"
+	"github.com/spf13/hugo/tpl/internal"
+)
+
+const name = "inflect"
+
+func init() {
+	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
+		ctx := New()
+
+		examples := [][2]string{
+			{`{{ humanize "my-first-post" }}`, `My first post`},
+			{`{{ humanize "myCamelPost" }}`, `My camel post`},
+			{`{{ humanize "52" }}`, `52nd`},
+			{`{{ humanize 103 }}`, `103rd`},
+			{`{{ "cat" | pluralize }}`, `cats`},
+			{`{{ "cats" | singularize }}`, `cat`},
+		}
+
+		return &internal.TemplateFuncsNamespace{
+			Name:    name,
+			Context: func() interface{} { return ctx },
+			Aliases: map[string]interface{}{
+				"humanize":    ctx.Humanize,
+				"pluralize":   ctx.Pluralize,
+				"singularize": ctx.Singularize,
+			},
+			Examples: examples,
+		}
+
+	}
+
+	internal.AddTemplateFuncsNamespace(f)
+}
--- a/tpl/tplimpl/templateFuncster.go
+++ b/tpl/tplimpl/templateFuncster.go
@@ -21,7 +21,6 @@
 
 	bp "github.com/spf13/hugo/bufferpool"
 	"github.com/spf13/hugo/deps"
-	"github.com/spf13/hugo/tpl/inflect"
 	"github.com/spf13/hugo/tpl/os"
 	"github.com/spf13/hugo/tpl/safe"
 	"github.com/spf13/hugo/tpl/time"
@@ -35,7 +34,6 @@
 	cachedPartials partialCache
 
 	// Namespaces
-	inflect   *inflect.Namespace
 	os        *os.Namespace
 	safe      *safe.Namespace
 	time      *time.Namespace
@@ -51,7 +49,6 @@
 		cachedPartials: partialCache{p: make(map[string]interface{})},
 
 		// Namespaces
-		inflect:   inflect.New(),
 		os:        os.New(deps),
 		safe:      safe.New(),
 		time:      time.New(),
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -30,6 +30,7 @@
 	_ "github.com/spf13/hugo/tpl/data"
 	_ "github.com/spf13/hugo/tpl/encoding"
 	_ "github.com/spf13/hugo/tpl/images"
+	_ "github.com/spf13/hugo/tpl/inflect"
 	_ "github.com/spf13/hugo/tpl/lang"
 	_ "github.com/spf13/hugo/tpl/math"
 	_ "github.com/spf13/hugo/tpl/strings"
@@ -86,9 +87,8 @@
 func (t *templateFuncster) initFuncMap() {
 	funcMap := template.FuncMap{
 		// Namespaces
-		"inflect": t.inflect.Namespace,
-		"os":      t.os.Namespace,
-		"safe":    t.safe.Namespace,
+		"os":   t.os.Namespace,
+		"safe": t.safe.Namespace,
 		//"time":        t.time.Namespace,
 		"transform": t.transform.Namespace,
 		"urls":      t.urls.Namespace,
@@ -101,7 +101,6 @@
 		"highlight":     t.transform.Highlight,
 		"htmlEscape":    t.transform.HTMLEscape,
 		"htmlUnescape":  t.transform.HTMLUnescape,
-		"humanize":      t.inflect.Humanize,
 		"int":           func(v interface{}) (int, error) { return cast.ToIntE(v) },
 		"markdownify":   t.transform.Markdownify,
 		"now":           t.time.Now,
@@ -108,7 +107,6 @@
 		"partial":       t.partial,
 		"partialCached": t.partialCached,
 		"plainify":      t.transform.Plainify,
-		"pluralize":     t.inflect.Pluralize,
 		"print":         fmt.Sprint,
 		"printf":        fmt.Sprintf,
 		"println":       fmt.Sprintln,
@@ -126,7 +124,6 @@
 		"safeURL":       t.safe.URL,
 		"sanitizeURL":   t.safe.SanitizeURL,
 		"sanitizeurl":   t.safe.SanitizeURL,
-		"singularize":   t.inflect.Singularize,
 		"string":        func(v interface{}) (string, error) { return cast.ToStringE(v) },
 		"time":          t.time.AsTime,
 		"urlize":        t.PathSpec.URLize,
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -134,16 +134,11 @@
 htmlUnescape 3: {{"Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;[email protected]&amp;gt;" | htmlUnescape | htmlUnescape }}
 htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band <[email protected]>" | htmlUnescape | safeHTML }}
 htmlUnescape 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;" | htmlEscape | safeHTML }}
-humanize 1: {{ humanize "my-first-post" }}
-humanize 2: {{ humanize "myCamelPost" }}
-humanize 3: {{ humanize "52" }}
-humanize 4: {{ humanize 103 }}
 markdownify: {{ .Title | markdownify}}
 print: {{ print "works!" }}
 printf: {{ printf "%s!" "works" }}
 println: {{ println "works!" -}}
 plainify: {{ plainify  "Hello <strong>world</strong>, gophers!" }}
-pluralize: {{ "cat" | pluralize }}
 readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }}
 readFile: {{ readFile "README.txt" }}
 relLangURL: {{ "index.html" | relLangURL }}
@@ -155,7 +150,6 @@
 safeHTML: {{ "Bat&Man" | safeHTML }}
 safeJS: {{ "(1*2)" | safeJS | safeJS }}
 safeURL: {{ "http://gohugo.io" | safeURL | safeURL }}
-singularize: {{ "cats" | singularize }}
 strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
 time: {{ (time "2015-01-21").Year }}
 urlize: {{ "Bat Man" | urlize }}
@@ -175,16 +169,11 @@
 htmlUnescape 3: Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;
 htmlUnescape 4: Cathal Garvey & The Sunshine Band <[email protected]>
 htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;[email protected]&gt;
-humanize 1: My first post
-humanize 2: My camel post
-humanize 3: 52nd
-humanize 4: 103rd
 markdownify: <strong>BatMan</strong>
 print: works!
 printf: works!
 println: works!
 plainify: Hello world, gophers!
-pluralize: cats
 readDir: README.txt
 readFile: Hugo Rocks!
 relLangURL: /hugo/en/index.html
@@ -196,7 +185,6 @@
 safeHTML: Bat&Man
 safeJS: (1*2)
 safeURL: http://gohugo.io
-singularize: cat
 strings.TrimPrefix: , world!
 time: 2015
 urlize: bat-man