Skip to content

Commit

Permalink
tpl/math: Make it a package that stands on its own
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 30, 2017
1 parent 8be9fb0 commit 6d3a4ff
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 22 deletions.
53 changes: 53 additions & 0 deletions tpl/math/init.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 0 additions & 3 deletions tpl/tplimpl/templateFuncster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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),
Expand Down
8 changes: 1 addition & 7 deletions tpl/tplimpl/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
60 changes: 48 additions & 12 deletions tpl/tplimpl/template_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()

Expand All @@ -76,15 +124,13 @@ 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 }}
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 }}
Expand All @@ -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!" -}}
Expand Down Expand Up @@ -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"}}
Expand All @@ -155,15 +197,13 @@ 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=
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
Expand All @@ -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!
Expand Down Expand Up @@ -217,7 +254,6 @@ slicestr: Bat
slicestr: Man
sort: [A B C]
strings.TrimPrefix: , world!
sub: 1
substr: Bat
substr: Man
title: Bat Man
Expand Down

0 comments on commit 6d3a4ff

Please sign in to comment.