Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with nested shortcodes #798

Merged
merged 1 commit into from
Jan 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions hugolib/shortcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,11 @@ func createShortcodePlaceholder(id int) string {
return fmt.Sprintf("{@{@%s-%d@}@}", shortcodePlaceholderPrefix, id)
}

func renderShortcodes(sc shortcode, p *Page, t tpl.Template) string {

tokenizedRenderedShortcodes := make(map[string](string))
startCount := 0

shortcodes := renderShortcode(sc, tokenizedRenderedShortcodes, startCount, p, t)

// placeholders will be numbered from 1.. and top down
for i := 1; i <= len(tokenizedRenderedShortcodes); i++ {
placeHolder := createShortcodePlaceholder(i)
shortcodes = strings.Replace(shortcodes, placeHolder, tokenizedRenderedShortcodes[placeHolder], 1)
}
return shortcodes
}

const innerNewlineRegexp = "\n"
const innerCleanupRegexp = `\A<p>(.*)</p>\n\z`
const innerCleanupExpand = "$1"

func renderShortcode(sc shortcode, tokenizedShortcodes map[string](string), cnt int, p *Page, t tpl.Template) string {
func renderShortcode(sc shortcode, p *Page, t tpl.Template) string {
var data = &ShortcodeWithPage{Params: sc.params, Page: p}
tmpl := GetTemplate(sc.name, t)

Expand All @@ -198,12 +183,7 @@ func renderShortcode(sc shortcode, tokenizedShortcodes map[string](string), cnt
case string:
inner += innerData.(string)
case shortcode:
// nested shortcodes will be rendered individually, replace them with temporary numbered tokens
cnt++
placeHolder := createShortcodePlaceholder(cnt)
renderedContent := renderShortcode(innerData.(shortcode), tokenizedShortcodes, cnt, p, t)
tokenizedShortcodes[placeHolder] = renderedContent
inner += placeHolder
inner += renderShortcode(innerData.(shortcode), p, t)
default:
jww.ERROR.Printf("Illegal state on shortcode rendering of '%s' in page %s. Illegal type in inner data: %s ",
sc.name, p.BaseFileName(), reflect.TypeOf(innerData))
Expand Down Expand Up @@ -265,7 +245,7 @@ func extractAndRenderShortcodes(stringToParse string, p *Page, t tpl.Template) (
// need to have something to replace with
renderedShortcodes[key] = ""
} else {
renderedShortcodes[key] = renderShortcodes(sc, p, t)
renderedShortcodes[key] = renderShortcode(sc, p, t)
}
}

Expand Down Expand Up @@ -418,7 +398,6 @@ Loop:
currShortcode.params = make([]string, 0)
}

// wrap it in a block level element to let it be left alone by the markup engine
placeHolder := createShortcodePlaceholder(id)
result.WriteString(placeHolder)
shortCodes[placeHolder] = currShortcode
Expand Down
6 changes: 3 additions & 3 deletions hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestNestedSC(t *testing.T) {
tem.AddInternalShortcode("scn1.html", `<div>Outer, inner is {{ .Inner }}</div>`)
tem.AddInternalShortcode("scn2.html", `<div>SC2</div>`)

CheckShortCodeMatch(t, `{{% scn1 %}}{{% scn2 %}}{{% /scn1 %}}`, "<div>Outer, inner is <div>SC2</div></div>", tem)
CheckShortCodeMatch(t, `{{% scn1 %}}{{% scn2 %}}{{% /scn1 %}}`, "<div>Outer, inner is <div>SC2</div>\n</div>", tem)

CheckShortCodeMatch(t, `{{< scn1 >}}{{% scn2 %}}{{< /scn1 >}}`, "<div>Outer, inner is <div>SC2</div></div>", tem)
}
Expand All @@ -123,11 +123,11 @@ func TestNestedComplexSC(t *testing.T) {
tem.AddInternalShortcode("aside.html", `-aside-{{ .Inner }}-asideStop-`)

CheckShortCodeMatch(t, `{{< row >}}1-s{{% column %}}2-**s**{{< aside >}}3-**s**{{< /aside >}}4-s{{% /column %}}5-s{{< /row >}}6-s`,
"-row-1-s-col-2-<strong>s</strong>-aside-3-**s**-asideStop-4-s-colStop-5-s-rowStop-6-s", tem)
"-row-1-s-col-2-<strong>s</strong>-aside-3-<strong>s</strong>-asideStop-4-s-colStop-5-s-rowStop-6-s", tem)

// turn around the markup flag
CheckShortCodeMatch(t, `{{% row %}}1-s{{< column >}}2-**s**{{% aside %}}3-**s**{{% /aside %}}4-s{{< /column >}}5-s{{% /row %}}6-s`,
"-row-1-s-col-2-**s**-aside-3-<strong>s</strong>-asideStop-4-s-colStop-5-s-rowStop-6-s", tem)
"-row-1-s-col-2-<strong>s</strong>-aside-3-<strong>s</strong>-asideStop-4-s-colStop-5-s-rowStop-6-s", tem)
}

func TestFigureImgWidth(t *testing.T) {
Expand Down