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

See #3042
This commit is contained in:
Bjørn Erik Pedersen
2017-04-30 21:52:56 +02:00
parent a3bf118eaa
commit 8a49c0b3b8
15 changed files with 156 additions and 141 deletions

View File

@@ -39,6 +39,7 @@ const (
var (
_ tpl.TemplateHandler = (*templateHandler)(nil)
_ tpl.TemplateFuncsGetter = (*templateHandler)(nil)
_ tpl.TemplateTestMocker = (*templateHandler)(nil)
_ tpl.TemplateFinder = (*htmlTemplates)(nil)
_ tpl.TemplateFinder = (*textTemplates)(nil)
@@ -262,6 +263,10 @@ func (t *templateHandler) SetFuncs(funcMap map[string]interface{}) {
t.setFuncs(funcMap)
}
func (t *templateHandler) GetFuncs() map[string]interface{} {
return t.html.funcster.funcMap
}
func (t *htmlTemplates) setFuncs(funcMap map[string]interface{}) {
t.t.Funcs(funcMap)
}

View File

@@ -21,7 +21,6 @@ import (
bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/collections"
"github.com/spf13/hugo/tpl/crypto"
"github.com/spf13/hugo/tpl/encoding"
"github.com/spf13/hugo/tpl/images"
@@ -39,16 +38,15 @@ type templateFuncster struct {
cachedPartials partialCache
// Namespaces
collections *collections.Namespace
crypto *crypto.Namespace
encoding *encoding.Namespace
images *images.Namespace
inflect *inflect.Namespace
os *os.Namespace
safe *safe.Namespace
time *time.Namespace
transform *transform.Namespace
urls *urls.Namespace
crypto *crypto.Namespace
encoding *encoding.Namespace
images *images.Namespace
inflect *inflect.Namespace
os *os.Namespace
safe *safe.Namespace
time *time.Namespace
transform *transform.Namespace
urls *urls.Namespace
*deps.Deps
}
@@ -59,16 +57,15 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
cachedPartials: partialCache{p: make(map[string]interface{})},
// Namespaces
collections: collections.New(deps),
crypto: crypto.New(),
encoding: encoding.New(),
images: images.New(deps),
inflect: inflect.New(),
os: os.New(deps),
safe: safe.New(),
time: time.New(),
transform: transform.New(deps),
urls: urls.New(deps),
crypto: crypto.New(),
encoding: encoding.New(),
images: images.New(deps),
inflect: inflect.New(),
os: os.New(deps),
safe: safe.New(),
time: time.New(),
transform: transform.New(deps),
urls: urls.New(deps),
}
}

View File

@@ -24,6 +24,7 @@ import (
"github.com/spf13/hugo/tpl/internal"
// Init the namespaces
_ "github.com/spf13/hugo/tpl/collections"
_ "github.com/spf13/hugo/tpl/compare"
_ "github.com/spf13/hugo/tpl/data"
_ "github.com/spf13/hugo/tpl/lang"
@@ -82,43 +83,30 @@ func (t *templateFuncster) partialCached(name string, context interface{}, varia
func (t *templateFuncster) initFuncMap() {
funcMap := template.FuncMap{
// Namespaces
"collections": t.collections.Namespace,
"crypto": t.crypto.Namespace,
"encoding": t.encoding.Namespace,
"images": t.images.Namespace,
"inflect": t.inflect.Namespace,
"os": t.os.Namespace,
"safe": t.safe.Namespace,
"crypto": t.crypto.Namespace,
"encoding": t.encoding.Namespace,
"images": t.images.Namespace,
"inflect": t.inflect.Namespace,
"os": t.os.Namespace,
"safe": t.safe.Namespace,
//"time": t.time.Namespace,
"transform": t.transform.Namespace,
"urls": t.urls.Namespace,
"absURL": t.urls.AbsURL,
"absLangURL": t.urls.AbsLangURL,
"after": t.collections.After,
"apply": t.collections.Apply,
"base64Decode": t.encoding.Base64Decode,
"base64Encode": t.encoding.Base64Encode,
"dateFormat": t.time.Format,
"delimit": t.collections.Delimit,
"dict": t.collections.Dictionary,
"echoParam": t.collections.EchoParam,
"emojify": t.transform.Emojify,
"first": t.collections.First,
"getenv": t.os.Getenv,
"highlight": t.transform.Highlight,
"htmlEscape": t.transform.HTMLEscape,
"htmlUnescape": t.transform.HTMLUnescape,
"humanize": t.inflect.Humanize,
"imageConfig": t.images.Config,
"in": t.collections.In,
"index": t.collections.Index,
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
"intersect": t.collections.Intersect,
"isSet": t.collections.IsSet,
"isset": t.collections.IsSet,
"jsonify": t.encoding.Jsonify,
"last": t.collections.Last,
"markdownify": t.transform.Markdownify,
"md5": t.crypto.MD5,
"now": t.time.Now,
@@ -129,7 +117,6 @@ func (t *templateFuncster) initFuncMap() {
"print": fmt.Sprint,
"printf": fmt.Sprintf,
"println": fmt.Sprintln,
"querify": t.collections.Querify,
"readDir": t.os.ReadDir,
"readFile": t.os.ReadFile,
"ref": t.urls.Ref,
@@ -144,18 +131,12 @@ func (t *templateFuncster) initFuncMap() {
"safeURL": t.safe.URL,
"sanitizeURL": t.safe.SanitizeURL,
"sanitizeurl": t.safe.SanitizeURL,
"seq": t.collections.Seq,
"sha1": t.crypto.SHA1,
"sha256": t.crypto.SHA256,
"shuffle": t.collections.Shuffle,
"singularize": t.inflect.Singularize,
"slice": t.collections.Slice,
"sort": t.collections.Sort,
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
"time": t.time.AsTime,
"union": t.collections.Union,
"urlize": t.PathSpec.URLize,
"where": t.collections.Where,
}
// Merge the namespace funcs
@@ -172,5 +153,4 @@ func (t *templateFuncster) initFuncMap() {
t.funcMap = funcMap
t.Tmpl.(*templateHandler).setFuncs(funcMap)
t.collections.Funcs(funcMap)
}

View File

@@ -129,8 +129,6 @@ base64Decode 2: {{ 42 | base64Encode | base64Decode }}
base64Encode: {{ "Hello world" | base64Encode }}
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 }}
htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}
@@ -143,7 +141,6 @@ humanize 1: {{ humanize "my-first-post" }}
humanize 2: {{ humanize "myCamelPost" }}
humanize 3: {{ humanize "52" }}
humanize 4: {{ humanize 103 }}
in: {{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}
jsonify: {{ (slice "A" "B" "C") | jsonify }}
markdownify: {{ .Title | markdownify}}
md5: {{ md5 "Hello world, gophers!" }}
@@ -152,8 +149,6 @@ printf: {{ printf "%s!" "works" }}
println: {{ println "works!" -}}
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
pluralize: {{ "cat" | pluralize }}
querify 1: {{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}
querify 2: <a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>
readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }}
readFile: {{ readFile "README.txt" }}
relLangURL: {{ "index.html" | relLangURL }}
@@ -165,14 +160,11 @@ safeHTML: {{ "Bat&Man" | safeHTML | safeHTML }}
safeHTML: {{ "Bat&Man" | safeHTML }}
safeJS: {{ "(1*2)" | safeJS | safeJS }}
safeURL: {{ "http://gohugo.io" | safeURL | safeURL }}
seq: {{ seq 3 }}
sha1: {{ sha1 "Hello world, gophers!" }}
sha256: {{ sha256 "Hello world, gophers!" }}
singularize: {{ "cats" | singularize }}
sort: {{ slice "B" "C" "A" | sort }}
strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
time: {{ (time "2015-01-21").Year }}
union: {{ union (slice 1 2 3) (slice 3 4 5) }}
urlize: {{ "Bat Man" | urlize }}
`
@@ -185,8 +177,6 @@ base64Decode 2: 42
base64Encode: SGVsbG8gd29ybGQ=
crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
dateFormat: Wednesday, Jan 21, 2015
delimit: A, B and C
echoParam: en
emojify: I ❤️ Hugo
htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
htmlEscape 2: Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;
@@ -199,7 +189,6 @@ humanize 1: My first post
humanize 2: My camel post
humanize 3: 52nd
humanize 4: 103rd
in: Substring found!
jsonify: ["A","B","C"]
markdownify: <strong>BatMan</strong>
md5: b3029f756f98f79e7f1b7f1d1f0dd53b
@@ -208,8 +197,6 @@ printf: works!
println: works!
plainify: Hello world, gophers!
pluralize: cats
querify 1: bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose
querify 2: <a href="https://www.google.com?page=3&amp;q=test">Search</a>
readDir: README.txt
readFile: Hugo Rocks!
relLangURL: /hugo/en/index.html
@@ -221,14 +208,11 @@ safeHTML: Bat&Man
safeHTML: Bat&Man
safeJS: (1*2)
safeURL: http://gohugo.io
seq: [1 2 3]
sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
sha256: 6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46
singularize: cat
sort: [A B C]
strings.TrimPrefix: , world!
time: 2015
union: [1 2 3 4 5]
urlize: bat-man
`