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

See #3042
This commit is contained in:
Bjørn Erik Pedersen 2017-04-30 22:52:47 +02:00
parent fc77b6303c
commit b958c0c109
4 changed files with 50 additions and 12 deletions

46
tpl/os/init.go Normal file
View File

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

View File

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

View File

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

View File

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