Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorCathal Garvey <cathalgarvey@cathalgarvey.me>2016-07-19 01:14:05 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-07-19 01:14:05 +0300
commit20555b1630765d68c4b6d6c90406483195935faf (patch)
tree21552fd84a3426b03963c871016c4ae35f434940 /tpl
parent7dd5cd675a4a62bfcd6b64b86b9514d7b9947b64 (diff)
Add `htmlEscape` and `htmlUnescape` template functions
These functions allow trivial escaping and unescaping of HTML entities, and make it far easier to compose other functions for the creation of parameterised URLs.
Diffstat (limited to 'tpl')
-rw-r--r--tpl/template_funcs.go18
-rw-r--r--tpl/template_funcs_test.go14
2 files changed, 32 insertions, 0 deletions
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go
index c12218468..b04b36810 100644
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -1775,6 +1775,22 @@ func querify(params ...interface{}) (string, error) {
return qs.Encode(), nil
}
+func htmlEscape(in interface{}) (string, error) {
+ conv, err := cast.ToStringE(in)
+ if err != nil {
+ return "", err
+ }
+ return html.EscapeString(conv), nil
+}
+
+func htmlUnescape(in interface{}) (string, error) {
+ conv, err := cast.ToStringE(in)
+ if err != nil {
+ return "", err
+ }
+ return html.UnescapeString(conv), nil
+}
+
func init() {
funcMap = template.FuncMap{
"absURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
@@ -1803,6 +1819,8 @@ func init() {
"gt": gt,
"hasPrefix": func(a, b string) bool { return strings.HasPrefix(a, b) },
"highlight": highlight,
+ "htmlEscape": htmlEscape,
+ "htmlUnescape": htmlUnescape,
"humanize": humanize,
"in": in,
"index": index,
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
index eeffd46bd..2d8f753bd 100644
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -93,6 +93,13 @@ eq: {{ if eq .Section "blog" }}current{{ end }}
findRE: {{ findRE "[G|g]o" "Hugo is a static side generator written in Go." 1 }}
hasPrefix 1: {{ hasPrefix "Hugo" "Hu" }}
hasPrefix 2: {{ hasPrefix "Hugo" "Fu" }}
+htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
+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 2: {{"Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;" | htmlUnescape | htmlUnescape | safeHTML}}
+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 5: {{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlEscape | safeHTML }}
humanize 1: {{ humanize "my-first-post" }}
humanize 2: {{ humanize "myCamelPost" }}
humanize 3: {{ humanize "52" }}
@@ -149,6 +156,13 @@ eq: current
findRE: [go]
hasPrefix 1: true
hasPrefix 2: false
+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;
+htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
+htmlUnescape 2: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
+htmlUnescape 3: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
+htmlUnescape 4: Cathal Garvey & The Sunshine Band <cathal@foo.bar>
+htmlUnescape 5: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
humanize 1: My first post
humanize 2: My camel post
humanize 3: 52nd