shithub: hugo

Download patch

ref: 077d726b51368bb5affb7ce5644e5b45a0f9ea42
parent: 99aee30410f6908ac9c11a463eb45d1f28f5ca94
author: bep <[email protected]>
date: Tue Jan 20 07:41:08 EST 2015

Replace regexp based Chomp with builtin TrimRight

--- a/tpl/template.go
+++ b/tpl/template.go
@@ -29,7 +29,6 @@
 	"os"
 	"path/filepath"
 	"reflect"
-	"regexp"
 	"sort"
 	"strconv"
 	"strings"
@@ -38,7 +37,6 @@
 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
@@ -877,7 +875,7 @@
 		return "", err
 	}
 
-	return chompRegexp.ReplaceAllString(s, ""), nil
+	return strings.TrimRight(s, "\r\n"), nil
 }
 
 // Trim leading/trailing characters defined by b from a
@@ -1245,44 +1243,43 @@
 
 func init() {
 	funcMap = template.FuncMap{
-		"urlize":       helpers.Urlize,
-		"sanitizeurl":  helpers.SanitizeUrl,
-		"eq":           Eq,
-		"ne":           Ne,
-		"gt":           Gt,
-		"ge":           Ge,
-		"lt":           Lt,
-		"le":           Le,
-		"in":           In,
-		"intersect":    Intersect,
-		"isset":        IsSet,
-		"echoParam":    ReturnWhenSet,
-		"safeHtml":     SafeHtml,
-		"safeCss":      SafeCss,
-		"safeUrl":      SafeUrl,
-		"markdownify":  Markdownify,
-		"first":        First,
-		"where":        Where,
-		"delimit":      Delimit,
-		"sort":         Sort,
-		"highlight":    Highlight,
-		"add":          func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '+') },
-		"sub":          func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '-') },
-		"div":          func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '/') },
-		"mod":          Mod,
-		"mul":          func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') },
-		"modBool":      ModBool,
-		"lower":        func(a string) string { return strings.ToLower(a) },
-		"upper":        func(a string) string { return strings.ToUpper(a) },
-		"title":        func(a string) string { return strings.Title(a) },
-		"partial":      Partial,
-		"ref":          Ref,
-		"relref":       RelRef,
-		"apply":        Apply,
-		"chomp":        Chomp,
-		"replace":      Replace,
-		"trim":         Trim,
+		"urlize":      helpers.Urlize,
+		"sanitizeurl": helpers.SanitizeUrl,
+		"eq":          Eq,
+		"ne":          Ne,
+		"gt":          Gt,
+		"ge":          Ge,
+		"lt":          Lt,
+		"le":          Le,
+		"in":          In,
+		"intersect":   Intersect,
+		"isset":       IsSet,
+		"echoParam":   ReturnWhenSet,
+		"safeHtml":    SafeHtml,
+		"safeCss":     SafeCss,
+		"safeUrl":     SafeUrl,
+		"markdownify": Markdownify,
+		"first":       First,
+		"where":       Where,
+		"delimit":     Delimit,
+		"sort":        Sort,
+		"highlight":   Highlight,
+		"add":         func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '+') },
+		"sub":         func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '-') },
+		"div":         func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '/') },
+		"mod":         Mod,
+		"mul":         func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') },
+		"modBool":     ModBool,
+		"lower":       func(a string) string { return strings.ToLower(a) },
+		"upper":       func(a string) string { return strings.ToUpper(a) },
+		"title":       func(a string) string { return strings.Title(a) },
+		"partial":     Partial,
+		"ref":         Ref,
+		"relref":      RelRef,
+		"apply":       Apply,
+		"chomp":       Chomp,
+		"replace":     Replace,
+		"trim":        Trim,
 	}
 
-	chompRegexp = regexp.MustCompile("[\r\n]+$")
 }
--- a/tpl/template_test.go
+++ b/tpl/template_test.go
@@ -835,9 +835,9 @@
 func TestChomp(t *testing.T) {
 	base := "\n This is\na story "
 	for i, item := range []string{
-		"\n",
-		"\r",
-		"\r\n",
+		"\n", "\n\n",
+		"\r", "\r\r",
+		"\r\n", "\r\n\r\n",
 	} {
 		chomped, _ := Chomp(base + item)