ref: 6033abe1e7f69a8539137cc2659624866e2e1183
parent: 14e93de8a1974ffc54e8b3843fcd89a7ae8ca4d5
author: Austin Ziegler <[email protected]>
date: Tue Dec 9 15:37:51 EST 2014
Add a chomp function. - Mostly useful in pipelines.
--- a/docs/content/templates/functions.md
+++ b/docs/content/templates/functions.md
@@ -252,6 +252,11 @@
e.g. `{{title "BatMan"}}` → "Batman"
+### chomp
+Removes any trailing newline characters. Useful in a pipeline to remove newlines added by other processing (including `markdownify`).
+
+e.g., `{{chomp "<p>Blockhead</p>\n"` → `"<p>Blockhead</p>"`
+
### highlight
Take a string of code and a language, uses Pygments to return the syntax
highlighted code in HTML. Used in the [highlight
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -29,6 +29,7 @@
"os"
"path/filepath"
"reflect"
+ "regexp"
"sort"
"strconv"
"strings"
@@ -37,6 +38,7 @@
var localTemplates *template.Template
var tmpl Template
var funcMap template.FuncMap
+var chompRegexp *regexp.Regexp
type Template interface {
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
@@ -667,6 +669,15 @@
return refPage(page, ref, "RelRef")
}
+func Chomp(text interface{}) (string, error) {
+ s, err := cast.ToStringE(text)
+ if err != nil {
+ return "", err
+ }
+
+ return chompRegexp.ReplaceAllString(s, ""), nil
+}
+
func SafeHtml(text string) template.HTML {
return template.HTML(text)
}
@@ -1022,5 +1033,8 @@
"partial": Partial,
"ref": Ref,
"relref": RelRef,
+ "chomp": Chomp,
}
+
+ chompRegexp = regexp.MustCompile("[\r\n]+$")
}