shithub: hugo

Download patch

ref: c5373efcf07aeb161324b3ce844d41a172da42bc
parent: 8f95172c7af334f61d75faad74cc75016804eca6
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 30 07:34:45 EDT 2017

tpl: Add TemplateFuncsNamespaceRegistry

As a first step to remove the hard ties between `tplimpl` and the different namespace packages.

The `lang` package is used as the first example use case.

See #3042

--- /dev/null
+++ b/tpl/internal/templatefuncsRegistry.go
@@ -1,0 +1,47 @@
+// Copyright 2017-present The Hugo Authors. All rights reserved.
+//
+// Portions Copyright The Go Authors.
+
+// 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 internal
+
+import (
+	"github.com/spf13/hugo/deps"
+)
+
+var TemplateFuncsNamespaceRegistry []func(d *deps.Deps) *TemplateFuncsNamespace
+
+func AddTemplateFuncsNamespace(ns func(d *deps.Deps) *TemplateFuncsNamespace) {
+	TemplateFuncsNamespaceRegistry = append(TemplateFuncsNamespaceRegistry, ns)
+}
+
+type TemplateFuncsNamespace struct {
+	// The namespace name, "strings", "lang", etc.
+	Name string
+
+	// This is the method receiver.
+	Context interface{}
+
+	// Any template funcs aliases. This is mainly motivated by keeping
+	// backwards compability, but some new template funcs may also make
+	// sense to give short and snappy aliases.
+	// Note that these aliases are global and will be merged, so the last
+	// key will win.
+	Aliases map[string]interface{}
+
+	// A slice of input/expected examples.
+	// We keep it a the namespace level for now, but may find a way to keep track
+	// of the single template func, for documentation purposes.
+	// Some of these, hopefully just a few, may depend on some test data to run.
+	Examples [][2]string
+}
--- /dev/null
+++ b/tpl/lang/init.go
@@ -1,0 +1,44 @@
+// 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 lang
+
+import (
+	"github.com/spf13/hugo/deps"
+	"github.com/spf13/hugo/tpl/internal"
+)
+
+const name = "lang"
+
+func init() {
+	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
+		ctx := New(d)
+
+		examples := [][2]string{
+			{},
+		}
+
+		return &internal.TemplateFuncsNamespace{
+			Name:    name,
+			Context: func() interface{} { return ctx },
+			Aliases: map[string]interface{}{
+				"i18n": ctx.Translate,
+				"T":    ctx.Translate,
+			},
+			Examples: examples,
+		}
+
+	}
+
+	internal.AddTemplateFuncsNamespace(f)
+}
--- a/tpl/lang/lang.go
+++ b/tpl/lang/lang.go
@@ -31,6 +31,7 @@
 }
 
 // Namespace returns a pointer to the current namespace instance.
+// TODO(bep) namespace remove this and other unused when done.
 func (ns *Namespace) Namespace() *Namespace { return ns }
 
 // Translate ...
@@ -41,9 +42,4 @@
 	}
 
 	return ns.deps.Translate(sid, args...), nil
-}
-
-// T is an alias to Translate.
-func (ns *Namespace) T(id interface{}, args ...interface{}) (string, error) {
-	return ns.Translate(id, args...)
 }
--- a/tpl/tplimpl/templateFuncster.go
+++ b/tpl/tplimpl/templateFuncster.go
@@ -27,7 +27,6 @@
 	"github.com/spf13/hugo/tpl/encoding"
 	"github.com/spf13/hugo/tpl/images"
 	"github.com/spf13/hugo/tpl/inflect"
-	"github.com/spf13/hugo/tpl/lang"
 	"github.com/spf13/hugo/tpl/math"
 	"github.com/spf13/hugo/tpl/os"
 	"github.com/spf13/hugo/tpl/safe"
@@ -49,7 +48,6 @@
 	encoding    *encoding.Namespace
 	images      *images.Namespace
 	inflect     *inflect.Namespace
-	lang        *lang.Namespace
 	math        *math.Namespace
 	os          *os.Namespace
 	safe        *safe.Namespace
@@ -73,7 +71,6 @@
 		encoding:    encoding.New(),
 		images:      images.New(deps),
 		inflect:     inflect.New(),
-		lang:        lang.New(deps),
 		math:        math.New(),
 		os:          os.New(deps),
 		safe:        safe.New(),
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -22,6 +22,10 @@
 
 	"github.com/spf13/cast"
 	"github.com/spf13/hugo/tpl/compare"
+	"github.com/spf13/hugo/tpl/internal"
+
+	// Init the namespaces
+	_ "github.com/spf13/hugo/tpl/lang"
 )
 
 // Get retrieves partial output from the cache based upon the partial name.
@@ -181,8 +185,18 @@
 		"upper":         t.strings.ToUpper,
 		"urlize":        t.PathSpec.URLize,
 		"where":         t.collections.Where,
-		"i18n":          t.lang.Translate,
-		"T":             t.lang.T,
+	}
+
+	// Merge the namespace funcs
+	for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
+		ns := nsf(t.Deps)
+		// TODO(bep) namespace ns.Context is a dummy func just to make this work.
+		// Consider if we can add this context to the rendering context in an easy
+		// way to make this cleaner. Maybe.
+		funcMap[ns.Name] = ns.Context
+		for k, v := range ns.Aliases {
+			funcMap[k] = v
+		}
 	}
 
 	t.funcMap = funcMap
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -70,6 +70,7 @@
 
 	// Add the examples from the docs: As a smoke test and to make sure the examples work.
 	// TODO(bep): docs: fix title example
+	// TODO(bep) namespace remove when done
 	in :=
 		`absLangURL: {{ "index.html" | absLangURL }}
 absURL: {{ "http://gohugo.io/" | absURL }}