render shortcodes prior to converting to html

This commit is contained in:
spf13 2013-12-06 23:32:00 -05:00
parent a45de56db1
commit f432b187a0
4 changed files with 33 additions and 8 deletions

View file

@ -36,6 +36,7 @@ import (
type Page struct {
Status string
Images []string
RawContent []byte
Content template.HTML
Summary template.HTML
Truncated bool
@ -520,17 +521,23 @@ func (page *Page) parse(reader io.Reader) error {
if err = page.update(meta); err != nil {
return err
}
}
}
page.Content = template.HTML(p.Content())
return nil
}
func (page *Page) Convert() error {
switch page.guessMarkupType() {
case "md", "markdown", "mdown":
page.convertMarkdown(bytes.NewReader(p.Content()))
page.convertMarkdown(bytes.NewReader([]byte(page.Content)))
case "rst":
page.convertRestructuredText(bytes.NewReader(p.Content()))
page.convertRestructuredText(bytes.NewReader([]byte(page.Content)))
case "html":
fallthrough
default:
page.Content = template.HTML(p.Content())
page.Content = template.HTML(page.Content)
}
return nil
}

View file

@ -227,6 +227,8 @@ func checkTruncation(t *testing.T, page *Page, shouldBe bool, msg string) {
func TestCreateNewPage(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple.md")
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
}
@ -240,6 +242,7 @@ func TestCreateNewPage(t *testing.T) {
func TestPageWithDelimiter(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER), "simple.md")
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
}
@ -253,6 +256,7 @@ func TestPageWithDelimiter(t *testing.T) {
func TestPageWithShortCodeInSummary(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SHORTCODE_IN_SUMMARY), "simple.md")
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
}
@ -265,6 +269,7 @@ func TestPageWithShortCodeInSummary(t *testing.T) {
func TestPageWithMoreTag(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_SUMMARY_DELIMITER_SAME_LINE), "simple.md")
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
}
@ -277,6 +282,7 @@ func TestPageWithMoreTag(t *testing.T) {
func TestPageWithDate(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE), "simple")
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
}
@ -289,6 +295,7 @@ func TestPageWithDate(t *testing.T) {
func TestWordCount(t *testing.T) {
p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_WITH_LONG_CONTENT), "simple.md")
p.Convert()
if err != nil {
t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
}

View file

@ -170,8 +170,6 @@ func (s *Site) Render() (err error) {
return
}
s.timerStep("render and write aliases")
s.ProcessShortcodes()
s.timerStep("render shortcodes")
if err = s.RenderIndexes(); err != nil {
return
}
@ -289,6 +287,16 @@ func (s *Site) CreatePages() (err error) {
page.Tmpl = s.Tmpl
page.Section = file.Section
page.Dir = file.Dir
// Handling short codes prior to Conversion to HTML
page.Content = template.HTML(ShortcodesHandle(string(page.Content), page, s.Tmpl))
page.Summary = template.HTML(ShortcodesHandle(string(page.Summary), page, s.Tmpl))
err = page.Convert()
if err != nil {
return err
}
if s.Config.BuildDrafts || !page.Draft {
s.Pages = append(s.Pages, page)
}

View file

@ -23,8 +23,8 @@ content`
TEMPLATE_CONTENT = "{{ .Content }}"
TEMPLATE_DATE = "{{ .Date }}"
INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}"
TEMPLATE_WITH_URL_REL = "<a href=\"foobar.jpg\">Going</a>"
TEMPLATE_WITH_URL_ABS = "<a href=\"/foobar.jpg\">Going</a>"
TEMPLATE_WITH_URL_REL = "<a href=\"foobar.jpg\">Going</a>"
TEMPLATE_WITH_URL_ABS = "<a href=\"/foobar.jpg\">Going</a>"
PAGE_URL_SPECIFIED = `---
title: simple template
url: "mycategory/my-whatever-content/"
@ -50,6 +50,7 @@ func pageMust(p *Page, err error) *Page {
func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p.Convert()
s := new(Site)
s.prepTemplates()
err := s.renderThing(p, "foobar", nil)
@ -106,6 +107,7 @@ func TestRenderThing(t *testing.T) {
for i, test := range tests {
p, err := ReadFrom(strings.NewReader(test.content), "content/a/file.md")
p.Convert()
if err != nil {
t.Fatalf("Error parsing buffer: %s", err)
}
@ -234,6 +236,7 @@ func TestSkipRender(t *testing.T) {
Config: Config{Verbose: true, BaseUrl: "http://auth/bub"},
Source: &source.InMemorySource{sources},
}
s.initializeSiteInfo()
s.prepTemplates()