From 744dccbea4a0dfecea4777a7f0d3bba99c2a9adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 30 Apr 2017 22:19:49 +0200 Subject: [PATCH] tpl/crypto: Make it a package that stands on its own See #3042 --- tpl/crypto/init.go | 47 ++++++++++++++++++++++++++++++ tpl/tplimpl/templateFuncster.go | 3 -- tpl/tplimpl/template_funcs.go | 5 +--- tpl/tplimpl/template_funcs_test.go | 6 ---- 4 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 tpl/crypto/init.go diff --git a/tpl/crypto/init.go b/tpl/crypto/init.go new file mode 100644 index 000000000..81e9b3a1a --- /dev/null +++ b/tpl/crypto/init.go @@ -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) +} diff --git a/tpl/tplimpl/templateFuncster.go b/tpl/tplimpl/templateFuncster.go index c5134f740..2f018cf32 100644 --- a/tpl/tplimpl/templateFuncster.go +++ b/tpl/tplimpl/templateFuncster.go @@ -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(), diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index eb266bc0c..5adfff212 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -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, diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index b7212cfc2..053457557 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -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: BatMan -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