From fc77b6303c8aeda6362d7e2fc5d0fe52067c1a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 30 Apr 2017 22:43:26 +0200 Subject: [PATCH] tpl/inflect: Make it a package that stands on its own See #3042 --- tpl/inflect/init.go | 50 ++++++++++++++++++++++++++++++ tpl/tplimpl/templateFuncster.go | 3 -- tpl/tplimpl/template_funcs.go | 9 ++---- tpl/tplimpl/template_funcs_test.go | 12 ------- 4 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 tpl/inflect/init.go diff --git a/tpl/inflect/init.go b/tpl/inflect/init.go new file mode 100644 index 000000000..b42ae5af3 --- /dev/null +++ b/tpl/inflect/init.go @@ -0,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) +} diff --git a/tpl/tplimpl/templateFuncster.go b/tpl/tplimpl/templateFuncster.go index 5b2594cdc..a4001f56d 100644 --- a/tpl/tplimpl/templateFuncster.go +++ b/tpl/tplimpl/templateFuncster.go @@ -21,7 +21,6 @@ import ( 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 @@ type templateFuncster struct { cachedPartials partialCache // Namespaces - inflect *inflect.Namespace os *os.Namespace safe *safe.Namespace time *time.Namespace @@ -51,7 +49,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster { cachedPartials: partialCache{p: make(map[string]interface{})}, // Namespaces - inflect: inflect.New(), os: os.New(deps), safe: safe.New(), time: time.New(), diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index cc9711fa9..0bc8f4590 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -30,6 +30,7 @@ import ( _ "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) partialCached(name string, context interface{}, varia 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,14 +101,12 @@ func (t *templateFuncster) initFuncMap() { "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, "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 @@ func (t *templateFuncster) initFuncMap() { "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, diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index 52bf499ac..e894d3286 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -134,16 +134,11 @@ htmlUnescape 2: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo. htmlUnescape 3: {{"Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlUnescape | htmlUnescape }} htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band " | htmlUnescape | safeHTML }} htmlUnescape 5: {{ htmlUnescape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | 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 world, 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 | safeHTML }} 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 2: Cathal Garvey & The Sunshine Band htmlUnescape 3: Cathal Garvey & The Sunshine Band <cathal@foo.bar> htmlUnescape 4: Cathal Garvey & The Sunshine Band htmlUnescape 5: Cathal Garvey & The Sunshine Band <cathal@foo.bar> -humanize 1: My first post -humanize 2: My camel post -humanize 3: 52nd -humanize 4: 103rd markdownify: BatMan 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 safeHTML: Bat&Man safeJS: (1*2) safeURL: http://gohugo.io -singularize: cat strings.TrimPrefix: , world! time: 2015 urlize: bat-man