hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 2bee4a157068edcd9a159ee848bec46c061d34b1
parent 5df85770fca6184e4230e9cb4fddeb841087563f
Author: bep <bjorn.erik.pedersen@gmail.com>
Date:   Thu,  5 Feb 2015 18:31:11 +0100

Replace 4 strings.Replace with 1 strings.Replacer

Consumes less memory, slightly faster.

Diffstat:
Mhelpers/content.go | 13+++++--------
Mhelpers/content_test.go | 2++
2 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/helpers/content.go b/helpers/content.go
@@ -69,18 +69,16 @@ var blackfridayExtensionMap = map[string]int{
 	"autoHeaderIds":          blackfriday.EXTENSION_AUTO_HEADER_IDS,
 }
 
+var stripHTMLReplacer = strings.NewReplacer("\n", " ", "</p>", "\n", "<br>", "\n", "<br />", "\n")
+
 // StripHTML accepts a string, strips out all HTML tags and returns it.
 func StripHTML(s string) string {
-	output := ""
 
 	// Shortcut strings with no tags in them
 	if !strings.ContainsAny(s, "<>") {
-		output = s
+		return s
 	} else {
-		s = strings.Replace(s, "\n", " ", -1)
-		s = strings.Replace(s, "</p>", "\n", -1)
-		s = strings.Replace(s, "<br>", "\n", -1)
-		s = strings.Replace(s, "<br />", "\n", -1) // <br /> is the xhtml line break tag
+		s = stripHTMLReplacer.Replace(s)
 
 		// Walk through the string removing all tags
 		b := new(bytes.Buffer)
@@ -97,9 +95,8 @@ func StripHTML(s string) string {
 				}
 			}
 		}
-		output = b.String()
+		return b.String()
 	}
-	return output
 }
 
 // StripEmptyNav strips out empty <nav> tags from content.
diff --git a/helpers/content_test.go b/helpers/content_test.go
@@ -14,6 +14,8 @@ func TestStripHTML(t *testing.T) {
 		{"<h1>strip h1 tag <h1>", "strip h1 tag "},
 		{"<p> strip p tag </p>", " strip p tag \n"},
 		{"</br> strip br<br>", " strip br\n"},
+		{"</br> strip br2<br />", " strip br2\n"},
+		{"This <strong>is</strong> a\nnewline", "This is a newline"},
 	}
 	for i, d := range data {
 		output := StripHTML(d.input)