tpl/urls: Make it a package that stands on its own
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 30 Apr 2017 21:33:14 +0000 (23:33 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 1 May 2017 13:13:41 +0000 (15:13 +0200)
 See #3042

tpl/tplimpl/templateFuncster.go
tpl/tplimpl/template_funcs.go
tpl/tplimpl/template_funcs_test.go
tpl/urls/init.go [new file with mode: 0644]

index b40de53074b0d2b0f246565fc7b09bae01ee19f3..c37d7f93789c357762ae3017111e400a35979736 100644 (file)
@@ -21,7 +21,6 @@ import (
 
        bp "github.com/spf13/hugo/bufferpool"
        "github.com/spf13/hugo/deps"
-       "github.com/spf13/hugo/tpl/urls"
 )
 
 // Some of the template funcs are'nt entirely stateless.
@@ -29,9 +28,6 @@ type templateFuncster struct {
        funcMap        template.FuncMap
        cachedPartials partialCache
 
-       // Namespaces
-       urls *urls.Namespace
-
        *deps.Deps
 }
 
@@ -39,9 +35,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
        return &templateFuncster{
                Deps:           deps,
                cachedPartials: partialCache{p: make(map[string]interface{})},
-
-               // Namespaces
-               urls: urls.New(deps),
        }
 }
 
index e5303a8d7a9b6e17609f8ceaaafdbed312ae725c..d1a2dd73a49e7e7859a2ba17ed5d9ff35c1f1bfb 100644 (file)
@@ -38,6 +38,7 @@ import (
        _ "github.com/spf13/hugo/tpl/strings"
        _ "github.com/spf13/hugo/tpl/time"
        _ "github.com/spf13/hugo/tpl/transform"
+       _ "github.com/spf13/hugo/tpl/urls"
 )
 
 // Get retrieves partial output from the cache based upon the partial name.
@@ -92,20 +93,12 @@ func (t *templateFuncster) initFuncMap() {
        funcMap := template.FuncMap{
                // Namespaces
                //"time":        t.time.Namespace,
-               "urls": t.urls.Namespace,
-
-               "absURL":        t.urls.AbsURL,
-               "absLangURL":    t.urls.AbsLangURL,
                "int":           func(v interface{}) (int, error) { return cast.ToIntE(v) },
                "partial":       t.partial,
                "partialCached": t.partialCached,
                "print":         fmt.Sprint,
                "printf":        fmt.Sprintf,
                "println":       fmt.Sprintln,
-               "ref":           t.urls.Ref,
-               "relURL":        t.urls.RelURL,
-               "relLangURL":    t.urls.RelLangURL,
-               "relref":        t.urls.RelRef,
                "string":        func(v interface{}) (string, error) { return cast.ToStringE(v) },
                "urlize":        t.PathSpec.URLize,
        }
index 5ee11e16a3b1759fd106cbc36f4d2649a566f5ad..186da511b8304b359361418d746bade2ee124663 100644 (file)
@@ -64,6 +64,8 @@ func TestTemplateFuncsExamples(t *testing.T) {
 
        v.Set("workingDir", workingDir)
        v.Set("multilingual", true)
+       v.Set("baseURL", "http://mysite.com/hugo/")
+       v.Set("CurrentContentLanguage", helpers.NewLanguage("en", v))
 
        fs := hugofs.NewMem(v)
 
@@ -122,10 +124,7 @@ func TestFuncsInTemplate(t *testing.T) {
        // TODO(bep): docs: fix title example
        // TODO(bep) namespace remove when done
        in :=
-               `absLangURL: {{ "index.html" | absLangURL }}
-absURL: {{ "http://gohugo.io/" | absURL }}
-absURL: {{ "mystyle.css" | absURL }}
-absURL: {{ 42 | absURL }}
+               `
 crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
 dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
 htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
@@ -138,18 +137,11 @@ htmlUnescape 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;catha
 print: {{ print "works!" }}
 printf: {{ printf "%s!" "works" }}
 println: {{ println "works!" -}}
-relLangURL: {{ "index.html" | relLangURL }}
-relURL 1: {{ "http://gohugo.io/" | relURL }}
-relURL 2: {{ "mystyle.css" | relURL }}
-relURL 3: {{ mul 2 21 | relURL }}
 strings.TrimPrefix: {{ strings.TrimPrefix "Goodbye,, world!" "Goodbye," }}
 urlize: {{ "Bat Man" | urlize }}
 `
 
-       expected := `absLangURL: http://mysite.com/hugo/en/index.html
-absURL: http://gohugo.io/
-absURL: http://mysite.com/hugo/mystyle.css
-absURL: http://mysite.com/hugo/42
+       expected := `
 crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
 dateFormat: Wednesday, Jan 21, 2015
 htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
@@ -162,10 +154,6 @@ htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
 print: works!
 printf: works!
 println: works!
-relLangURL: /hugo/en/index.html
-relURL 1: http://gohugo.io/
-relURL 2: /hugo/mystyle.css
-relURL 3: /hugo/42
 strings.TrimPrefix: , world!
 urlize: bat-man
 `
diff --git a/tpl/urls/init.go b/tpl/urls/init.go
new file mode 100644 (file)
index 0000000..a687704
--- /dev/null
@@ -0,0 +1,55 @@
+// 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 urls
+
+import (
+       "github.com/spf13/hugo/deps"
+       "github.com/spf13/hugo/tpl/internal"
+)
+
+const name = "urls"
+
+func init() {
+       f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
+               ctx := New(d)
+
+               examples := [][2]string{
+                       {`{{ "index.html" | absLangURL }}`, `http://mysite.com/hugo/en/index.html`},
+                       {`{{ "http://gohugo.io/" | absURL }}`, `http://gohugo.io/`},
+                       {`{{ "mystyle.css" | absURL }}`, `http://mysite.com/hugo/mystyle.css`},
+                       {`{{ 42 | absURL }}`, `http://mysite.com/hugo/42`},
+                       {`{{ "index.html" | relLangURL }}`, `/hugo/en/index.html`},
+                       {`{{ "http://gohugo.io/" | relURL }}`, `http://gohugo.io/`},
+                       {`{{ "mystyle.css" | relURL }}`, `/hugo/mystyle.css`},
+                       {`{{ mul 2 21 | relURL }}`, `/hugo/42`},
+               }
+
+               return &internal.TemplateFuncsNamespace{
+                       Name:    name,
+                       Context: func() interface{} { return ctx },
+                       Aliases: map[string]interface{}{
+                               "absURL":     ctx.AbsURL,
+                               "absLangURL": ctx.AbsLangURL,
+                               "ref":        ctx.Ref,
+                               "relURL":     ctx.RelURL,
+                               "relLangURL": ctx.RelLangURL,
+                               "relref":     ctx.RelRef,
+                       },
+                       Examples: examples,
+               }
+
+       }
+
+       internal.AddTemplateFuncsNamespace(f)
+}