Restoring former snippet behavior & adding test to ensure future behavior

This commit is contained in:
spf13 2013-10-04 12:28:28 -04:00
parent dcfcbac589
commit 266f583a8c
2 changed files with 42 additions and 16 deletions

View file

@ -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) Sort() { sort.Sort(p) }
func (p Pages) Limit(n int) Pages { return p[0:n] } 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) { 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 { } else {
plainContent := StripHTML(StripShortcodes(string(content))) // If hugo defines split:
return []byte(TruncateWordsToWholeSentence(plainContent, summaryLength)), true // 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) b.ReadFrom(lines)
content := b.Bytes() content := b.Bytes()
page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content)))) page.Content = template.HTML(string(blackfriday.MarkdownCommon(RemoveSummaryDivider(content))))
summary, plain := getSummaryString(content) summary := getSummaryString(content, "markdown")
if plain {
page.Summary = template.HTML(string(summary)) page.Summary = template.HTML(string(summary))
} else {
page.Summary = template.HTML(string(blackfriday.MarkdownCommon(summary)))
}
} }
func (page *Page) convertRestructuredText(lines io.Reader) { func (page *Page) convertRestructuredText(lines io.Reader) {
@ -437,12 +448,8 @@ func (page *Page) convertRestructuredText(lines io.Reader) {
b.ReadFrom(lines) b.ReadFrom(lines)
content := b.Bytes() content := b.Bytes()
page.Content = template.HTML(getRstContent(content)) page.Content = template.HTML(getRstContent(content))
summary, plain := getSummaryString(content) summary := getSummaryString(content, "rst")
if plain {
page.Summary = template.HTML(string(summary)) page.Summary = template.HTML(string(summary))
} else {
page.Summary = template.HTML(getRstContent(summary))
}
} }
func (p *Page) TargetPath() (outfile string) { func (p *Page) TargetPath() (outfile string) {

View file

@ -109,6 +109,14 @@ title: Simple
Summary Next Line Summary Next Line
<!--more--> <!--more-->
Some more text
`
SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY = `---
title: Simple
---
Summary Next Line. {{% img src="/not/real" %}}.
More text here.
Some more text Some more text
` `
@ -206,7 +214,18 @@ func TestPageWithDelimiter(t *testing.T) {
checkPageSummary(t, p, "<p>Summary Next Line</p>\n") checkPageSummary(t, p, "<p>Summary Next Line</p>\n")
checkPageType(t, p, "page") checkPageType(t, p, "page")
checkPageLayout(t, p, "page/single.html") 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, "<p>Summary Next Line. {{% img src=&ldquo;/not/real&rdquo; %}}.\nMore text here.</p>\n\n<p>Some more text</p>\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) { func TestPageWithMoreTag(t *testing.T) {