diff --git a/hugolib/helpers.go b/hugolib/helpers.go index c814a6289..9695f6310 100644 --- a/hugolib/helpers.go +++ b/hugolib/helpers.go @@ -20,6 +20,7 @@ import ( "github.com/kr/pretty" "html/template" "os" + "os/exec" "reflect" "regexp" "strconv" @@ -28,6 +29,8 @@ import ( ) var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]") +var summaryLength = 70 +var summaryDivider = []byte("") // TODO: Make these wrappers private // Wrapper around Fprintf taking verbose flag in account. @@ -329,3 +332,32 @@ func TruncateWordsToWholeSentence(s string, max int) string { func MakePermalink(domain string, path string) string { return strings.TrimRight(domain, "/") + "/" + strings.TrimLeft(path, "/") } + +func getSummaryString(content []byte) ([]byte, bool) { + if (bytes.Contains(content, summaryDivider)) { + return bytes.Split(content, summaryDivider)[0], false + } else { + plainContent := StripHTML(StripShortcodes(string(content))) + return []byte(TruncateWordsToWholeSentence(plainContent, summaryLength)), true + } +} + +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, "
") { + rstLines = (rstLines[i+1 : len(rstLines)-3]) + } + } + return strings.Join(rstLines, "\n") +} diff --git a/hugolib/page.go b/hugolib/page.go index c3aebb3f2..8a3e353b6 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -26,7 +26,6 @@ import ( "io/ioutil" "launchpad.net/goyaml" "os" - "os/exec" "path/filepath" "sort" "strings" @@ -55,8 +54,6 @@ type Page struct { Node } -const summaryLength = 70 - type File struct { FileName, OutFile, Extension string } @@ -476,27 +473,25 @@ func (page *Page) parse(reader io.Reader) error { func (page *Page) convertMarkdown(lines io.Reader) { b := new(bytes.Buffer) b.ReadFrom(lines) - content := string(blackfriday.MarkdownCommon(b.Bytes())) - page.Content = template.HTML(content) - page.Summary = template.HTML(TruncateWordsToWholeSentence(StripHTML(StripShortcodes(content)), summaryLength)) + content := b.Bytes() + page.Content = template.HTML(string(blackfriday.MarkdownCommon(content))) + summary, plain := getSummaryString(content) + if plain { + page.Summary = template.HTML(string(summary)) + } else { + page.Summary = template.HTML(string(blackfriday.MarkdownCommon(summary))) + } } func (page *Page) convertRestructuredText(lines io.Reader) { - cmd := exec.Command("rst2html.py") - cmd.Stdin = lines - var out bytes.Buffer - cmd.Stdout = &out - if err := cmd.Run(); err != nil { - fmt.Println(err) + b := new(bytes.Buffer) + b.ReadFrom(lines) + content := b.Bytes() + page.Content = template.HTML(getRstContent(content)) + summary, plain := getSummaryString(content) + if plain { + page.Summary = template.HTML(string(summary)) + } else { + page.Summary = template.HTML(getRstContent(summary)) } - - rstLines := strings.Split(out.String(), "\n") - for i, line := range rstLines { - if strings.HasPrefix(line, "") { - rstLines = (rstLines[i+1 : len(rstLines)-3]) - } - } - content := strings.Join(rstLines, "\n") - page.Content = template.HTML(content) - page.Summary = template.HTML(TruncateWordsToWholeSentence(StripHTML(StripShortcodes(content)), summaryLength)) } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 48bf1498d..08eef840f 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -69,6 +69,15 @@ var SIMPLE_PAGE_JSON_COMPACT = ` Text ` +var SIMPLE_PAGE_WITH_SUMMARY_DELIMITER = `--- +title: Simple +--- +Simple Page + + +Some more text +` + func checkError(t *testing.T, err error, expected string) { if err == nil { t.Fatalf("err is nil") @@ -108,6 +117,12 @@ func checkPageContent(t *testing.T, page *Page, content string) { } } +func checkPageSummary(t *testing.T, page *Page, summary string) { + if page.Summary != template.HTML(summary) { + t.Fatalf("Page summary is: `%s`. Expected `%s`", page.Summary, summary) + } +} + func checkPageType(t *testing.T, page *Page, pageType string) { if page.Type() != pageType { t.Fatalf("Page type is: %s. Expected: %s", page.Type(), pageType) @@ -127,6 +142,19 @@ func TestCreateNewPage(t *testing.T) { } checkPageTitle(t, p, "Simple") checkPageContent(t, p, "Simple Page
\n") + checkPageSummary(t, p, "Simple Page") + checkPageType(t, p, "page") + checkPageLayout(t, p, "page/single.html") +} + +func TestPageWithDelimiter(t *testing.T) { + p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple") + if err != nil { + t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) + } + checkPageTitle(t, p, "Simple") + checkPageContent(t, p, "Simple Page
\n\n\n\nSome more text
\n") + checkPageSummary(t, p, "Simple Page
\n") checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html") }