hugolib: Fix shortcode namespace issue
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 15 Apr 2019 13:17:46 +0000 (15:17 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 15 Apr 2019 16:31:56 +0000 (18:31 +0200)
Fixes #5863

hugolib/shortcode_test.go
tpl/tplimpl/shortcodes.go
tpl/tplimpl/shortcodes_test.go
tpl/tplimpl/template.go

index dd01eb5c504a2512da9e9c6e2fa9f8d7257cb495..ea56bf79281ff91512c73fa69c5c91be46c678c0 100644 (file)
@@ -1126,3 +1126,32 @@ CONTENT:{{ .Content }}
 
        }
 }
+
+// https://github.com/gohugoio/hugo/issues/5863
+func TestShortcodeNamespaced(t *testing.T) {
+       t.Parallel()
+       assert := require.New(t)
+
+       builder := newTestSitesBuilder(t).WithSimpleConfigFile()
+
+       builder.WithContent("page.md", `---
+title: "Hugo Rocks!"
+---
+
+# doc
+
+   hello: {{< hello >}}
+   test/hello: {{< test/hello >}}
+
+`).WithTemplatesAdded(
+               "layouts/shortcodes/hello.html", `hello`,
+               "layouts/shortcodes/test/hello.html", `test/hello`).CreateSites().Build(BuildCfg{})
+
+       s := builder.H.Sites[0]
+       assert.Equal(1, len(s.RegularPages()))
+
+       builder.AssertFileContent("public/page/index.html",
+               "hello: hello",
+               "test/hello: test/hello",
+       )
+}
index 40fdeea5d10bef043a4709fcd990c8154706e03e..92c25f10802030fc8a3f958671508c1ab6e25517 100644 (file)
@@ -152,9 +152,9 @@ func resolveTemplateType(name string) templateType {
 }
 
 func isShortcode(name string) bool {
-       return strings.Contains(name, "shortcodes/")
+       return strings.Contains(name, shortcodesPathPrefix)
 }
 
 func isInternal(name string) bool {
-       return strings.HasPrefix(name, "_internal/")
+       return strings.HasPrefix(name, internalPathPrefix)
 }
index 7439f715aca9cdcb1293cd4bbe9674fbdfd5ef02..da30d414958c6666bd2b3393357f8f4954beed7c 100644 (file)
@@ -85,9 +85,13 @@ func TestShortcodesTemplate(t *testing.T) {
 
        })
 
-       t.Run("Template", func(t *testing.T) {
+       t.Run("Name", func(t *testing.T) {
                assert := require.New(t)
 
+               assert.Equal("foo.html", templateBaseName(templateShortcode, "shortcodes/foo.html"))
+               assert.Equal("foo.html", templateBaseName(templateShortcode, "_internal/shortcodes/foo.html"))
+               assert.Equal("test/foo.html", templateBaseName(templateShortcode, "shortcodes/test/foo.html"))
+
                assert.True(true)
 
        })
index 49b9e1c349ab3b2cdd635199dd7e076b72caafdd..8fcaa8d646296f00d60c66e012995526cd7ed8bf 100644 (file)
@@ -16,7 +16,6 @@ package tplimpl
 import (
        "fmt"
        "html/template"
-       "path"
        "strings"
        texttemplate "text/template"
 
@@ -112,8 +111,27 @@ type templateHandler struct {
        *deps.Deps
 }
 
+const (
+       shortcodesPathPrefix = "shortcodes/"
+       internalPathPrefix   = "_internal/"
+)
+
+// resolves _internal/shortcodes/param.html => param.html etc.
+func templateBaseName(typ templateType, name string) string {
+       name = strings.TrimPrefix(name, internalPathPrefix)
+       switch typ {
+       case templateShortcode:
+               return strings.TrimPrefix(name, shortcodesPathPrefix)
+       default:
+               panic("not implemented")
+       }
+
+}
+
 func (t *templateHandler) addShortcodeVariant(name string, info tpl.Info, templ tpl.Template) {
-       shortcodename, variants := templateNameAndVariants(path.Base(name))
+       base := templateBaseName(templateShortcode, name)
+
+       shortcodename, variants := templateNameAndVariants(base)
 
        templs, found := t.shortcodes[shortcodename]
        if !found {
@@ -204,7 +222,7 @@ func (t *templateHandler) applyTemplateInfo(templ tpl.Template, found bool) (tpl
 // This currently only applies to shortcodes and what we get here is the
 // shortcode name.
 func (t *templateHandler) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
-       name = path.Base(name)
+       name = templateBaseName(templateShortcode, name)
        s, found := t.shortcodes[name]
        if !found {
                return nil, false, false