From: bep
Date: Thu, 5 Feb 2015 17:31:11 +0000 (+0100)
Subject: Replace 4 strings.Replace with 1 strings.Replacer
X-Git-Tag: v0.13~66
X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=2bee4a15;p=brevno-suite%2Fhugo
Replace 4 strings.Replace with 1 strings.Replacer
Consumes less memory, slightly faster.
---
diff --git a/helpers/content.go b/helpers/content.go
index 3b33b6b1..fe5f26e5 100644
--- 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", " ", "
", "\n", " ", "\n", " ", "\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, "", "\n", -1)
- s = strings.Replace(s, " ", "\n", -1)
- s = strings.Replace(s, " ", "\n", -1) // 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