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
This commit is contained in:
Bjørn Erik Pedersen 2018-04-19 18:06:40 +02:00
parent d6a2024e6b
commit 4d26ab33dc
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
18 changed files with 392 additions and 153 deletions

View file

@ -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<p>(.*)</p>\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: