mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
744dccbea4
commit
9aee8ace4e
4 changed files with 53 additions and 19 deletions
48
tpl/encoding/init.go
Normal file
48
tpl/encoding/init.go
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
// 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 encoding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/hugo/deps"
|
||||||
|
"github.com/spf13/hugo/tpl/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
const name = "encoding"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
|
||||||
|
ctx := New()
|
||||||
|
|
||||||
|
examples := [][2]string{
|
||||||
|
{`{{ (slice "A" "B" "C") | jsonify }}`, `["A","B","C"]`},
|
||||||
|
{`{{ "SGVsbG8gd29ybGQ=" | base64Decode }}`, `Hello world`},
|
||||||
|
{`{{ 42 | base64Encode | base64Decode }}`, `42`},
|
||||||
|
{`{{ "Hello world" | base64Encode }}`, `SGVsbG8gd29ybGQ=`},
|
||||||
|
}
|
||||||
|
|
||||||
|
return &internal.TemplateFuncsNamespace{
|
||||||
|
Name: name,
|
||||||
|
Context: func() interface{} { return ctx },
|
||||||
|
Aliases: map[string]interface{}{
|
||||||
|
"base64Decode": ctx.Base64Decode,
|
||||||
|
"base64Encode": ctx.Base64Encode,
|
||||||
|
"jsonify": ctx.Jsonify,
|
||||||
|
},
|
||||||
|
Examples: examples,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal.AddTemplateFuncsNamespace(f)
|
||||||
|
}
|
|
@ -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/encoding"
|
|
||||||
"github.com/spf13/hugo/tpl/images"
|
"github.com/spf13/hugo/tpl/images"
|
||||||
"github.com/spf13/hugo/tpl/inflect"
|
"github.com/spf13/hugo/tpl/inflect"
|
||||||
"github.com/spf13/hugo/tpl/os"
|
"github.com/spf13/hugo/tpl/os"
|
||||||
|
@ -37,7 +36,6 @@ type templateFuncster struct {
|
||||||
cachedPartials partialCache
|
cachedPartials partialCache
|
||||||
|
|
||||||
// Namespaces
|
// Namespaces
|
||||||
encoding *encoding.Namespace
|
|
||||||
images *images.Namespace
|
images *images.Namespace
|
||||||
inflect *inflect.Namespace
|
inflect *inflect.Namespace
|
||||||
os *os.Namespace
|
os *os.Namespace
|
||||||
|
@ -55,7 +53,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
|
||||||
cachedPartials: partialCache{p: make(map[string]interface{})},
|
cachedPartials: partialCache{p: make(map[string]interface{})},
|
||||||
|
|
||||||
// Namespaces
|
// Namespaces
|
||||||
encoding: encoding.New(),
|
|
||||||
images: images.New(deps),
|
images: images.New(deps),
|
||||||
inflect: inflect.New(),
|
inflect: inflect.New(),
|
||||||
os: os.New(deps),
|
os: os.New(deps),
|
||||||
|
|
|
@ -28,6 +28,7 @@ import (
|
||||||
_ "github.com/spf13/hugo/tpl/compare"
|
_ "github.com/spf13/hugo/tpl/compare"
|
||||||
_ "github.com/spf13/hugo/tpl/crypto"
|
_ "github.com/spf13/hugo/tpl/crypto"
|
||||||
_ "github.com/spf13/hugo/tpl/data"
|
_ "github.com/spf13/hugo/tpl/data"
|
||||||
|
_ "github.com/spf13/hugo/tpl/encoding"
|
||||||
_ "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"
|
||||||
|
@ -84,7 +85,6 @@ 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
|
||||||
"encoding": t.encoding.Namespace,
|
|
||||||
"images": t.images.Namespace,
|
"images": t.images.Namespace,
|
||||||
"inflect": t.inflect.Namespace,
|
"inflect": t.inflect.Namespace,
|
||||||
"os": t.os.Namespace,
|
"os": t.os.Namespace,
|
||||||
|
@ -95,8 +95,6 @@ func (t *templateFuncster) initFuncMap() {
|
||||||
|
|
||||||
"absURL": t.urls.AbsURL,
|
"absURL": t.urls.AbsURL,
|
||||||
"absLangURL": t.urls.AbsLangURL,
|
"absLangURL": t.urls.AbsLangURL,
|
||||||
"base64Decode": t.encoding.Base64Decode,
|
|
||||||
"base64Encode": t.encoding.Base64Encode,
|
|
||||||
"dateFormat": t.time.Format,
|
"dateFormat": t.time.Format,
|
||||||
"emojify": t.transform.Emojify,
|
"emojify": t.transform.Emojify,
|
||||||
"getenv": t.os.Getenv,
|
"getenv": t.os.Getenv,
|
||||||
|
@ -106,7 +104,6 @@ func (t *templateFuncster) initFuncMap() {
|
||||||
"humanize": t.inflect.Humanize,
|
"humanize": t.inflect.Humanize,
|
||||||
"imageConfig": t.images.Config,
|
"imageConfig": t.images.Config,
|
||||||
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
|
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
|
||||||
"jsonify": t.encoding.Jsonify,
|
|
||||||
"markdownify": t.transform.Markdownify,
|
"markdownify": t.transform.Markdownify,
|
||||||
"now": t.time.Now,
|
"now": t.time.Now,
|
||||||
"partial": t.partial,
|
"partial": t.partial,
|
||||||
|
|
|
@ -124,9 +124,6 @@ func TestFuncsInTemplate(t *testing.T) {
|
||||||
absURL: {{ "http://gohugo.io/" | absURL }}
|
absURL: {{ "http://gohugo.io/" | absURL }}
|
||||||
absURL: {{ "mystyle.css" | absURL }}
|
absURL: {{ "mystyle.css" | absURL }}
|
||||||
absURL: {{ 42 | absURL }}
|
absURL: {{ 42 | absURL }}
|
||||||
base64Decode 1: {{ "SGVsbG8gd29ybGQ=" | base64Decode }}
|
|
||||||
base64Decode 2: {{ 42 | base64Encode | base64Decode }}
|
|
||||||
base64Encode: {{ "Hello world" | base64Encode }}
|
|
||||||
crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
|
crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
|
||||||
dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
|
dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
|
||||||
emojify: {{ "I :heart: Hugo" | emojify }}
|
emojify: {{ "I :heart: Hugo" | emojify }}
|
||||||
|
@ -141,7 +138,6 @@ humanize 1: {{ humanize "my-first-post" }}
|
||||||
humanize 2: {{ humanize "myCamelPost" }}
|
humanize 2: {{ humanize "myCamelPost" }}
|
||||||
humanize 3: {{ humanize "52" }}
|
humanize 3: {{ humanize "52" }}
|
||||||
humanize 4: {{ humanize 103 }}
|
humanize 4: {{ humanize 103 }}
|
||||||
jsonify: {{ (slice "A" "B" "C") | jsonify }}
|
|
||||||
markdownify: {{ .Title | markdownify}}
|
markdownify: {{ .Title | markdownify}}
|
||||||
print: {{ print "works!" }}
|
print: {{ print "works!" }}
|
||||||
printf: {{ printf "%s!" "works" }}
|
printf: {{ printf "%s!" "works" }}
|
||||||
|
@ -169,9 +165,6 @@ urlize: {{ "Bat Man" | urlize }}
|
||||||
absURL: http://gohugo.io/
|
absURL: http://gohugo.io/
|
||||||
absURL: http://mysite.com/hugo/mystyle.css
|
absURL: http://mysite.com/hugo/mystyle.css
|
||||||
absURL: http://mysite.com/hugo/42
|
absURL: http://mysite.com/hugo/42
|
||||||
base64Decode 1: Hello world
|
|
||||||
base64Decode 2: 42
|
|
||||||
base64Encode: SGVsbG8gd29ybGQ=
|
|
||||||
crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
|
crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
|
||||||
dateFormat: Wednesday, Jan 21, 2015
|
dateFormat: Wednesday, Jan 21, 2015
|
||||||
emojify: I ❤️ Hugo
|
emojify: I ❤️ Hugo
|
||||||
|
@ -186,7 +179,6 @@ humanize 1: My first post
|
||||||
humanize 2: My camel post
|
humanize 2: My camel post
|
||||||
humanize 3: 52nd
|
humanize 3: 52nd
|
||||||
humanize 4: 103rd
|
humanize 4: 103rd
|
||||||
jsonify: ["A","B","C"]
|
|
||||||
markdownify: <strong>BatMan</strong>
|
markdownify: <strong>BatMan</strong>
|
||||||
print: works!
|
print: works!
|
||||||
printf: works!
|
printf: works!
|
||||||
|
|
Loading…
Reference in a new issue