tpl: Add TemplateFuncsNamespaceRegistry - hugo - [fork] hugo port for 9front
HTML git clone git@git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
DIR commit c5373efcf07aeb161324b3ce844d41a172da42bc
DIR parent 8f95172c7af334f61d75faad74cc75016804eca6
HTML Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Sun, 30 Apr 2017 11:34:45 +0200
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
Diffstat:
A tpl/internal/templatefuncsRegistry… | 47 +++++++++++++++++++++++++++++++
A tpl/lang/init.go | 44 +++++++++++++++++++++++++++++++
M tpl/lang/lang.go | 6 +-----
M tpl/tplimpl/templateFuncster.go | 3 ---
M tpl/tplimpl/template_funcs.go | 18 ++++++++++++++++--
M tpl/tplimpl/template_funcs_test.go | 1 +
6 files changed, 109 insertions(+), 10 deletions(-)
---
DIR diff --git a/tpl/internal/templatefuncsRegistry.go b/tpl/internal/templatefuncsRegistry.go
@@ -0,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
+}
DIR diff --git a/tpl/lang/init.go b/tpl/lang/init.go
@@ -0,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)
+}
DIR diff --git a/tpl/lang/lang.go b/tpl/lang/lang.go
@@ -31,6 +31,7 @@ type Namespace struct {
}
// 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 ...
@@ -42,8 +43,3 @@ func (ns *Namespace) Translate(id interface{}, args ...interface{}) (string, err
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...)
-}
DIR diff --git a/tpl/tplimpl/templateFuncster.go b/tpl/tplimpl/templateFuncster.go
@@ -27,7 +27,6 @@ import (
"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 @@ type templateFuncster struct {
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 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
encoding: encoding.New(),
images: images.New(deps),
inflect: inflect.New(),
- lang: lang.New(deps),
math: math.New(),
os: os.New(deps),
safe: safe.New(),
DIR diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go
@@ -22,6 +22,10 @@ import (
"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 @@ func (t *templateFuncster) initFuncMap() {
"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
DIR diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go
@@ -70,6 +70,7 @@ func TestFuncsInTemplate(t *testing.T) {
// 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 }}