hugo/hugolib/summary.go

76 lines
1.6 KiB
Go
Raw Normal View History

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")
}