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
This commit is contained in:
Bjørn Erik Pedersen 2017-04-30 11:34:45 +02:00
parent 8f95172c7a
commit c5373efcf0
6 changed files with 109 additions and 10 deletions

View file

@ -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
}

44
tpl/lang/init.go Normal file
View file

@ -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)
}

View file

@ -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...)
}

View file

@ -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(),

View file

@ -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

View file

@ -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 }}