shithub: hugo

Download patch

ref: 9aee8ace4e04b6b164b3c13d622021168b072baf
parent: 744dccbea4a0dfecea4777a7f0d3bba99c2a9adb
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 30 18:32:40 EDT 2017

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

See #3042

--- /dev/null
+++ b/tpl/encoding/init.go
@@ -1,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)
+}
--- 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/encoding"
 	"github.com/spf13/hugo/tpl/images"
 	"github.com/spf13/hugo/tpl/inflect"
 	"github.com/spf13/hugo/tpl/os"
@@ -37,7 +36,6 @@
 	cachedPartials partialCache
 
 	// Namespaces
-	encoding  *encoding.Namespace
 	images    *images.Namespace
 	inflect   *inflect.Namespace
 	os        *os.Namespace
@@ -55,7 +53,6 @@
 		cachedPartials: partialCache{p: make(map[string]interface{})},
 
 		// Namespaces
-		encoding:  encoding.New(),
 		images:    images.New(deps),
 		inflect:   inflect.New(),
 		os:        os.New(deps),
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -28,6 +28,7 @@
 	_ "github.com/spf13/hugo/tpl/compare"
 	_ "github.com/spf13/hugo/tpl/crypto"
 	_ "github.com/spf13/hugo/tpl/data"
+	_ "github.com/spf13/hugo/tpl/encoding"
 	_ "github.com/spf13/hugo/tpl/lang"
 	_ "github.com/spf13/hugo/tpl/math"
 	_ "github.com/spf13/hugo/tpl/strings"
@@ -84,11 +85,10 @@
 func (t *templateFuncster) initFuncMap() {
 	funcMap := template.FuncMap{
 		// Namespaces
-		"encoding": t.encoding.Namespace,
-		"images":   t.images.Namespace,
-		"inflect":  t.inflect.Namespace,
-		"os":       t.os.Namespace,
-		"safe":     t.safe.Namespace,
+		"images":  t.images.Namespace,
+		"inflect": t.inflect.Namespace,
+		"os":      t.os.Namespace,
+		"safe":    t.safe.Namespace,
 		//"time":        t.time.Namespace,
 		"transform": t.transform.Namespace,
 		"urls":      t.urls.Namespace,
@@ -95,8 +95,6 @@
 
 		"absURL":        t.urls.AbsURL,
 		"absLangURL":    t.urls.AbsLangURL,
-		"base64Decode":  t.encoding.Base64Decode,
-		"base64Encode":  t.encoding.Base64Encode,
 		"dateFormat":    t.time.Format,
 		"emojify":       t.transform.Emojify,
 		"getenv":        t.os.Getenv,
@@ -106,7 +104,6 @@
 		"humanize":      t.inflect.Humanize,
 		"imageConfig":   t.images.Config,
 		"int":           func(v interface{}) (int, error) { return cast.ToIntE(v) },
-		"jsonify":       t.encoding.Jsonify,
 		"markdownify":   t.transform.Markdownify,
 		"now":           t.time.Now,
 		"partial":       t.partial,
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -124,9 +124,6 @@
 absURL: {{ "http://gohugo.io/" | absURL }}
 absURL: {{ "mystyle.css" | absURL }}
 absURL: {{ 42 | absURL }}
-base64Decode 1: {{ "SGVsbG8gd29ybGQ=" | base64Decode }}
-base64Decode 2: {{ 42 | base64Encode | base64Decode }}
-base64Encode: {{ "Hello world" | base64Encode }}
 crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
 dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
 emojify: {{ "I :heart: Hugo" | emojify }}
@@ -141,7 +138,6 @@
 humanize 2: {{ humanize "myCamelPost" }}
 humanize 3: {{ humanize "52" }}
 humanize 4: {{ humanize 103 }}
-jsonify: {{ (slice "A" "B" "C") | jsonify }}
 markdownify: {{ .Title | markdownify}}
 print: {{ print "works!" }}
 printf: {{ printf "%s!" "works" }}
@@ -169,9 +165,6 @@
 absURL: http://gohugo.io/
 absURL: http://mysite.com/hugo/mystyle.css
 absURL: http://mysite.com/hugo/42
-base64Decode 1: Hello world
-base64Decode 2: 42
-base64Encode: SGVsbG8gd29ybGQ=
 crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
 dateFormat: Wednesday, Jan 21, 2015
 emojify: I ❤️ Hugo
@@ -186,7 +179,6 @@
 humanize 2: My camel post
 humanize 3: 52nd
 humanize 4: 103rd
-jsonify: ["A","B","C"]
 markdownify: <strong>BatMan</strong>
 print: works!
 printf: works!