From 4d26ab33dcef704086f43828d1dfb4b8beae2593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 19 Apr 2018 18:06:40 +0200 Subject: Make .Content (almost) always available in shortcodes This resolves some surprising behaviour when reading other pages' content from shortcodes. Before this commit, that behaviour was undefined. Note that this has never been an issue from regular templates. It will still not be possible to get **the current shortcode's page's rendered content**. That would have impressed Einstein. The new and well defined rules are: * `.Page.Content` from a shortcode will be empty. The related `.Page.Truncated` `.Page.Summary`, `.Page.WordCount`, `.Page.ReadingTime`, `.Page.Plain` and `.Page.PlainWords` will also have empty values. * For _other pages_ (retrieved via `.Page.Site.GetPage`, `.Site.Pages` etc.) the `.Content` is there to use as you please as long as you don't have infinite content recursion in your shortcode/content setup. See below. * `.Page.TableOfContents` is good to go (but does not support shortcodes in headlines; this is unchanged) If you get into a situation of infinite recursion, the `.Content` will be empty. Run `hugo -v` for more information. Fixes #4632 Fixes #4653 Fixes #4655 --- hugolib/shortcode.go | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'hugolib/shortcode.go') diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index 78971132b..df4acba5f 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -37,7 +37,7 @@ import ( type ShortcodeWithPage struct { Params interface{} Inner template.HTML - Page *Page + Page *PageWithoutContent Parent *ShortcodeWithPage IsNamedParams bool scratch *Scratch @@ -177,7 +177,7 @@ func newDefaultScKey(shortcodeplaceholder string) scKey { type shortcodeHandler struct { init sync.Once - p *Page + p *PageWithoutContent // This is all shortcode rendering funcs for all potential output formats. contentShortcodes map[scKey]func() (string, error) @@ -196,11 +196,26 @@ type shortcodeHandler struct { // All the shortcode names in this set. nameSet map[string]bool + + placeholderID int + placeholderFunc func() string +} + +func (s *shortcodeHandler) nextPlaceholderID() int { + s.placeholderID++ + return s.placeholderID +} + +func (s *shortcodeHandler) createShortcodePlaceholder() string { + if s.placeholderFunc != nil { + return s.placeholderFunc() + } + return fmt.Sprintf("HAHA%s-%p-%d-HBHB", shortcodePlaceholderPrefix, s.p.Page, s.nextPlaceholderID()) } func newShortcodeHandler(p *Page) *shortcodeHandler { return &shortcodeHandler{ - p: p, + p: p.withoutContent(), contentShortcodes: make(map[scKey]func() (string, error)), shortcodes: make(map[string]shortcode), nameSet: make(map[string]bool), @@ -240,15 +255,11 @@ func clearIsInnerShortcodeCache() { isInnerShortcodeCache.m = make(map[string]bool) } -func createShortcodePlaceholder(id int) string { - return fmt.Sprintf("HAHA%s-%dHBHB", shortcodePlaceholderPrefix, id) -} - const innerNewlineRegexp = "\n" const innerCleanupRegexp = `\A

(.*)

\n\z` const innerCleanupExpand = "$1" -func prepareShortcodeForPage(placeholder string, sc shortcode, parent *ShortcodeWithPage, p *Page) map[scKey]func() (string, error) { +func prepareShortcodeForPage(placeholder string, sc shortcode, parent *ShortcodeWithPage, p *PageWithoutContent) map[scKey]func() (string, error) { m := make(map[scKey]func() (string, error)) lang := p.Lang() @@ -268,7 +279,7 @@ func renderShortcode( tmplKey scKey, sc shortcode, parent *ShortcodeWithPage, - p *Page) string { + p *PageWithoutContent) string { tmpl := getShortcodeTemplateForTemplateKey(tmplKey, sc.name, p.s.Tmpl) if tmpl == nil { @@ -347,7 +358,7 @@ func renderShortcode( // the content from the previous output format, if any. func (s *shortcodeHandler) updateDelta() bool { s.init.Do(func() { - s.contentShortcodes = createShortcodeRenderers(s.shortcodes, s.p) + s.contentShortcodes = createShortcodeRenderers(s.shortcodes, s.p.withoutContent()) }) contentShortcodes := s.contentShortcodesForOutputFormat(s.p.s.rc.Format) @@ -399,7 +410,7 @@ func (s *shortcodeHandler) contentShortcodesForOutputFormat(f output.Format) map return contentShortcodesForOuputFormat } -func (s *shortcodeHandler) executeShortcodesForDelta(p *Page) error { +func (s *shortcodeHandler) executeShortcodesForDelta(p *PageWithoutContent) error { for k, render := range s.contentShortcodesDelta { renderedShortcode, err := render() @@ -414,7 +425,7 @@ func (s *shortcodeHandler) executeShortcodesForDelta(p *Page) error { } -func createShortcodeRenderers(shortcodes map[string]shortcode, p *Page) map[scKey]func() (string, error) { +func createShortcodeRenderers(shortcodes map[string]shortcode, p *PageWithoutContent) map[scKey]func() (string, error) { shortcodeRenderers := make(map[scKey]func() (string, error)) @@ -433,7 +444,7 @@ var errShortCodeIllegalState = errors.New("Illegal shortcode state") // pageTokens state: // - before: positioned just before the shortcode start // - after: shortcode(s) consumed (plural when they are nested) -func (s *shortcodeHandler) extractShortcode(pt *pageTokens, p *Page) (shortcode, error) { +func (s *shortcodeHandler) extractShortcode(pt *pageTokens, p *PageWithoutContent) (shortcode, error) { sc := shortcode{} var isInner = false @@ -555,7 +566,7 @@ Loop: return sc, nil } -func (s *shortcodeHandler) extractShortcodes(stringToParse string, p *Page) (string, error) { +func (s *shortcodeHandler) extractShortcodes(stringToParse string, p *PageWithoutContent) (string, error) { startIdx := strings.Index(stringToParse, "{{") @@ -569,8 +580,6 @@ func (s *shortcodeHandler) extractShortcodes(stringToParse string, p *Page) (str // it seems that the time isn't really spent in the byte copy operations, and the impl. gets a lot cleaner pt := &pageTokens{lexer: newShortcodeLexer("parse-page", stringToParse, pos(startIdx))} - id := 1 // incremented id, will be appended onto temp. shortcode placeholders - result := bp.GetBuffer() defer bp.PutBuffer(result) //var result bytes.Buffer @@ -605,10 +614,9 @@ Loop: currShortcode.params = make([]string, 0) } - placeHolder := createShortcodePlaceholder(id) + placeHolder := s.createShortcodePlaceholder() result.WriteString(placeHolder) s.shortcodes[placeHolder] = currShortcode - id++ case tEOF: break Loop case tError: -- cgit v1.2.3