aboutsummaryrefslogtreecommitdiffstats
path: root/post/post.go
diff options
context:
space:
mode:
authorAdam Mathes <adam@trenchant.org>2017-12-21 14:32:11 -0800
committerAdam Mathes <adam@trenchant.org>2017-12-21 14:32:11 -0800
commitcc59df8acfda58c34d5db14dc1db9eba3390b527 (patch)
treedde1dd0a0a585e0e68794ea70231a3e954b08467 /post/post.go
parente826734316f306df0cea38c9c234010c75b18290 (diff)
downloadsnkt-cc59df8acfda58c34d5db14dc1db9eba3390b527.tar.gz
snkt-cc59df8acfda58c34d5db14dc1db9eba3390b527.tar.bz2
snkt-cc59df8acfda58c34d5db14dc1db9eba3390b527.zip
add PlainText field to post, along with FirstWords and FirstChars functions for summaries/intros
Diffstat (limited to 'post/post.go')
-rw-r--r--post/post.go39
1 files changed, 38 insertions, 1 deletions
diff --git a/post/post.go b/post/post.go
index 4644a50..0311ab0 100644
--- a/post/post.go
+++ b/post/post.go
@@ -9,6 +9,7 @@ import (
"adammathes.com/snkt/render"
"adammathes.com/snkt/text"
"adammathes.com/snkt/vlog"
+ "github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/rwcarlsen/goexif/exif"
"io/ioutil"
@@ -50,6 +51,9 @@ type Post struct {
// Content with sources and references resolved to absolute URLs
AbsoluteContent string
+ // Content HTML tags removed
+ PlainText string
+
// Post following chronologically (later)
Next *Post
// Post preceding chronologically (earlier)
@@ -192,9 +196,13 @@ func (p *Post) parse() {
//
p.Content = string(p.Filter([]byte(p.Text)))
p.AbsoluteContent = render.ResolveURLs(p.Content, p.Site.GetURL())
+ policy := bluemonday.StrictPolicy()
+ p.PlainText = policy.Sanitize(p.Content)
+ p.PlainText = strings.Replace(p.PlainText, "\n\n", "\n", -1)
+ p.PlainText = strings.Replace(p.PlainText, " ", " ", -1)
// WordCount
- p.WordCount = len(strings.Split(p.Text, " "))
+ p.WordCount = len(strings.Split(p.PlainText, " "))
// Tags
// TODO: separate tag stuff to other module
@@ -377,3 +385,32 @@ func (p *Post) ContainsTag(tag string) bool {
}
return false
}
+
+/*
+Returns the first words of the plain text version of the post, up to `maxWords`
+*/
+func (p *Post) FirstWords(maxWords int) string {
+ words := strings.Split(p.PlainText, " ")
+ if len(words) <= maxWords {
+ maxWords = len(words)
+ }
+
+ return strings.Join(words[0:maxWords], " ")
+}
+
+/*
+Returns one or more words of the plain text version of the post, up to `maxChars`
+*/
+func (p *Post) FirstChars(maxChars int) string {
+
+ s := ""
+
+ words := strings.Split(p.PlainText, " ")
+ for _, word := range words {
+ if len(s)+len(word) > maxChars {
+ break
+ }
+ s = s + " " + word
+ }
+ return s
+}