diff options
author | Austin Ziegler <austin@zieglers.ca> | 2014-12-10 04:37:51 +0300 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-01-02 13:50:22 +0300 |
commit | 6033abe1e7f69a8539137cc2659624866e2e1183 (patch) | |
tree | 5904f2f82181567ecfba354e11c847213b08e89a /tpl | |
parent | 14e93de8a1974ffc54e8b3843fcd89a7ae8ca4d5 (diff) |
Add a chomp function.
- Mostly useful in pipelines.
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/template.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/tpl/template.go b/tpl/template.go index 197703a73..ef0096cef 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -29,6 +29,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "sort" "strconv" "strings" @@ -37,6 +38,7 @@ import ( 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 @@ func RelRef(page interface{}, ref string) template.HTML { 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 @@ func init() { "partial": Partial, "ref": Ref, "relref": RelRef, + "chomp": Chomp, } + + chompRegexp = regexp.MustCompile("[\r\n]+$") } |