2013-09-03 18:38:20 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2014-03-31 13:23:34 -04:00
|
|
|
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2013-09-03 18:38:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2014-03-31 13:23:34 -04:00
|
|
|
jww.ERROR.Println(err)
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|