Add shortcode IsNamedParams property
authorCameron Moore <moorereason@gmail.com>
Sat, 21 Nov 2015 00:59:54 +0000 (18:59 -0600)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 23 Nov 2015 16:17:18 +0000 (17:17 +0100)
It would be helpful to know whether a shortcode was called with positional or
named parameters.  This commit adds a boolean `IsNamedParams` property to the
`ShortcodeWithPage` struct.

hugolib/shortcode.go
hugolib/shortcode_test.go

index a306023a71adc085ce8eb366ea64827b2fedfebc..0b2e9f78ec92a3b2543edb8a06bee2d62e988586 100644 (file)
@@ -31,9 +31,10 @@ import (
 )
 
 type ShortcodeWithPage struct {
-       Params interface{}
-       Inner  template.HTML
-       Page   *Page
+       Params        interface{}
+       Inner         template.HTML
+       Page          *Page
+       IsNamedParams bool
 }
 
 func (scp *ShortcodeWithPage) Ref(ref string) (string, error) {
@@ -189,7 +190,6 @@ const innerCleanupRegexp = `\A<p>(.*)</p>\n\z`
 const innerCleanupExpand = "$1"
 
 func renderShortcode(sc shortcode, p *Page, t tpl.Template) string {
-       var data = &ShortcodeWithPage{Params: sc.params, Page: p}
        tmpl := getShortcodeTemplate(sc.name, t)
 
        if tmpl == nil {
@@ -197,6 +197,11 @@ func renderShortcode(sc shortcode, p *Page, t tpl.Template) string {
                return ""
        }
 
+       data := &ShortcodeWithPage{Params: sc.params, Page: p}
+       if sc.params != nil {
+               data.IsNamedParams = reflect.TypeOf(sc.params).Kind() == reflect.Map
+       }
+
        if len(sc.inner) > 0 {
                var inner string
                for _, innerData := range sc.inner {
index 3527bbdf1db6647c12e9726e73cdf3afe24c215d..7a670ba814c23f0908b6f04a8fb561041b43b633 100644 (file)
@@ -119,6 +119,20 @@ func TestNamedParamSC(t *testing.T) {
        CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, `<img src="one" class="aspen grove">`, tem)
 }
 
+func TestIsNamedParamsSC(t *testing.T) {
+       tem := tpl.New()
+       tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`)
+       tem.AddInternalShortcode("byname.html", `<div id="{{ .Get "id" }}">`)
+       tem.AddInternalShortcode("ifnamedparams.html", `<div id="{{ if .IsNamedParams }}{{ .Get "id" }}{{ else }}{{ .Get 0 }}{{end}}">`)
+
+       CheckShortCodeMatch(t, `{{< ifnamedparams id="name" >}}`, `<div id="name">`, tem)
+       CheckShortCodeMatch(t, `{{< ifnamedparams position >}}`, `<div id="position">`, tem)
+       CheckShortCodeMatch(t, `{{< byname id="name" >}}`, `<div id="name">`, tem)
+       CheckShortCodeMatch(t, `{{< byname position >}}`, `<div id="error: cannot access positional params by string name">`, tem)
+       CheckShortCodeMatch(t, `{{< byposition position >}}`, `<div id="position">`, tem)
+       CheckShortCodeMatch(t, `{{< byposition id="name" >}}`, `<div id="error: cannot access named params by position">`, tem)
+}
+
 func TestInnerSC(t *testing.T) {
        tem := tpl.New()
        tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)