diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-30 17:45:56 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-01 15:13:41 +0200 |
commit | eefa0703cb1f9935ebc12b8dc91d6955663df4ac (patch) | |
tree | 5d000d453a63685eefc7fce063264eb5c9b124b5 | |
parent | c5373efcf07aeb161324b3ce844d41a172da42bc (diff) |
tpl/math: Make it a package that stands on its own
See #3042
-rw-r--r-- | tpl/math/init.go | 53 | ||||
-rw-r--r-- | tpl/tplimpl/templateFuncster.go | 3 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs.go | 8 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs_test.go | 60 |
4 files changed, 102 insertions, 22 deletions
diff --git a/tpl/math/init.go b/tpl/math/init.go new file mode 100644 index 000000000..69ba2cc17 --- /dev/null +++ b/tpl/math/init.go @@ -0,0 +1,53 @@ +// 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 math + +import ( + "github.com/spf13/hugo/deps" + "github.com/spf13/hugo/tpl/internal" +) + +const name = "math" + +func init() { + f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { + ctx := New() + + examples := [][2]string{ + {"{{add 1 2}}", "3"}, + {"{{div 6 3}}", "2"}, + {"{{mod 15 3}}", "0"}, + {"{{modBool 15 3}}", "true"}, + {"{{mul 2 3}}", "6"}, + {"{{sub 3 2}}", "1"}, + } + + return &internal.TemplateFuncsNamespace{ + Name: name, + Context: func() interface{} { return ctx }, + Aliases: map[string]interface{}{ + "add": ctx.Add, + "div": ctx.Div, + "mod": ctx.Mod, + "modBool": ctx.ModBool, + "mul": ctx.Mul, + "sub": ctx.Sub, + }, + Examples: examples, + } + + } + + internal.AddTemplateFuncsNamespace(f) +} diff --git a/tpl/tplimpl/templateFuncster.go b/tpl/tplimpl/templateFuncster.go index c8afec51a..e533e0db8 100644 --- a/tpl/tplimpl/templateFuncster.go +++ b/tpl/tplimpl/templateFuncster.go @@ -27,7 +27,6 @@ import ( "github.com/spf13/hugo/tpl/encoding" "github.com/spf13/hugo/tpl/images" "github.com/spf13/hugo/tpl/inflect" - "github.com/spf13/hugo/tpl/math" "github.com/spf13/hugo/tpl/os" "github.com/spf13/hugo/tpl/safe" hstrings "github.com/spf13/hugo/tpl/strings" @@ -48,7 +47,6 @@ type templateFuncster struct { encoding *encoding.Namespace images *images.Namespace inflect *inflect.Namespace - math *math.Namespace os *os.Namespace safe *safe.Namespace strings *hstrings.Namespace @@ -71,7 +69,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster { encoding: encoding.New(), images: images.New(deps), inflect: inflect.New(), - math: math.New(), os: os.New(deps), safe: safe.New(), strings: hstrings.New(deps), diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index b1d7d71f9..d29bbbd06 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -26,6 +26,7 @@ import ( // Init the namespaces _ "github.com/spf13/hugo/tpl/lang" + _ "github.com/spf13/hugo/tpl/math" ) // Get retrieves partial output from the cache based upon the partial name. @@ -84,7 +85,6 @@ func (t *templateFuncster) initFuncMap() { "encoding": t.encoding.Namespace, "images": t.images.Namespace, "inflect": t.inflect.Namespace, - "math": t.math.Namespace, "os": t.os.Namespace, "safe": t.safe.Namespace, "strings": t.strings.Namespace, @@ -94,7 +94,6 @@ func (t *templateFuncster) initFuncMap() { "absURL": t.urls.AbsURL, "absLangURL": t.urls.AbsLangURL, - "add": t.math.Add, "after": t.collections.After, "apply": t.collections.Apply, "base64Decode": t.encoding.Base64Decode, @@ -106,7 +105,6 @@ func (t *templateFuncster) initFuncMap() { "dateFormat": t.time.Format, "delimit": t.collections.Delimit, "dict": t.collections.Dictionary, - "div": t.math.Div, "echoParam": t.collections.EchoParam, "emojify": t.transform.Emojify, "eq": compare.Eq, @@ -136,9 +134,6 @@ func (t *templateFuncster) initFuncMap() { "lt": compare.Lt, "markdownify": t.transform.Markdownify, "md5": t.crypto.MD5, - "mod": t.math.Mod, - "modBool": t.math.ModBool, - "mul": t.math.Mul, "ne": compare.Ne, "now": t.time.Now, "partial": t.partial, @@ -175,7 +170,6 @@ func (t *templateFuncster) initFuncMap() { "sort": t.collections.Sort, "split": t.strings.Split, "string": func(v interface{}) (string, error) { return cast.ToStringE(v) }, - "sub": t.math.Sub, "substr": t.strings.Substr, "time": t.time.AsTime, "title": t.strings.Title, diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index bdbe067c9..ba9c1b77c 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -32,6 +32,7 @@ import ( "github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/i18n" "github.com/spf13/hugo/tpl" + "github.com/spf13/hugo/tpl/internal" jww "github.com/spf13/jwalterweatherman" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -54,6 +55,53 @@ func newDepsConfig(cfg config.Provider) deps.DepsCfg { } } +func TestTemplateFuncsExamples(t *testing.T) { + t.Parallel() + + workingDir := "/home/hugo" + + v := viper.New() + + v.Set("workingDir", workingDir) + v.Set("multilingual", true) + + fs := hugofs.NewMem(v) + + afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755) + + d, err := deps.New(newDepsConfig(v)) + require.NoError(t, err) + + var data struct { + Title string + Section string + Params map[string]interface{} + } + + data.Title = "**BatMan**" + data.Section = "blog" + data.Params = map[string]interface{}{"langCode": "en"} + + for _, nsf := range internal.TemplateFuncsNamespaceRegistry { + ns := nsf(d) + for i, example := range ns.Examples { + in, expected := example[0], example[1] + d.WithTemplate = func(templ tpl.TemplateHandler) error { + require.NoError(t, templ.AddTemplate("test", in)) + return nil + } + require.NoError(t, d.LoadResources()) + + var b bytes.Buffer + require.NoError(t, d.Tmpl.Lookup("test").Execute(&b, &data)) + if b.String() != expected { + t.Fatalf("%s[%d]: got %q expected %q", ns.Name, i, b.String(), expected) + } + } + } + +} + func TestFuncsInTemplate(t *testing.T) { t.Parallel() @@ -76,7 +124,6 @@ func TestFuncsInTemplate(t *testing.T) { absURL: {{ "http://gohugo.io/" | absURL }} absURL: {{ "mystyle.css" | absURL }} absURL: {{ 42 | absURL }} -add: {{add 1 2}} base64Decode 1: {{ "SGVsbG8gd29ybGQ=" | base64Decode }} base64Decode 2: {{ 42 | base64Encode | base64Decode }} base64Encode: {{ "Hello world" | base64Encode }} @@ -84,7 +131,6 @@ 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 " }} -div: {{div 6 3}} echoParam: {{ echoParam .Params "langCode" }} emojify: {{ "I :heart: Hugo" | emojify }} eq: {{ if eq .Section "blog" }}current{{ end }} @@ -107,9 +153,6 @@ jsonify: {{ (slice "A" "B" "C") | jsonify }} lower: {{lower "BatMan"}} markdownify: {{ .Title | markdownify}} md5: {{ md5 "Hello world, gophers!" }} -mod: {{mod 15 3}} -modBool: {{modBool 15 3}} -mul: {{mul 2 3}} print: {{ print "works!" }} printf: {{ printf "%s!" "works" }} println: {{ println "works!" -}} @@ -138,7 +181,6 @@ slicestr: {{slicestr "BatMan" 0 3}} slicestr: {{slicestr "BatMan" 3}} sort: {{ slice "B" "C" "A" | sort }} strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }} -sub: {{sub 3 2}} substr: {{substr "BatMan" 0 -3}} substr: {{substr "BatMan" 3 3}} title: {{title "Bat man"}} @@ -155,7 +197,6 @@ urlize: {{ "Bat Man" | urlize }} absURL: http://gohugo.io/ absURL: http://mysite.com/hugo/mystyle.css absURL: http://mysite.com/hugo/42 -add: 3 base64Decode 1: Hello world base64Decode 2: 42 base64Encode: SGVsbG8gd29ybGQ= @@ -163,7 +204,6 @@ chomp: <p>Blockhead</p> crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b dateFormat: Wednesday, Jan 21, 2015 delimit: A, B and C -div: 2 echoParam: en emojify: I ❤️ Hugo eq: current @@ -186,9 +226,6 @@ jsonify: ["A","B","C"] lower: batman markdownify: <strong>BatMan</strong> md5: b3029f756f98f79e7f1b7f1d1f0dd53b -mod: 0 -modBool: true -mul: 6 print: works! printf: works! println: works! @@ -217,7 +254,6 @@ slicestr: Bat slicestr: Man sort: [A B C] strings.TrimPrefix: , world! -sub: 1 substr: Bat substr: Man title: Bat Man |