Skip to content

Commit af72db8

Browse files
committed
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
1 parent e951d65 commit af72db8

File tree

10 files changed

+399
-66
lines changed

10 files changed

+399
-66
lines changed

‎hugolib/handler_page.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (h htmlHandler) PageConvert(p *Page) HandledResult {
8080
p.createWorkContentCopy()
8181

8282
if err := p.processShortcodes(); err != nil {
83-
return HandledResult{err: err}
83+
p.s.Log.ERROR.Println(err)
8484
}
8585

8686
return HandledResult{err: nil}
@@ -131,7 +131,7 @@ func commonConvert(p *Page) HandledResult {
131131
p.createWorkContentCopy()
132132

133133
if err := p.processShortcodes(); err != nil {
134-
return HandledResult{err: err}
134+
p.s.Log.ERROR.Println(err)
135135
}
136136

137137
// TODO(bep) these page handlers need to be re-evaluated, as it is hard to

‎hugolib/hugo_sites.go‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,7 @@ func (h *HugoSites) setupTranslations() {
492492
}
493493
}
494494

495-
func (s *Site) preparePagesForRender(outFormatIdx int, cfg *BuildCfg) {
496-
497-
if outFormatIdx > 0 {
498-
// TODO(bep) for now
499-
return
500-
}
495+
func (s *Site) preparePagesForRender(cfg *BuildCfg) {
501496

502497
pageChan := make(chan *Page)
503498
wg := &sync.WaitGroup{}
@@ -508,8 +503,16 @@ func (s *Site) preparePagesForRender(outFormatIdx int, cfg *BuildCfg) {
508503
go func(pages <-chan *Page, wg *sync.WaitGroup) {
509504
defer wg.Done()
510505
for p := range pages {
506+
if !p.shouldRenderTo(s.rc.Format) {
507+
// No need to prepare
508+
continue
509+
}
510+
var shortcodeUpdate bool
511+
if p.shortcodeState != nil {
512+
shortcodeUpdate = p.shortcodeState.updateDelta()
513+
}
511514

512-
if !cfg.whatChanged.other && p.rendered {
515+
if !shortcodeUpdate && !cfg.whatChanged.other && p.rendered {
513516
// No need to process it again.
514517
continue
515518
}
@@ -521,10 +524,12 @@ func (s *Site) preparePagesForRender(outFormatIdx int, cfg *BuildCfg) {
521524
// Mark it as rendered
522525
p.rendered = true
523526

524-
// If in watch mode, we need to keep the original so we can
525-
// repeat this process on rebuild.
527+
// If in watch mode or if we have multiple output formats,
528+
// we need to keep the original so we can
529+
// potentially repeat this process on rebuild.
530+
needsACopy := cfg.Watching || len(p.outputFormats) > 1
526531
var workContentCopy []byte
527-
if cfg.Watching {
532+
if needsACopy {
528533
workContentCopy = make([]byte, len(p.workContent))
529534
copy(workContentCopy, p.workContent)
530535
} else {
@@ -589,15 +594,15 @@ func (h *HugoSites) Pages() Pages {
589594
}
590595

591596
func handleShortcodes(p *Page, rawContentCopy []byte) ([]byte, error) {
592-
if p.shortcodeState != nil && len(p.shortcodeState.contentShortCodes) > 0 {
593-
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", len(p.shortcodeState.contentShortCodes), p.BaseFileName())
594-
shortcodes, err := executeShortcodeFuncMap(p.shortcodeState.contentShortCodes)
597+
if p.shortcodeState != nil && len(p.shortcodeState.contentShortcodes) > 0 {
598+
p.s.Log.DEBUG.Printf("Replace %d shortcodes in %q", len(p.shortcodeState.contentShortcodes), p.BaseFileName())
599+
err := p.shortcodeState.executeShortcodesForDelta(p)
595600

596601
if err != nil {
597602
return rawContentCopy, err
598603
}
599604

600-
rawContentCopy, err = replaceShortcodeTokens(rawContentCopy, shortcodePlaceholderPrefix, shortcodes)
605+
rawContentCopy, err = replaceShortcodeTokens(rawContentCopy, shortcodePlaceholderPrefix, p.shortcodeState.renderedShortcodes)
601606

602607
if err != nil {
603608
p.s.Log.FATAL.Printf("Failed to replace shortcode tokens in %s:\n%s", p.BaseFileName(), err.Error())

‎hugolib/hugo_sites_build.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (h *HugoSites) render(config *BuildCfg) error {
213213
s.initRenderFormats()
214214
for i, rf := range s.renderFormats {
215215
s.rc = &siteRenderingContext{Format: rf}
216-
s.preparePagesForRender(i, config)
216+
s.preparePagesForRender(config)
217217

218218
if !config.SkipRender {
219219
if err := s.render(i); err != nil {

‎hugolib/page.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,11 @@ func (p *Page) Menus() PageMenus {
12571257
return p.pageMenus
12581258
}
12591259

1260+
func (p *Page) shouldRenderTo(f output.Format) bool {
1261+
_, found := p.outputFormats.GetByName(f.Name)
1262+
return found
1263+
}
1264+
12601265
func (p *Page) determineMarkupType() string {
12611266
// Try markup explicitly set in the frontmatter
12621267
p.Markup = helpers.GuessType(p.Markup)
@@ -1372,8 +1377,8 @@ func (p *Page) SaveSource() error {
13721377
}
13731378

13741379
func (p *Page) processShortcodes() error {
1375-
p.shortcodeState = newShortcodeHandler()
1376-
tmpContent, err := p.shortcodeState.extractAndRenderShortcodes(string(p.workContent), p)
1380+
p.shortcodeState = newShortcodeHandler(p)
1381+
tmpContent, err := p.shortcodeState.extractShortcodes(string(p.workContent), p)
13771382
if err != nil {
13781383
return err
13791384
}

0 commit comments

Comments
 (0)