Save auto-detected markup type in Page.Markup

If Page.Markup was not set by the user, it will now be set after
guessing from the file extension. This means, Page.Markup will be set in
any case. It can be used by a theme to differentiate between markup
types.

Fixes #1950
This commit is contained in:
ypnos 2016-03-20 21:40:03 +01:00 committed by Bjørn Erik Pedersen
parent e6e98bf52d
commit 9f6b5ad3b4
2 changed files with 11 additions and 12 deletions

View file

@ -264,7 +264,7 @@ func (p *Page) renderBytes(content []byte) []byte {
} }
} }
return helpers.RenderBytes( return helpers.RenderBytes(
&helpers.RenderingContext{Content: content, PageFmt: p.guessMarkupType(), &helpers.RenderingContext{Content: content, PageFmt: p.determineMarkupType(),
DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn}) DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
} }
@ -279,7 +279,7 @@ func (p *Page) renderContent(content []byte) []byte {
return p.Node.Site.SourceRelativeLinkFile(ref, p) return p.Node.Site.SourceRelativeLinkFile(ref, p)
} }
} }
return helpers.RenderBytesWithTOC(&helpers.RenderingContext{Content: content, PageFmt: p.guessMarkupType(), return helpers.RenderBytesWithTOC(&helpers.RenderingContext{Content: content, PageFmt: p.determineMarkupType(),
DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn}) DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
} }
@ -801,16 +801,15 @@ func (p *Page) Render(layout ...string) template.HTML {
return tpl.ExecuteTemplateToHTML(p, l...) return tpl.ExecuteTemplateToHTML(p, l...)
} }
func (p *Page) guessMarkupType() string { func (p *Page) determineMarkupType() string {
// First try the explicitly set markup from the frontmatter // Try markup explicitly set in the frontmatter
if p.Markup != "" { p.Markup = helpers.GuessType(p.Markup)
format := helpers.GuessType(p.Markup) if p.Markup == "unknown" {
if format != "unknown" { // Fall back to file extension (might also return "unknown")
return format p.Markup = helpers.GuessType(p.Source.Ext())
}
} }
return helpers.GuessType(p.Source.Ext()) return p.Markup
} }
func (p *Page) parse(reader io.Reader) error { func (p *Page) parse(reader io.Reader) error {

View file

@ -234,7 +234,7 @@ func renderShortcode(sc shortcode, parent *ShortcodeWithPage, p *Page, t tpl.Tem
if sc.doMarkup { if sc.doMarkup {
newInner := helpers.RenderBytes(&helpers.RenderingContext{ newInner := helpers.RenderBytes(&helpers.RenderingContext{
Content: []byte(inner), PageFmt: p.guessMarkupType(), Content: []byte(inner), PageFmt: p.determineMarkupType(),
DocumentID: p.UniqueID(), Config: p.getRenderingConfig()}) DocumentID: p.UniqueID(), Config: p.getRenderingConfig()})
// If the type is “unknown” or “markdown”, we assume the markdown // If the type is “unknown” or “markdown”, we assume the markdown
@ -250,7 +250,7 @@ func renderShortcode(sc shortcode, parent *ShortcodeWithPage, p *Page, t tpl.Tem
// substitutions in <div>HUGOSHORTCODE-1</div> which prevents the // substitutions in <div>HUGOSHORTCODE-1</div> which prevents the
// generation, but means that you cant use shortcodes inside of // generation, but means that you cant use shortcodes inside of
// markdown structures itself (e.g., `[foo]({{% ref foo.md %}})`). // markdown structures itself (e.g., `[foo]({{% ref foo.md %}})`).
switch p.guessMarkupType() { switch p.determineMarkupType() {
case "unknown", "markdown": case "unknown", "markdown":
if match, _ := regexp.MatchString(innerNewlineRegexp, inner); !match { if match, _ := regexp.MatchString(innerNewlineRegexp, inner); !match {
cleaner, err := regexp.Compile(innerCleanupRegexp) cleaner, err := regexp.Compile(innerCleanupRegexp)