From fde47c5eb9435083cc492b8648517b374eb60c6b Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 20 Nov 2015 18:59:54 -0600 Subject: [PATCH] Add shortcode IsNamedParams property 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 | 13 +++++++++---- hugolib/shortcode_test.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index a306023a7..0b2e9f78e 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -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

(.*)

\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 { diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index 3527bbdf1..7a670ba81 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -119,6 +119,20 @@ func TestNamedParamSC(t *testing.T) { CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, ``, tem) } +func TestIsNamedParamsSC(t *testing.T) { + tem := tpl.New() + tem.AddInternalShortcode("byposition.html", `
`) + tem.AddInternalShortcode("byname.html", `
`) + tem.AddInternalShortcode("ifnamedparams.html", `
`) + + CheckShortCodeMatch(t, `{{< ifnamedparams id="name" >}}`, `
`, tem) + CheckShortCodeMatch(t, `{{< ifnamedparams position >}}`, `
`, tem) + CheckShortCodeMatch(t, `{{< byname id="name" >}}`, `
`, tem) + CheckShortCodeMatch(t, `{{< byname position >}}`, `
`, tem) + CheckShortCodeMatch(t, `{{< byposition position >}}`, `
`, tem) + CheckShortCodeMatch(t, `{{< byposition id="name" >}}`, `
`, tem) +} + func TestInnerSC(t *testing.T) { tem := tpl.New() tem.AddInternalShortcode("inside.html", `{{ .Inner }}
`)