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

See #3042
This commit is contained in:
Bjørn Erik Pedersen 2017-04-30 22:19:49 +02:00
parent 8a49c0b3b8
commit 744dccbea4
4 changed files with 48 additions and 13 deletions

47
tpl/crypto/init.go Normal file
View file

@ -0,0 +1,47 @@
// 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 crypto
import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/internal"
)
const name = "crypto"
func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New()
examples := [][2]string{
{`{{ md5 "Hello world, gophers!" }}`, `b3029f756f98f79e7f1b7f1d1f0dd53b`},
{`{{ sha1 "Hello world, gophers!" }}`, `c8b5b0e33d408246e30f53e32b8f7627a7a649d4`},
{`{{ sha256 "Hello world, gophers!" }}`, `6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46`},
}
return &internal.TemplateFuncsNamespace{
Name: name,
Context: func() interface{} { return ctx },
Aliases: map[string]interface{}{
"md5": ctx.MD5,
"sha1": ctx.SHA1,
"sha256": ctx.SHA256,
},
Examples: examples,
}
}
internal.AddTemplateFuncsNamespace(f)
}

View file

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

View file

@ -26,6 +26,7 @@ import (
// Init the namespaces
_ "github.com/spf13/hugo/tpl/collections"
_ "github.com/spf13/hugo/tpl/compare"
_ "github.com/spf13/hugo/tpl/crypto"
_ "github.com/spf13/hugo/tpl/data"
_ "github.com/spf13/hugo/tpl/lang"
_ "github.com/spf13/hugo/tpl/math"
@ -83,7 +84,6 @@ func (t *templateFuncster) partialCached(name string, context interface{}, varia
func (t *templateFuncster) initFuncMap() {
funcMap := template.FuncMap{
// Namespaces
"crypto": t.crypto.Namespace,
"encoding": t.encoding.Namespace,
"images": t.images.Namespace,
"inflect": t.inflect.Namespace,
@ -108,7 +108,6 @@ func (t *templateFuncster) initFuncMap() {
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
"jsonify": t.encoding.Jsonify,
"markdownify": t.transform.Markdownify,
"md5": t.crypto.MD5,
"now": t.time.Now,
"partial": t.partial,
"partialCached": t.partialCached,
@ -131,8 +130,6 @@ func (t *templateFuncster) initFuncMap() {
"safeURL": t.safe.URL,
"sanitizeURL": t.safe.SanitizeURL,
"sanitizeurl": t.safe.SanitizeURL,
"sha1": t.crypto.SHA1,
"sha256": t.crypto.SHA256,
"singularize": t.inflect.Singularize,
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
"time": t.time.AsTime,

View file

@ -143,7 +143,6 @@ humanize 3: {{ humanize "52" }}
humanize 4: {{ humanize 103 }}
jsonify: {{ (slice "A" "B" "C") | jsonify }}
markdownify: {{ .Title | markdownify}}
md5: {{ md5 "Hello world, gophers!" }}
print: {{ print "works!" }}
printf: {{ printf "%s!" "works" }}
println: {{ println "works!" -}}
@ -160,8 +159,6 @@ safeHTML: {{ "Bat&Man" | safeHTML | safeHTML }}
safeHTML: {{ "Bat&Man" | safeHTML }}
safeJS: {{ "(1*2)" | safeJS | safeJS }}
safeURL: {{ "http://gohugo.io" | safeURL | safeURL }}
sha1: {{ sha1 "Hello world, gophers!" }}
sha256: {{ sha256 "Hello world, gophers!" }}
singularize: {{ "cats" | singularize }}
strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
time: {{ (time "2015-01-21").Year }}
@ -191,7 +188,6 @@ humanize 3: 52nd
humanize 4: 103rd
jsonify: ["A","B","C"]
markdownify: <strong>BatMan</strong>
md5: b3029f756f98f79e7f1b7f1d1f0dd53b
print: works!
printf: works!
println: works!
@ -208,8 +204,6 @@ safeHTML: Bat&Man
safeHTML: Bat&Man
safeJS: (1*2)
safeURL: http://gohugo.io
sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
sha256: 6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46
singularize: cat
strings.TrimPrefix: , world!
time: 2015