Skip to content

Commit

Permalink
Allow the same shortcode to be used with or without inline content
Browse files Browse the repository at this point in the history
Fixes #934
  • Loading branch information
bep committed Mar 2, 2015
1 parent 241f9f9 commit ab5862c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/content/extras/shortcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ of the content between the opening and closing shortcodes. If a closing
shortcode is required, you can check the length of `.Inner` and provide a warning
to the user.

A shortcode with `.Inner` content can be used wihout the inline content, and without the closing shortcode, by using the self-closing syntax:

{{</* innershortcode /*/>}}

The variable `.Params` contains the list of parameters in case you need to do more complicated things than `.Get`.

You can also use the variable `.Page` to access all the normal [Page Variables](/templates/variables/).
Expand Down
10 changes: 8 additions & 2 deletions hugolib/shortcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,21 @@ Loop:
}

case tScClose:
next := pt.peek()
if !isInner {
next := pt.peek()
if next.typ == tError {
// return that error, more specific
continue
}
return sc, fmt.Errorf("Shortcode '%s' in page '%s' has no .Inner, yet a closing tag was provided", next.val, p.FullFilePath())
}
pt.consume(2)
if next.typ == tRightDelimScWithMarkup || next.typ == tRightDelimScNoMarkup {
// self-closing
pt.consume(1)
} else {
pt.consume(2)
}

return sc, nil
case tText:
sc.inner = append(sc.inner, currItem.val)
Expand Down
5 changes: 4 additions & 1 deletion hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func TestExtractShortcodes(t *testing.T) {
testScPlaceholderRegexp, ""},
{"inner", `Some text. {{< inner >}}Inner Content{{< / inner >}}. Some more text.`, `inner([], false){[Inner Content]}`,
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
// issue #934
{"inner self-closing", `Some text. {{< inner />}}. Some more text.`, `inner([], false){[]}`,
fmt.Sprintf("Some text. %s. Some more text.", testScPlaceholderRegexp), ""},
{"close, but not inner", "{{< tag >}}foo{{< /tag >}}", "", false, "Shortcode 'tag' in page 'simple.md' has no .Inner.*"},
{"nested inner", `Inner->{{< inner >}}Inner Content->{{% inner2 param1 %}}inner2txt{{% /inner2 %}}Inner close->{{< / inner >}}<-done`,
`inner([], false){[Inner Content-> inner2([\"param1\"], true){[inner2txt]} Inner close->]}`,
Expand Down Expand Up @@ -212,7 +215,7 @@ func TestExtractShortcodes(t *testing.T) {
tem.AddInternalShortcode("tag.html", `tag`)
tem.AddInternalShortcode("sc1.html", `sc1`)
tem.AddInternalShortcode("sc2.html", `sc2`)
tem.AddInternalShortcode("inner.html", `{{.Inner}}`)
tem.AddInternalShortcode("inner.html", `{{with .Inner }}{{ . }}{{ end }}`)
tem.AddInternalShortcode("inner2.html", `{{.Inner}}`)
tem.AddInternalShortcode("inner3.html", `{{.Inner}}`)

Expand Down
3 changes: 3 additions & 0 deletions hugolib/shortcodeparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var shortCodeLexerTests = []shortCodeLexerTest{

{"two params", `{{< sc1 param1 param2 >}}`, []item{
tstLeftNoMD, tstSC1, tstParam1, tstParam2, tstRightNoMD, tstEOF}},
// issue #934
{"self-closing", `{{< sc1 />}}`, []item{
tstLeftNoMD, tstSC1, tstSCClose, tstRightNoMD, tstEOF}},
{"nested simple", `{{< sc1 >}}{{< sc2 >}}{{< /sc1 >}}`, []item{
tstLeftNoMD, tstSC1, tstRightNoMD,
tstLeftNoMD, tstSC2, tstRightNoMD,
Expand Down

0 comments on commit ab5862c

Please sign in to comment.