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

View file

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

View file

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