Permalink
Browse files

hugolib: Handle shortcode per output format

This commit allows shortcode per output format, a typical use case would be the special AMP media tags.

Note that this will only re-render the "overridden" shortcodes and only  in pages where these are used, so performance in the normal case should not suffer.

Closes #3220
  • Loading branch information...
bep committed May 6, 2017
1 parent e951d65 commit af72db806f2c1c0bf1dfe5832275c41eeba89906
View
@@ -80,7 +80,7 @@ func (h htmlHandler) PageConvert(p *Page) HandledResult {
p.createWorkContentCopy()
if err := p.processShortcodes(); err != nil {
return HandledResult{err: err}
p.s.Log.ERROR.Println(err)
}
return HandledResult{err: nil}
@@ -131,7 +131,7 @@ func commonConvert(p *Page) HandledResult {
p.createWorkContentCopy()
if err := p.processShortcodes(); err != nil {
return HandledResult{err: err}
p.s.Log.ERROR.Println(err)
}
// TODO(bep) these page handlers need to be re-evaluated, as it is hard to
View
@@ -492,12 +492,7 @@ func (h *HugoSites) setupTranslations() {
}
}
func (s *Site) preparePagesForRender(outFormatIdx int, cfg *BuildCfg) {
if outFormatIdx > 0 {
// TODO(bep) for now
return
}
func (s *Site) preparePagesForRender(cfg *BuildCfg) {
pageChan := make(chan *Page)
wg := &sync.WaitGroup{}
@@ -508,8 +503,16 @@ func (s *Site) preparePagesForRender(outFormatIdx int, cfg *BuildCfg) {
go func(pages <-chan *Page, wg *sync.WaitGroup) {
defer wg.Done()
for p := range pages {
if !p.shouldRenderTo(s.rc.Format) {
// No need to prepare
continue
}
var shortcodeUpdate bool
if p.shortcodeState != nil {
shortcodeUpdate = p.shortcodeState.updateDelta()
}
if !cfg.whatChanged.other && p.rendered {
if !shortcodeUpdate && !cfg.whatChanged.other && p.rendered {
// No need to process it again.
continue
}
@@ -521,10 +524,12 @@ func (s *Site) preparePagesForRender(outFormatIdx int, cfg *BuildCfg) {
// Mark it as rendered
p.rendered = true
// If in watch mode, we need to keep the original so we can
// repeat this process on rebuild.
// If in watch mode or if we have multiple output formats,
// we need to keep the original so we can
// potentially repeat this process on rebuild.
needsACopy := cfg.Watching || len(p.outputFormats) > 1
var workContentCopy []byte
if cfg.Watching {
if needsACopy {
workContentCopy = make([]byte, len(p.workContent))
copy(workContentCopy, p.workContent)
} else {
@@ -589,15 +594,15 @@ func (h *HugoSites) Pages() Pages {
}
func handleShortcodes(p *Page, rawContentCopy []byte) ([]byte, error) {
if p.shortcodeState != nil && len(p.shortcodeState.contentShortCodes) > 0 {
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", len(p.shortcodeState.contentShortCodes), p.BaseFileName())
shortcodes, err := executeShortcodeFuncMap(p.shortcodeState.contentShortCodes)
if p.shortcodeState != nil && len(p.shortcodeState.contentShortcodes) > 0 {
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", len(p.shortcodeState.contentShortcodes), p.BaseFileName())
err := p.shortcodeState.executeShortcodesForDelta(p)
if err != nil {
return rawContentCopy, err
}
rawContentCopy, err = replaceShortcodeTokens(rawContentCopy, shortcodePlaceholderPrefix, shortcodes)
rawContentCopy, err = replaceShortcodeTokens(rawContentCopy, shortcodePlaceholderPrefix, p.shortcodeState.renderedShortcodes)
if err != nil {
p.s.Log.FATAL.Printf("Failed to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())
@@ -213,7 +213,7 @@ func (h *HugoSites) render(config *BuildCfg) error {
s.initRenderFormats()
for i, rf := range s.renderFormats {
s.rc = &siteRenderingContext{Format: rf}
s.preparePagesForRender(i, config)
s.preparePagesForRender(config)
if !config.SkipRender {
if err := s.render(i); err != nil {
View
@@ -1257,6 +1257,11 @@ func (p *Page) Menus() PageMenus {
return p.pageMenus
}
func (p *Page) shouldRenderTo(f output.Format) bool {
_, found := p.outputFormats.GetByName(f.Name)
return found
}
func (p *Page) determineMarkupType() string {
// Try markup explicitly set in the frontmatter
p.Markup = helpers.GuessType(p.Markup)
@@ -1372,8 +1377,8 @@ func (p *Page) SaveSource() error {
}
func (p *Page) processShortcodes() error {
p.shortcodeState = newShortcodeHandler()
tmpContent, err := p.shortcodeState.extractAndRenderShortcodes(string(p.workContent), p)
p.shortcodeState = newShortcodeHandler(p)
tmpContent, err := p.shortcodeState.extractShortcodes(string(p.workContent), p)
if err != nil {
return err
}
Oops, something went wrong.

0 comments on commit af72db8

Please sign in to comment.