shithub: hugo

Download patch

ref: ab5862cd00d84114cc82cde88d31ab1d8eddd388
parent: 241f9f9e469b14145f88546ce3873c9b0e1a2634
author: bep <[email protected]>
date: Mon Mar 2 16:23:16 EST 2015

Allow the same shortcode to  be used with or without inline content

Fixes #934

--- a/docs/content/extras/shortcodes.md
+++ b/docs/content/extras/shortcodes.md
@@ -193,6 +193,10 @@
 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/). 
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -307,8 +307,8 @@
 			}
 
 		case tScClose:
+			next := pt.peek()
 			if !isInner {
-				next := pt.peek()
 				if next.typ == tError {
 					// return that error, more specific
 					continue
@@ -315,7 +315,13 @@
 				}
 				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)
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -184,6 +184,9 @@
 			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->]}`,
@@ -212,7 +215,7 @@
 		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}}`)
 
--- a/hugolib/shortcodeparser_test.go
+++ b/hugolib/shortcodeparser_test.go
@@ -65,6 +65,9 @@
 
 	{"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,