From 266f583a8cf08c8c7788cf3ba7328e19bf406dc3 Mon Sep 17 00:00:00 2001 From: spf13 Date: Fri, 4 Oct 2013 12:28:28 -0400 Subject: [PATCH] Restoring former snippet behavior & adding test to ensure future behavior --- hugolib/page.go | 39 +++++++++++++++++++++++---------------- hugolib/page_test.go | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index 7bb51ebb0..e9a7d6daf 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -76,12 +76,27 @@ func (p Pages) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p Pages) Sort() { sort.Sort(p) } func (p Pages) Limit(n int) Pages { return p[0:n] } -func getSummaryString(content []byte) ([]byte, bool) { +func getSummaryString(content []byte, fmt string) []byte { if bytes.Contains(content, summaryDivider) { - return bytes.Split(content, summaryDivider)[0], false + // If user defines split: + // Split then render + return renderBytes(bytes.Split(content, summaryDivider)[0], fmt) } else { - plainContent := StripHTML(StripShortcodes(string(content))) - return []byte(TruncateWordsToWholeSentence(plainContent, summaryLength)), true + // If hugo defines split: + // render, strip html, then split + plainContent := StripHTML(StripShortcodes(string(renderBytes(content, fmt)))) + return []byte(TruncateWordsToWholeSentence(plainContent, summaryLength)) + } +} + +func renderBytes(content []byte, fmt string) []byte { + switch fmt { + default: + return blackfriday.MarkdownCommon(content) + case "markdown": + return blackfriday.MarkdownCommon(content) + case "rst": + return []byte(getRstContent(content)) } } @@ -424,12 +439,8 @@ func (page *Page) convertMarkdown(lines io.Reader) { b.ReadFrom(lines) content := b.Bytes() page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content)))) - summary, plain := getSummaryString(content) - if plain { - page.Summary = template.HTML(string(summary)) - } else { - page.Summary = template.HTML(string(blackfriday.MarkdownCommon(summary))) - } + summary := getSummaryString(content, "markdown") + page.Summary = template.HTML(string(summary)) } func (page *Page) convertRestructuredText(lines io.Reader) { @@ -437,12 +448,8 @@ func (page *Page) convertRestructuredText(lines io.Reader) { 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)) - } + summary := getSummaryString(content, "rst") + page.Summary = template.HTML(string(summary)) } func (p *Page) TargetPath() (outfile string) { diff --git a/hugolib/page_test.go b/hugolib/page_test.go index bf5e97352..33da04e5d 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -109,6 +109,14 @@ title: Simple Summary Next Line +Some more text +` + SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY = `--- +title: Simple +--- +Summary Next Line. {{% img src="/not/real" %}}. +More text here. + Some more text ` @@ -206,7 +214,18 @@ func TestPageWithDelimiter(t *testing.T) { checkPageSummary(t, p, "

Summary Next Line

\n") checkPageType(t, p, "page") checkPageLayout(t, p, "page/single.html") +} +func TestPageWithShortCodeInSummary(t *testing.T) { + p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY), "simple.md") + if err != nil { + t.Fatalf("Unable to create a page with frontmatter and body content: %s", err) + } + checkPageTitle(t, p, "Simple") + checkPageContent(t, p, "

Summary Next Line. {{% img src=“/not/real” %}}.\nMore text here.

\n\n

Some more text

\n") + checkPageSummary(t, p, "Summary Next Line. . More text here. Some more text") + checkPageType(t, p, "page") + checkPageLayout(t, p, "page/single.html") } func TestPageWithMoreTag(t *testing.T) {