Skip to content

Commit

Permalink
tpl: Fix the remaining template funcs namespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 1, 2017
1 parent b55f471 commit 7ded16f
Show file tree
Hide file tree
Showing 41 changed files with 396 additions and 485 deletions.
35 changes: 35 additions & 0 deletions tpl/cast/cast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 cast

import (
_cast "github.com/spf13/cast"
)

// New returns a new instance of the cast-namespaced template functions.
func New() *Namespace {
return &Namespace{}
}

// Namespace provides template functions for the "cast" namespace.
type Namespace struct {
}

func (ns *Namespace) ToInt(v interface{}) (int, error) {
return _cast.ToIntE(v)
}

func (ns *Namespace) ToString(v interface{}) (string, error) {
return _cast.ToStringE(v)
}
45 changes: 45 additions & 0 deletions tpl/cast/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 cast

import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/internal"
)

const name = "cast"

func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New()

examples := [][2]string{
{`{{ "1234" | int | printf "%T" }}`, `int`},
{`{{ 1234 | string | printf "%T" }}`, `string`},
}

return &internal.TemplateFuncsNamespace{
Name: name,
Context: func() interface{} { return ctx },
Aliases: map[string]interface{}{
"int": ctx.ToInt,
"string": ctx.ToString,
},
Examples: examples,
}

}

internal.AddTemplateFuncsNamespace(f)
}
3 changes: 0 additions & 3 deletions tpl/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ type Namespace struct {
deps *deps.Deps
}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }

