summaryrefslogtreecommitdiffstats
path: root/hugolib/summary.go
diff options
context:
space:
mode:
authorNoah Campbell <noahcampbell@gmail.com>2013-09-03 15:38:20 -0700
committerNoah Campbell <noahcampbell@gmail.com>2013-09-03 16:16:07 -0700
commit79d9f82e79014fffabaedd34a3997475967508f6 (patch)
treeffa8bb8c09c71773992b1ce34dc95e8a13c492fd /hugolib/summary.go
parent207d8fb7af6d1b7bcb9753a290fd60e90f8e5e0c (diff)
Code reorg, helpers.go has been decomposed.
It started with wanting to move templates in template bundles and the rest followed. I did my best to start grouping related functions together, but there are some that I missed. There is also the method Urlize that seems to be a special function used in both worlds. I'll need to revisit this method.
Diffstat (limited to 'hugolib/summary.go')
-rw-r--r--hugolib/summary.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/hugolib/summary.go b/hugolib/summary.go
new file mode 100644
index 000000000..1053e1975
--- /dev/null
+++ b/hugolib/summary.go
@@ -0,0 +1,75 @@
+package hugolib
+
+import (
+ "bytes"
+ "fmt"
+ "os/exec"
+ "strings"
+)
+
+var summaryLength = 70
+var summaryDivider = []byte("<!--more-->")
+
+func TotalWords(s string) int {
+ return len(strings.Fields(s))
+}
+
+func WordCount(s string) map[string]int {
+ m := make(map[string]int)
+ for _, f := range strings.Fields(s) {
+ m[f] += 1
+ }
+
+ return m
+}
+
+func RemoveSummaryDivider(content []byte) []byte {
+ return bytes.Replace(content, summaryDivider, []byte(""), -1)
+}
+
+func TruncateWords(s string, max int) string {
+ words := strings.Fields(s)
+ if max > len(words) {
+ return strings.Join(words, " ")
+ }
+
+ return strings.Join(words[:max], " ")
+}
+
+func TruncateWordsToWholeSentence(s string, max int) string {
+ words := strings.Fields(s)
+ if max > len(words) {
+ return strings.Join(words, " ")
+ }
+
+ for counter, word := range words[max:] {
+ if strings.HasSuffix(word, ".") ||
+ strings.HasSuffix(word, "?") ||
+ strings.HasSuffix(word, ".\"") ||
+ strings.HasSuffix(word, "!") {
+ return strings.Join(words[:max+counter+1], " ")
+ }
+ }
+
+ return strings.Join(words[:max], " ")
+}
+
+func getRstContent(content []byte) string {
+ cleanContent := bytes.Replace(content, summaryDivider, []byte(""), 1)
+
+ cmd := exec.Command("rst2html.py", "--leave-comments")
+ cmd.Stdin = bytes.NewReader(cleanContent)
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ if err := cmd.Run(); err != nil {
+ fmt.Println(err)
+ }
+
+ rstLines := strings.Split(out.String(), "\n")
+ for i, line := range rstLines {
+ if strings.HasPrefix(line, "<body>") {
+ rstLines = (rstLines[i+1 : len(rstLines)-3])
+ }
+ }
+ return strings.Join(rstLines, "\n")
+}