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

See #3042
This commit is contained in:
Bjørn Erik Pedersen 2017-04-30 23:16:54 +02:00
parent ee5aa84f2a
commit 55f90a3a0d
4 changed files with 54 additions and 19 deletions

View file

@ -21,7 +21,6 @@ import (
bp "github.com/spf13/hugo/bufferpool" bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/deps" "github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/transform"
"github.com/spf13/hugo/tpl/urls" "github.com/spf13/hugo/tpl/urls"
) )
@ -31,8 +30,7 @@ type templateFuncster struct {
cachedPartials partialCache cachedPartials partialCache
// Namespaces // Namespaces
transform *transform.Namespace urls *urls.Namespace
urls *urls.Namespace
*deps.Deps *deps.Deps
} }
@ -43,8 +41,7 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
cachedPartials: partialCache{p: make(map[string]interface{})}, cachedPartials: partialCache{p: make(map[string]interface{})},
// Namespaces // Namespaces
transform: transform.New(deps), urls: urls.New(deps),
urls: urls.New(deps),
} }
} }

View file

@ -37,6 +37,7 @@ import (
_ "github.com/spf13/hugo/tpl/safe" _ "github.com/spf13/hugo/tpl/safe"
_ "github.com/spf13/hugo/tpl/strings" _ "github.com/spf13/hugo/tpl/strings"
_ "github.com/spf13/hugo/tpl/time" _ "github.com/spf13/hugo/tpl/time"
_ "github.com/spf13/hugo/tpl/transform"
) )
// Get retrieves partial output from the cache based upon the partial name. // Get retrieves partial output from the cache based upon the partial name.
@ -91,20 +92,13 @@ func (t *templateFuncster) initFuncMap() {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
// Namespaces // Namespaces
//"time": t.time.Namespace, //"time": t.time.Namespace,
"transform": t.transform.Namespace, "urls": t.urls.Namespace,
"urls": t.urls.Namespace,
"absURL": t.urls.AbsURL, "absURL": t.urls.AbsURL,
"absLangURL": t.urls.AbsLangURL, "absLangURL": t.urls.AbsLangURL,
"emojify": t.transform.Emojify,
"highlight": t.transform.Highlight,
"htmlEscape": t.transform.HTMLEscape,
"htmlUnescape": t.transform.HTMLUnescape,
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) }, "int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
"markdownify": t.transform.Markdownify,
"partial": t.partial, "partial": t.partial,
"partialCached": t.partialCached, "partialCached": t.partialCached,
"plainify": t.transform.Plainify,
"print": fmt.Sprint, "print": fmt.Sprint,
"printf": fmt.Sprintf, "printf": fmt.Sprintf,
"println": fmt.Sprintln, "println": fmt.Sprintln,

View file

@ -128,7 +128,6 @@ absURL: {{ "mystyle.css" | absURL }}
absURL: {{ 42 | absURL }} absURL: {{ 42 | absURL }}
crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }} crypto.MD5: {{ crypto.MD5 "Hello world, gophers!" }}
dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }} dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
emojify: {{ "I :heart: Hugo" | emojify }}
htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}} htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}} htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}
htmlUnescape 1: {{htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | safeHTML}} htmlUnescape 1: {{htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | safeHTML}}
@ -136,11 +135,9 @@ htmlUnescape 2: {{"Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.
htmlUnescape 3: {{"Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;" | htmlUnescape | htmlUnescape }} htmlUnescape 3: {{"Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;" | htmlUnescape | htmlUnescape }}
htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }} htmlUnescape 4: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }}
htmlUnescape 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlEscape | safeHTML }} htmlUnescape 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlEscape | safeHTML }}
markdownify: {{ .Title | markdownify}}
print: {{ print "works!" }} print: {{ print "works!" }}
printf: {{ printf "%s!" "works" }} printf: {{ printf "%s!" "works" }}
println: {{ println "works!" -}} println: {{ println "works!" -}}
plainify: {{ plainify "Hello <strong>world</strong>, gophers!" }}
relLangURL: {{ "index.html" | relLangURL }} relLangURL: {{ "index.html" | relLangURL }}
relURL 1: {{ "http://gohugo.io/" | relURL }} relURL 1: {{ "http://gohugo.io/" | relURL }}
relURL 2: {{ "mystyle.css" | relURL }} relURL 2: {{ "mystyle.css" | relURL }}
@ -155,7 +152,6 @@ absURL: http://mysite.com/hugo/mystyle.css
absURL: http://mysite.com/hugo/42 absURL: http://mysite.com/hugo/42
crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b crypto.MD5: b3029f756f98f79e7f1b7f1d1f0dd53b
dateFormat: Wednesday, Jan 21, 2015 dateFormat: Wednesday, Jan 21, 2015
emojify: I Hugo
htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt; htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
htmlEscape 2: Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt; htmlEscape 2: Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;
htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar> htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
@ -163,11 +159,9 @@ htmlUnescape 2: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
htmlUnescape 3: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt; htmlUnescape 3: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar> htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt; htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
markdownify: <strong>BatMan</strong>
print: works! print: works!
printf: works! printf: works!
println: works! println: works!
plainify: Hello world, gophers!
relLangURL: /hugo/en/index.html relLangURL: /hugo/en/index.html
relURL 1: http://gohugo.io/ relURL 1: http://gohugo.io/
relURL 2: /hugo/mystyle.css relURL 2: /hugo/mystyle.css

50
tpl/transform/init.go Normal file
View file

@ -0,0 +1,50 @@
// 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 transform
import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/internal"
)
const name = "transform"
func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New(d)
examples := [][2]string{
{`{{ "I :heart: Hugo" | emojify }}`, `I ❤️ Hugo`},
{`{{ .Title | markdownify}}`, `<strong>BatMan</strong>`},
{`{{ plainify "Hello <strong>world</strong>, gophers!" }}`, `Hello world, gophers!`},
}
return &internal.TemplateFuncsNamespace{
Name: name,
Context: func() interface{} { return ctx },
Aliases: map[string]interface{}{
"emojify": ctx.Emojify,
"highlight": ctx.Highlight,
"htmlEscape": ctx.HTMLEscape,
"htmlUnescape": ctx.HTMLUnescape,
"markdownify": ctx.Markdownify,
"plainify": ctx.Plainify,
},
Examples: examples,
}
}
internal.AddTemplateFuncsNamespace(f)
}