// After returns all the items after the first N in a rangeable list.
func (ns *Namespace) After(index interface{}, seq interface{}) (interface{}, error) {
if index == nil || seq == nil {
Expand Down
8 changes: 0 additions & 8 deletions tpl/collections/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ import (

type tstNoStringer struct{}

func TestNamespace(t *testing.T) {
t.Parallel()

ns := New(&deps.Deps{})

assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}

func TestAfter(t *testing.T) {
t.Parallel()

Expand Down
20 changes: 10 additions & 10 deletions tpl/collections/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ func init() {
ctx := New(d)

examples := [][2]string{
{`delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}`, `delimit: A, B and C`},
{`echoParam: {{ echoParam .Params "langCode" }}`, `echoParam: en`},
{`in: {{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `in: Substring found!`},
{`{{ delimit (slice "A" "B" "C") ", " " and " }}`, `A, B and C`},
{`{{ echoParam .Params "langCode" }}`, `en`},
{`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
{
`querify 1: {{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
`querify 1: bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`},
`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`},
{
`querify 2: <a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
`querify 2: <a href="https://www.google.com?page=3&amp;q=test">Search</a>`},
{`sort: {{ slice "B" "C" "A" | sort }}`, `sort: [A B C]`},
{`seq: {{ seq 3 }}`, `seq: [1 2 3]`},
{`union: {{ union (slice 1 2 3) (slice 3 4 5) }}`, `union: [1 2 3 4 5]`},
`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
`<a href="https://www.google.com?page=3&amp;q=test">Search</a>`},
{`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
{`{{ seq 3 }}`, `[1 2 3]`},
{`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
}

return &internal.TemplateFuncsNamespace{
Expand Down
4 changes: 3 additions & 1 deletion tpl/compare/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func init() {
ctx := New()

examples := [][2]string{
{`eq: {{ if eq .Section "blog" }}current{{ end }}`, `eq: current`},
{`{{ if eq .Section "blog" }}current{{ end }}`, `current`},
{`{{ "Hugo Rocks!" | default "Hugo Rules!" }}`, `Hugo Rocks!`},
{`{{ "" | default "Hugo Rules!" }}`, `Hugo Rules!`},
}

return &internal.TemplateFuncsNamespace{
Expand Down
3 changes: 0 additions & 3 deletions tpl/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ func New() *Namespace {
// Namespace provides template functions for the "crypto" namespace.
type Namespace struct{}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }

// MD5 hashes the given input and returns its MD5 checksum.
func (ns *Namespace) MD5(in interface{}) (string, error) {
conv, err := cast.ToStringE(in)
Expand Down
8 changes: 0 additions & 8 deletions tpl/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestNamespace(t *testing.T) {
t.Parallel()

ns := New()

assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}

func TestMD5(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions tpl/crypto/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func init() {

examples := [][2]string{
{`{{ md5 "Hello world, gophers!" }}`, `b3029f756f98f79e7f1b7f1d1f0dd53b`},
{`{{ crypto.MD5 "Hello world, gophers!" }}`, `b3029f756f98f79e7f1b7f1d1f0dd53b`},
{`{{ sha1 "Hello world, gophers!" }}`, `c8b5b0e33d408246e30f53e32b8f7627a7a649d4`},
{`{{ sha256 "Hello world, gophers!" }}`, `6ec43b78da9669f50e4e422575c54bf87536954ccd58280219c393f2ce352b46`},
}
Expand Down
3 changes: 0 additions & 3 deletions tpl/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ func New(deps *deps.Deps) *Namespace {
type Namespace struct {
deps *deps.Deps
}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }
3 changes: 0 additions & 3 deletions tpl/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ func New() *Namespace {
// Namespace provides template functions for the "encoding" namespace.
type Namespace struct{}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }

// Base64Decode returns the base64 decoding of the given content.
func (ns *Namespace) Base64Decode(content interface{}) (string, error) {
conv, err := cast.ToStringE(content)
Expand Down
8 changes: 0 additions & 8 deletions tpl/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ import (

type tstNoStringer struct{}

func TestNamespace(t *testing.T) {
t.Parallel()

ns := New()

assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}

func TestBase64Decode(t *testing.T) {
t.Parallel()

Expand Down
39 changes: 39 additions & 0 deletions tpl/fmt/fmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 fmt

import (
_fmt "fmt"
)

// New returns a new instance of the fmt-namespaced template functions.
func New() *Namespace {
return &Namespace{}
}

// Namespace provides template functions for the "fmt" namespace.
type Namespace struct {
}

func (ns *Namespace) Print(a ...interface{}) (n int, err error) {
return _fmt.Print(a...)
}

func (ns *Namespace) Printf(format string, a ...interface{}) (n int, err error) {
return _fmt.Printf(format, a...)
}

func (ns *Namespace) Println(a ...interface{}) (n int, err error) {
return _fmt.Println(a...)
}
43 changes: 43 additions & 0 deletions tpl/fmt/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 fmt

import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/internal"
)

const name = "fmt"

func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New()

examples := [][2]string{
{`{{ print "works!" }}`, `works!`},
{`{{ printf "%s!" "works" }}`, `works!`},
{`{{ println "works!" }}`, "works!\n"},
}

return &internal.TemplateFuncsNamespace{
Name: name,
Context: func() interface{} { return ctx },
Aliases: map[string]interface{}{},
Examples: examples,
}

}

internal.AddTemplateFuncsNamespace(f)
}
3 changes: 0 additions & 3 deletions tpl/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ type Namespace struct {
deps *deps.Deps
}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }

// Config returns the image.Config for the specified path relative to the
// working directory.
func (ns *Namespace) Config(path interface{}) (image.Config, error) {
Expand Down
11 changes: 0 additions & 11 deletions tpl/images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ var configTests = []struct {
{path: "", expect: false},
}

func TestNamespace(t *testing.T) {
t.Parallel()

v := viper.New()
v.Set("workingDir", "/a/b")

ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})

assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}

func TestNSConfig(t *testing.T) {
t.Parallel()

Expand Down
3 changes: 0 additions & 3 deletions tpl/inflect/inflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ func New() *Namespace {
// Namespace provides template functions for the "inflect" namespace.
type Namespace struct{}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }

// Humanize returns the humanized form of a single parameter.
//
// If the parameter is either an integer or a string containing an integer
Expand Down
8 changes: 0 additions & 8 deletions tpl/inflect/inflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestNamespace(t *testing.T) {
t.Parallel()

ns := New()

assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}

func TestInflect(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 0 additions & 4 deletions tpl/lang/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type Namespace struct {
deps *deps.Deps
}

// 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 ...
func (ns *Namespace) Translate(id interface{}, args ...interface{}) (string, error) {
sid, err := cast.ToStringE(id)
Expand Down
3 changes: 0 additions & 3 deletions tpl/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func New() *Namespace {
// Namespace provides template functions for the "math" namespace.
type Namespace struct{}

// Namespace returns a pointer to the current namespace instance.
func (ns *Namespace) Namespace() *Namespace { return ns }

func (ns *Namespace) Add(a, b interface{}) (interface{}, error) {
return DoArithmetic(a, b, '+')
}
Expand Down
8 changes: 0 additions & 8 deletions tpl/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestNamespace(t *testing.T) {
t.Parallel()

ns := New()

assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}

func TestBasicNSArithmetic(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion tpl/os/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
ctx := New(d)

examples := [][2]string{
{`{{ range (readDir ".") }}{{ .Name }}{{ end }}`, `README.txt`},
{`{{ range (readDir ".") }}{{ .Name }}{{ end }}`, "README.txt"},
{`{{ readFile "README.txt" }}`, `Hugo Rocks!`},
}

Expand Down
Loading

0 comments on commit 7ded16f

Please sign in to comment.