tpl: Make chomp return template.HTML

This commit is contained in:
Bjørn Erik Pedersen 2016-03-03 19:45:23 +01:00
parent daaf4eb330
commit f223f17c76
2 changed files with 5 additions and 5 deletions

View file

@ -1196,13 +1196,13 @@ func relRef(page interface{}, ref string) template.HTML {
} }
// chomp removes trailing newline characters from a string. // chomp removes trailing newline characters from a string.
func chomp(text interface{}) (string, error) { func chomp(text interface{}) (template.HTML, error) {
s, err := cast.ToStringE(text) s, err := cast.ToStringE(text)
if err != nil { if err != nil {
return "", err return "", err
} }
return strings.TrimRight(s, "\r\n"), nil return template.HTML(strings.TrimRight(s, "\r\n")), nil
} }
// trim leading/trailing characters defined by b from a // trim leading/trailing characters defined by b from a

View file

@ -63,10 +63,9 @@ func TestFuncsInTemplate(t *testing.T) {
defer viper.Reset() defer viper.Reset()
// Add the examples from the docs: As a smoke test and to make sure the examples work. // Add the examples from the docs: As a smoke test and to make sure the examples work.
// TODO(bep): Look at the use of safeHTML below; these should maybe return template.HTML
// TODO(bep): docs: fix title example // TODO(bep): docs: fix title example
in := in :=
`chomp: {{chomp "<p>Blockhead</p>\n" | safeHTML }} `chomp: {{chomp "<p>Blockhead</p>\n" }}
dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }} dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
lower: {{lower "BatMan"}} lower: {{lower "BatMan"}}
markdownify: {{ .Title | markdownify}} markdownify: {{ .Title | markdownify}}
@ -1643,7 +1642,8 @@ func TestChomp(t *testing.T) {
"\r", "\r\r", "\r", "\r\r",
"\r\n", "\r\n\r\n", "\r\n", "\r\n\r\n",
} { } {
chomped, _ := chomp(base + item) c, _ := chomp(base + item)
chomped := string(c)
if chomped != base { if chomped != base {
t.Errorf("[%d] Chomp failed, got '%v'", i, chomped) t.Errorf("[%d] Chomp failed, got '%v'", i, chomped)