mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
c5373efcf0
commit
eefa0703cb
4 changed files with 102 additions and 22 deletions
53
tpl/math/init.go
Normal file
53
tpl/math/init.go
Normal file
|
@ -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)
|
||||
}
|
|
@ -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),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue