Add RuneCount to Page

Fixes #1266
This commit is contained in:
Bjørn Erik Pedersen
2015-07-12 11:05:37 +02:00
parent 90af334c21
commit 77c60a3440
4 changed files with 46 additions and 0 deletions

View File

@@ -66,6 +66,7 @@ type Page struct {
contentShortCodes map[string]string
plain string // TODO should be []byte
plainWords []string
plainRuneCount int
plainInit sync.Once
renderingConfig *helpers.Blackfriday
renderingConfigInit sync.Once
@@ -108,10 +109,24 @@ func (p *Page) PlainWords() []string {
return p.plainWords
}
// RuneCount returns the rune count, excluding any whitespace, of the plain content.
func (p *Page) RuneCount() int {
p.initPlain()
return p.plainRuneCount
}
func (p *Page) initPlain() {
p.plainInit.Do(func() {
p.plain = helpers.StripHTML(string(p.Content))
p.plainWords = strings.Fields(p.plain)
runeCount := 0
for _, r := range p.plain {
if !helpers.IsWhitespace(r) {
runeCount++
}
}
p.plainRuneCount = runeCount
return
})
}