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

See #3042
This commit is contained in:
Bjørn Erik Pedersen 2017-04-30 22:43:26 +02:00
parent a432c90aee
commit fc77b6303c
4 changed files with 53 additions and 21 deletions

50
tpl/inflect/init.go Normal file
View file

@ -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)
}

View file

@ -21,7 +21,6 @@ import (
bp "github.com/spf13/hugo/bufferpool" bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/deps" "github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/inflect"
"github.com/spf13/hugo/tpl/os" "github.com/spf13/hugo/tpl/os"
"github.com/spf13/hugo/tpl/safe" "github.com/spf13/hugo/tpl/safe"
"github.com/spf13/hugo/tpl/time" "github.com/spf13/hugo/tpl/time"
@ -35,7 +34,6 @@ type templateFuncster struct {
cachedPartials partialCache cachedPartials partialCache
// Namespaces // Namespaces
inflect *inflect.Namespace
os *os.Namespace os *os.Namespace
safe *safe.Namespace safe *safe.Namespace
time *time.Namespace time *time.Namespace
@ -51,7 +49,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
cachedPartials: partialCache{p: make(map[string]interface{})}, cachedPartials: partialCache{p: make(map[string]interface{})},
// Namespaces // Namespaces
inflect: inflect.New(),
os: os.New(deps), os: os.New(deps),
safe: safe.New(), safe: safe.New(),
time: time.New(), time: time.New(),

View file

@ -30,6 +30,7 @@ import (
_ "github.com/spf13/hugo/tpl/data" _ "github.com/spf13/hugo/tpl/data"
_ "github.com/spf13/hugo/tpl/encoding" _ "github.com/spf13/hugo/tpl/encoding"
_ "github.com/spf13/hugo/tpl/images" _ "github.com/spf13/hugo/tpl/images"
_ "github.com/spf13/hugo/tpl/inflect"
_ "github.com/spf13/hugo/tpl/lang" _ "github.com/spf13/hugo/tpl/lang"
_ "github.com/spf13/hugo/tpl/math" _ "github.com/spf13/hugo/tpl/math"
_ "github.com/spf13/hugo/tpl/strings" _ "github.com/spf13/hugo/tpl/strings"
@ -86,9 +87,8 @@ func (t *templateFuncster) partialCached(name string, context interface{}, varia
func (t *templateFuncster) initFuncMap() { func (t *templateFuncster) initFuncMap() {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
// Namespaces // Namespaces
"inflect": t.inflect.Namespace, "os": t.os.Namespace,
"os": t.os.Namespace, "safe": t.safe.Namespace,
"safe": t.safe.Namespace,
//"time": t.time.Namespace, //"time": t.time.Namespace,
"transform": t.transform.Namespace, "transform": t.transform.Namespace,
"urls": t.urls.Namespace, "urls": t.urls.Namespace,
@ -101,14 +101,12 @@ func (t *templateFuncster) initFuncMap() {
"highlight": t.transform.Highlight, "highlight": t.transform.Highlight,
"htmlEscape": t.transform.HTMLEscape, "htmlEscape": t.transform.HTMLEscape,
"htmlUnescape": t.transform.HTMLUnescape, "htmlUnescape": t.transform.HTMLUnescape,
"humanize": t.inflect.Humanize,
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) }, "int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
"markdownify": t.transform.Markdownify, "markdownify": t.transform.Markdownify,
"now": t.time.Now, "now": t.time.Now,
"partial": t.partial, "partial": t.partial,
"partialCached": t.partialCached, "partialCached": t.partialCached,
"plainify": t.transform.Plainify, "plainify": t.transform.Plainify,
"pluralize": t.inflect.Pluralize,
"print": fmt.Sprint, "print": fmt.Sprint,
"printf": fmt.Sprintf, "printf": fmt.Sprintf,
"println": fmt.Sprintln, "println": fmt.Sprintln,
@ -126,7 +124,6 @@ func (t *templateFuncster) initFuncMap() {
"safeURL": t.safe.URL, "safeURL": t.safe.URL,
"sanitizeURL": t.safe.SanitizeURL, "sanitizeURL": t.safe.SanitizeURL,
"sanitizeurl": t.safe.SanitizeURL, "sanitizeurl": t.safe.SanitizeURL,
"singularize": t.inflect.Singularize,
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) }, "string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
"time": t.time.AsTime, "time": t.time.AsTime,
"urlize": t.PathSpec.URLize, "urlize": t.PathSpec.URLize,

View file

@ -134,16 +134,11 @@ htmlUnescape 2: {{"Cathal Garvey & The Sunshine Band <cathal@foo.
htmlUnescape 3: {{"Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | htmlUnescape }} htmlUnescape 3: {{"Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | htmlUnescape }}
htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }} htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }}
htmlUnescape 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlEscape | safeHTML }} htmlUnescape 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlEscape | safeHTML }}
humanize 1: {{ humanize "my-first-post" }}
humanize 2: {{ humanize "myCamelPost" }}
humanize 3: {{ humanize "52" }}
humanize 4: {{ humanize 103 }}
markdownify: {{ .Title | markdownify}} markdownify: {{ .Title | markdownify}}
print: {{ print "works!" }} print: {{ print "works!" }}
printf: {{ printf "%s!" "works" }} printf: {{ printf "%s!" "works" }}
println: {{ println "works!" -}} println: {{ println "works!" -}}
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }} plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
pluralize: {{ "cat" | pluralize }}
readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }} readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }}
readFile: {{ readFile "README.txt" }} readFile: {{ readFile "README.txt" }}
relLangURL: {{ "index.html" | relLangURL }} relLangURL: {{ "index.html" | relLangURL }}
@ -155,7 +150,6 @@ safeHTML: {{ "Bat&Man" | safeHTML | safeHTML }}
safeHTML: {{ "Bat&Man" | safeHTML }} safeHTML: {{ "Bat&Man" | safeHTML }}
safeJS: {{ "(1*2)" | safeJS | safeJS }} safeJS: {{ "(1*2)" | safeJS | safeJS }}
safeURL: {{ "http://gohugo.io" | safeURL | safeURL }} safeURL: {{ "http://gohugo.io" | safeURL | safeURL }}
singularize: {{ "cats" | singularize }}
strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }} strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
time: {{ (time "2015-01-21").Year }} time: {{ (time "2015-01-21").Year }}
urlize: {{ "Bat Man" | urlize }} urlize: {{ "Bat Man" | urlize }}
@ -175,16 +169,11 @@ htmlUnescape 2: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
htmlUnescape 3: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt; htmlUnescape 3: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar> htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt; htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
humanize 1: My first post
humanize 2: My camel post
humanize 3: 52nd
humanize 4: 103rd
markdownify: <strong>BatMan</strong> markdownify: <strong>BatMan</strong>
print: works! print: works!
printf: works! printf: works!
println: works! println: works!
plainify: Hello world, gophers! plainify: Hello world, gophers!
pluralize: cats
readDir: README.txt readDir: README.txt
readFile: Hugo Rocks! readFile: Hugo Rocks!
relLangURL: /hugo/en/index.html relLangURL: /hugo/en/index.html
@ -196,7 +185,6 @@ safeHTML: Bat&Man
safeHTML: Bat&Man safeHTML: Bat&Man
safeJS: (1*2) safeJS: (1*2)
safeURL: http://gohugo.io safeURL: http://gohugo.io
singularize: cat
strings.TrimPrefix: , world! strings.TrimPrefix: , world!
time: 2015 time: 2015
urlize: bat-man urlize: bat-man