shithub: hugo

Download patch

ref: f8704c1bf23d22530ff417e0f48ee487a167a0f7
parent: 11a19e0760e55c31a8ba11eefe50c2b2b6520257
author: bep <[email protected]>
date: Wed Feb 4 19:38:50 EST 2015

Avoid calling strings.Fields multiple times with same content

This should be a relief for big sites.

--- a/helpers/content.go
+++ b/helpers/content.go
@@ -275,8 +275,7 @@
 
 // TruncateWordsToWholeSentence takes content and an int
 // and returns entire sentences from content, delimited by the int.
-func TruncateWordsToWholeSentence(s string, max int) string {
-	words := strings.Fields(s)
+func TruncateWordsToWholeSentence(words []string, max int) string {
 	if max > len(words) {
 		return strings.Join(words, " ")
 	}
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -65,6 +65,8 @@
 	rawContent          []byte
 	contentShortCodes   map[string]string
 	plain               string // TODO should be []byte
+	plainWords          []string
+	plainInit           sync.Once
 	renderingConfig     *helpers.Blackfriday
 	renderingConfigInit sync.Once
 	PageMeta
@@ -97,12 +99,22 @@
 type Pages []*Page
 
 func (p *Page) Plain() string {
-	if len(p.plain) == 0 {
-		p.plain = helpers.StripHTML(string(p.Content))
-	}
+	p.initPlain()
 	return p.plain
 }
 
+func (p *Page) PlainWords() []string {
+	p.initPlain()
+	return p.plainWords
+}
+
+func (p *Page) initPlain() {
+	p.plainInit.Do(func() {
+		p.plain = helpers.StripHTML(string(p.Content))
+		p.plainWords = strings.Fields(p.plain)
+	})
+}
+
 func (p *Page) IsNode() bool {
 	return false
 }
@@ -178,9 +190,11 @@
 	} else {
 		// If hugo defines split:
 		// render, strip html, then split
-		plain := strings.Join(strings.Fields(p.Plain()), " ")
-		p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(plain, helpers.SummaryLength)))
-		p.Truncated = len(p.Summary) != len(plain)
+		p.Summary = helpers.BytesToHTML([]byte(helpers.TruncateWordsToWholeSentence(p.PlainWords(), helpers.SummaryLength)))
+
+		// todo bep - check if the Plain() can be trimmed earlier
+		p.Truncated = len(p.Summary) != len(strings.Trim(p.Plain(), "\n\r "))
+
 	}
 }
 
@@ -322,7 +336,7 @@
 }
 
 func (p *Page) analyzePage() {
-	p.WordCount = helpers.TotalWords(p.Plain())
+	p.WordCount = len(p.PlainWords())
 	p.FuzzyWordCount = int((p.WordCount+100)/100) * 100
 	p.ReadingTime = int((p.WordCount + 212) / 213)
 }