shithub: hugo

Download patch

ref: b958c0c1091574d0bd8118045fdf421c7d946893
parent: fc77b6303c8aeda6362d7e2fc5d0fe52067c1a8d
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 30 18:52:47 EDT 2017

tpl/os: Make it a package that stands on its own

See #3042

--- /dev/null
+++ b/tpl/os/init.go
@@ -1,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)
+}
--- a/tpl/tplimpl/templateFuncster.go
+++ b/tpl/tplimpl/templateFuncster.go
@@ -21,7 +21,6 @@
 
 	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 @@
 	cachedPartials partialCache
 
 	// Namespaces
-	os        *os.Namespace
 	safe      *safe.Namespace
 	time      *time.Namespace
 	transform *transform.Namespace
@@ -49,7 +47,6 @@
 		cachedPartials: partialCache{p: make(map[string]interface{})},
 
 		// Namespaces
-		os:        os.New(deps),
 		safe:      safe.New(),
 		time:      time.New(),
 		transform: transform.New(deps),
--- a/tpl/tplimpl/template_funcs.go
+++ b/tpl/tplimpl/template_funcs.go
@@ -33,6 +33,7 @@
 	_ "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) 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 @@
 		"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 @@
 		"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,
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -69,7 +69,9 @@
 
 	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 @@
 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 @@
 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