aboutsummaryrefslogtreecommitdiffstats
path: root/post/post.go
diff options
context:
space:
mode:
authorAdam Mathes <adam@trenchant.org>2017-03-13 19:38:37 -0700
committerAdam Mathes <adam@trenchant.org>2017-03-13 19:38:37 -0700
commit0bde21f3da1152eeaea0a8f712f7244b26d5df02 (patch)
treea57b8d93f5c6169b2ff8faee937df8bf891074a1 /post/post.go
parent980434846df7e93e64882b7e852e6060acd8f547 (diff)
downloadsnkt-0bde21f3da1152eeaea0a8f712f7244b26d5df02.tar.gz
snkt-0bde21f3da1152eeaea0a8f712f7244b26d5df02.tar.bz2
snkt-0bde21f3da1152eeaea0a8f712f7244b26d5df02.zip
add WordCount to posts
Diffstat (limited to 'post/post.go')
-rw-r--r--post/post.go83
1 files changed, 45 insertions, 38 deletions
diff --git a/post/post.go b/post/post.go
index 278b4df..6ce195b 100644
--- a/post/post.go
+++ b/post/post.go
@@ -36,6 +36,7 @@ type Post struct {
Month time.Month
Day int
InFuture bool
+ WordCount int
// Content text -- raw, unprocessed, unfiltered markdown
Text string
@@ -120,48 +121,16 @@ func (p *Post) parse() {
p.Title = p.SourceFile
}
- //
- // Dates
- //
- // we only deal with yyyy-mm-dd [some legacy dates from my archives have times tacked on]
- // TODO: recover from empty dates/titles
- // TODO: probably should actually use times when present and clean up my archives
- var date_str = ""
- ds := strings.Fields(p.Meta["date"])
- if len(ds) > 0 {
- date_str = ds[0]
- }
-
- if date_str == "" {
- p.Time = p.FileInfo.ModTime()
- vlog.Printf("no date field in post %s, using file modification time\n", p.SourceFile)
- } else {
- var err error
- p.Time, err = time.ParseInLocation("2006-1-2", date_str, time.Local)
- if err != nil {
- // fallback is to use file modtime
- // should use create time but that doesn't seem to be in stdlib
- // TODO: figure out how to use file birth time
- vlog.Printf("no valid date parsed for post %s, using file modification time\n", p.SourceFile)
- p.Time = p.FileInfo.ModTime()
- }
- }
-
- p.Year, p.Month, p.Day = p.Time.Date()
- /* golang date format refresher
- 1 2 3 4 5 7 6
- Mon Jan 2 15:04:05 MST 2006 */
-
- p.Date = p.Time.Format("January 2, 2006")
- p.RssDate = p.Time.Format(time.RFC822)
- p.InFuture = time.Now().Before(p.Time)
- p.Permalink = p.GenPermalink()
+ p.parseDates()
//
// Content
//
p.Content = string(p.Filter([]byte(p.Text)))
p.AbsoluteContent = render.ResolveURLs(p.Content, p.Site.GetURL())
+
+ // WordCount
+ p.WordCount = len(strings.Split(p.Text, " "))
}
/*
@@ -205,6 +174,46 @@ func (p *Post) ParseFmt(s string) string {
return s
}
+func (p *Post) parseDates() {
+ //
+ // Dates
+ //
+ // we only deal with yyyy-mm-dd [some legacy dates from my archives have times tacked on]
+ // TODO: recover from empty dates/titles
+ // TODO: probably should actually use times when present and clean up my archives
+ var date_str = ""
+ ds := strings.Fields(p.Meta["date"])
+ if len(ds) > 0 {
+ date_str = ds[0]
+ }
+
+ if date_str == "" {
+ p.Time = p.FileInfo.ModTime()
+ vlog.Printf("no date field in post %s, using file modification time\n", p.SourceFile)
+ } else {
+ var err error
+ p.Time, err = time.ParseInLocation("2006-1-2", date_str, time.Local)
+ if err != nil {
+ // fallback is to use file modtime
+ // should use create time but that doesn't seem to be in stdlib
+ // TODO: figure out how to use file birth time
+ vlog.Printf("no valid date parsed for post %s, using file modification time\n", p.SourceFile)
+ p.Time = p.FileInfo.ModTime()
+ }
+ }
+
+ p.Year, p.Month, p.Day = p.Time.Date()
+ /* golang date format refresher
+ 1 2 3 4 5 7 6
+ Mon Jan 2 15:04:05 MST 2006 */
+
+ p.Date = p.Time.Format("January 2, 2006")
+ p.RssDate = p.Time.Format(time.RFC822)
+ p.InFuture = time.Now().Before(p.Time)
+ p.Permalink = p.GenPermalink()
+}
+
+
func (p *Post) CleanFilename() string {
return text.SanitizeFilename(text.RemoveExt(p.SourceFile))
}
@@ -213,8 +222,6 @@ func (p *Post) CleanTitle() string {
return text.SanitizeFilename(p.Title)
}
-
-
/*
GenPermalink generates the permalink for the post given the PermalinkFmt format specified in the configuration file.
*/