From b958c0c1091574d0bd8118045fdf421c7d946893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 30 Apr 2017 22:52:47 +0200 Subject: [PATCH] tpl/os: Make it a package that stands on its own See #3042 --- tpl/os/init.go | 46 ++++++++++++++++++++++++++++++ tpl/tplimpl/templateFuncster.go | 3 -- tpl/tplimpl/template_funcs.go | 5 +--- tpl/tplimpl/template_funcs_test.go | 8 ++---- 4 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 tpl/os/init.go 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 world, 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