summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-04-27 10:17:01 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-04-29 10:58:58 +0200
commit001a28c2f9ed121b33df18fe792859ab378784c6 (patch)
treec6ba82a96cbf697b6ec99a220c0e477529b10f51
parent391f59f996bee265ac4448103bd2739d4f397439 (diff)
Fix .WordCount, .FuzzyWordCount, .ReadingTime when summary marker is set
This bug was introduced in Hugo 0.40. It is when you use the `<!--more-->` summary marker. Note that this affects the word stats only. The related `PlainWords`, `Plain`, `Content` all return correct values. Fixes #4675 Fixes #4682
-rw-r--r--helpers/content.go5
-rw-r--r--hugolib/page.go19
-rw-r--r--hugolib/page_test.go95
3 files changed, 114 insertions, 5 deletions
diff --git a/helpers/content.go b/helpers/content.go
index f12a55ba8..d42995519 100644
--- a/helpers/content.go
+++ b/helpers/content.go
@@ -493,7 +493,10 @@ func totalWordsOld(s string) int {
}
// TruncateWordsByRune truncates words by runes.
-func (c *ContentSpec) TruncateWordsByRune(words []string) (string, bool) {
+func (c *ContentSpec) TruncateWordsByRune(in []string) (string, bool) {
+ words := make([]string, len(in))
+ copy(words, in)
+
count := 0
for index, word := range words {
if count >= c.summaryLength {
diff --git a/hugolib/page.go b/hugolib/page.go
index 9d25cd49a..2e008ece9 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -982,22 +982,33 @@ func (p *Page) ReadFrom(buf io.Reader) (int64, error) {
}
func (p *Page) WordCount() int {
- p.analyzePage()
+ p.initContentPlainAndMeta()
return p.wordCount
}
func (p *Page) ReadingTime() int {
- p.analyzePage()
+ p.initContentPlainAndMeta()
return p.readingTime
}
func (p *Page) FuzzyWordCount() int {
- p.analyzePage()
+ p.initContentPlainAndMeta()
return p.fuzzyWordCount
}
-func (p *Page) analyzePage() {
+func (p *Page) initContentPlainAndMeta() {
p.initContent()
+ p.initPlain(true)
+ p.initPlainWords(true)
+ p.initMeta()
+}
+
+func (p *Page) initContentAndMeta() {
+ p.initContent()
+ p.initMeta()
+}
+
+func (p *Page) initMeta() {
p.pageMetaInit.Do(func() {
if p.isCJKLanguage {
p.wordCount = 0
diff --git a/hugolib/page_test.go b/hugolib/page_test.go
index 6b2f29764..985373a90 100644
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -1735,6 +1735,101 @@ tags:
}
}
+// https://github.com/gohugoio/hugo/issues/4675
+func TestWordCountAndSimilarVsSummary(t *testing.T) {
+
+ t.Parallel()
+ assert := require.New(t)
+
+ single := []string{"_default/single.html", `
+WordCount: {{ .WordCount }}
+FuzzyWordCount: {{ .FuzzyWordCount }}
+ReadingTime: {{ .ReadingTime }}
+Len Plain: {{ len .Plain }}
+Len PlainWords: {{ len .PlainWords }}
+Truncated: {{ .Truncated }}
+Len Summary: {{ len .Summary }}
+Len Content: {{ len .Content }}
+`}
+
+ b := newTestSitesBuilder(t)
+ b.WithSimpleConfigFile().WithTemplatesAdded(single...).WithContent("p1.md", fmt.Sprintf(`---
+title: p1
+---
+
+%s
+
+`, strings.Repeat("word ", 510)),
+
+ "p2.md", fmt.Sprintf(`---
+title: p2
+---
+This is a summary.
+
+<!--more-->
+
+%s
+
+`, strings.Repeat("word ", 310)),
+ "p3.md", fmt.Sprintf(`---
+title: p3
+isCJKLanguage: true
+---
+Summary: In Chinese, 好 means good.
+
+<!--more-->
+
+%s
+
+`, strings.Repeat("好", 200)),
+ "p4.md", fmt.Sprintf(`---
+title: p4
+isCJKLanguage: false
+---
+Summary: In Chinese, 好 means good.
+
+<!--more-->
+
+%s
+
+`, strings.Repeat("好", 200)),
+
+ "p5.md", fmt.Sprintf(`---
+title: p4
+isCJKLanguage: true
+---
+Summary: In Chinese, 好 means good.
+
+%s
+
+`, strings.Repeat("好", 200)),
+ "p6.md", fmt.Sprintf(`---
+title: p4
+isCJKLanguage: false
+---
+Summary: In Chinese, 好 means good.
+
+%s
+
+`, strings.Repeat("好", 200)),
+ )
+
+ b.CreateSites().Build(BuildCfg{})
+
+ assert.Equal(1, len(b.H.Sites))
+ require.Len(t, b.H.Sites[0].RegularPages, 6)
+
+ b.AssertFileContent("public/p1/index.html", "WordCount: 510\nFuzzyWordCount: 600\nReadingTime: 3\nLen Plain: 2550\nLen PlainWords: 510\nTruncated: false\nLen Summary: 2549\nLen Content: 2557")
+
+ b.AssertFileContent("public/p2/index.html", "WordCount: 314\nFuzzyWordCount: 400\nReadingTime: 2\nLen Plain: 1570\nLen PlainWords: 314\nTruncated: true\nLen Summary: 34\nLen Content: 1592")
+
+ b.AssertFileContent("public/p3/index.html", "WordCount: 206\nFuzzyWordCount: 300\nReadingTime: 1\nLen Plain: 639\nLen PlainWords: 7\nTruncated: true\nLen Summary: 52\nLen Content: 661")
+ b.AssertFileContent("public/p4/index.html", "WordCount: 7\nFuzzyWordCount: 100\nReadingTime: 1\nLen Plain: 639\nLen PlainWords: 7\nTruncated: true\nLen Summary: 52\nLen Content: 661")
+ b.AssertFileContent("public/p5/index.html", "WordCount: 206\nFuzzyWordCount: 300\nReadingTime: 1\nLen Plain: 638\nLen PlainWords: 7\nTruncated: true\nLen Summary: 229\nLen Content: 653")
+ b.AssertFileContent("public/p6/index.html", "WordCount: 7\nFuzzyWordCount: 100\nReadingTime: 1\nLen Plain: 638\nLen PlainWords: 7\nTruncated: false\nLen Summary: 637\nLen Content: 653")
+
+}
+
func BenchmarkParsePage(b *testing.B) {
s := newTestSite(b)
f, _ := os.Open("testdata/redis.cn.md")