shithub: hugo

Download patch

ref: 744dccbea4a0dfecea4777a7f0d3bba99c2a9adb
parent: 8a49c0b3b8b5a374a64b639f46806192cd663fc9
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 30 18:19:49 EDT 2017

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

See #3042

--- /dev/null
+++ b/tpl/crypto/init.go
@@ -1,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)
+}
--- a/tpl/tplimpl/templateFuncster.go
+++ b/tpl/tplimpl/templateFuncster.go
@@ -21,7 +21,6 @@
 
 	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 @@
 	cachedPartials partialCache
 
 	// Namespaces
-	crypto    *crypto.Namespace
 	encoding  *encoding.Namespace
 	images    *images.Namespace
 	inflect   *inflect.Namespace
@@ -57,7 +55,6 @@
 		cachedPartials: partialCache{p: make(map[string]interface{})},
 
 		// Namespaces
-		crypto:    crypto.New(),
 		encoding:  encoding.New(),
 		images:    images.New(deps),
 		inflect:   inflect.New(),
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -26,6 +26,7 @@
 	// 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) 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 @@
 		"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 @@
 		"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,
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -143,7 +143,6 @@
 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 }}
 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 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
 safeJS: (1*2)
 safeURL: http://gohugo.io
-sha1: c8b5b0e33d408246e30f53e32b8f7627a7a649d4
-sha256: 6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46
 singularize: cat
 strings.TrimPrefix: , world!
 time: 2015