diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-30 22:52:47 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-01 15:13:41 +0200 |
commit | b958c0c1091574d0bd8118045fdf421c7d946893 (patch) | |
tree | 1b03de756f284383f4e60a19b384ae3c8863a82a | |
parent | fc77b6303c8aeda6362d7e2fc5d0fe52067c1a8d (diff) |
tpl/os: Make it a package that stands on its own
See #3042
-rw-r--r-- | tpl/os/init.go | 46 | ||||
-rw-r--r-- | tpl/tplimpl/templateFuncster.go | 3 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs.go | 5 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs_test.go | 8 |
4 files changed, 50 insertions, 12 deletions
diff --git a/tpl/os/init.go b/tpl/os/init.go new file mode 100644 index 000000000..4ab1c56ca --- /dev/null +++ b/tpl/os/init.go @@ -0,0 +1,46 @@ +// 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 os + +import ( + "github.com/spf13/hugo/deps" + "github.com/spf13/hugo/tpl/internal" +) + +const name = "os" + +func init() { + f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { + ctx := New(d) + + examples := [][2]string{ + {`{{ range (readDir ".") }}{{ .Name }}{{ end }}`, `README.txt`}, + {`{{ readFile "README.txt" }}`, `Hugo Rocks!`}, + } + + return &internal.TemplateFuncsNamespace{ + Name: name, + Context: func() interface{} { return ctx }, + Aliases: map[string]interface{}{ + "getenv": ctx.Getenv, + "readDir": ctx.ReadDir, + "readFile": ctx.ReadFile, + }, + Examples: examples, + } + + } + + internal.AddTemplateFuncsNamespace(f) +} diff --git a/tpl/tplimpl/templateFuncster.go b/tpl/tplimpl/templateFuncster.go index a4001f56d..747ec4930 100644 --- a/tpl/tplimpl/templateFuncster.go +++ b/tpl/tplimpl/templateFuncster.go @@ -21,7 +21,6 @@ import ( bp "github.com/spf13/hugo/bufferpool" "github.com/spf13/hugo/deps" - "github.com/spf13/hugo/tpl/os" "github.com/spf13/hugo/tpl/safe" "github.com/spf13/hugo/tpl/time" "github.com/spf13/hugo/tpl/transform" @@ -34,7 +33,6 @@ type templateFuncster struct { cachedPartials partialCache // Namespaces - os *os.Namespace safe *safe.Namespace time *time.Namespace transform *transform.Namespace @@ -49,7 +47,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster { cachedPartials: partialCache{p: make(map[string]interface{})}, // Namespaces - os: os.New(deps), safe: safe.New(), time: time.New(), transform: transform.New(deps), diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index 0bc8f4590..8179d9cbf 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -33,6 +33,7 @@ import ( _ "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/strings" ) @@ -87,7 +88,6 @@ func (t *templateFuncster) partialCached(name string, context interface{}, varia func (t *templateFuncster) initFuncMap() { funcMap := template.FuncMap{ // Namespaces - "os": t.os.Namespace, "safe": t.safe.Namespace, //"time": t.time.Namespace, "transform": t.transform.Namespace, @@ -97,7 +97,6 @@ func (t *templateFuncster) initFuncMap() { "absLangURL": t.urls.AbsLangURL, "dateFormat": t.time.Format, "emojify": t.transform.Emojify, - "getenv": t.os.Getenv, "highlight": t.transform.Highlight, "htmlEscape": t.transform.HTMLEscape, "htmlUnescape": t.transform.HTMLUnescape, @@ -110,8 +109,6 @@ func (t *templateFuncster) initFuncMap() { "print": fmt.Sprint, "printf": fmt.Sprintf, "println": fmt.Sprintln, - "readDir": t.os.ReadDir, - "readFile": t.os.ReadFile, "ref": t.urls.Ref, "relURL": t.urls.RelURL, "relLangURL": t.urls.RelLangURL, diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go index e894d3286..206f2c8b3 100644 --- a/tpl/tplimpl/template_funcs_test.go +++ b/tpl/tplimpl/template_funcs_test.go @@ -69,7 +69,9 @@ func TestTemplateFuncsExamples(t *testing.T) { afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755) - d, err := deps.New(newDepsConfig(v)) + depsCfg := newDepsConfig(v) + depsCfg.Fs = fs + d, err := deps.New(depsCfg) require.NoError(t, err) var data struct { @@ -139,8 +141,6 @@ print: {{ print "works!" }} printf: {{ printf "%s!" "works" }} println: {{ println "works!" -}} plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }} -readDir: {{ range (readDir ".") }}{{ .Name }}{{ end }} -readFile: {{ readFile "README.txt" }} relLangURL: {{ "index.html" | relLangURL }} relURL 1: {{ "http://gohugo.io/" | relURL }} relURL 2: {{ "mystyle.css" | relURL }} @@ -174,8 +174,6 @@ print: works! printf: works! println: works! plainify: Hello world, gophers! -readDir: README.txt -readFile: Hugo Rocks! relLangURL: /hugo/en/index.html relURL 1: http://gohugo.io/ relURL 2: /hugo/mystyle.css |