ref: 2e4ccd3d34dedc136dd4d0976705c690c63ffd73
parent: fd924d1802cb9c20c2617b1c72dac6bc36560d61
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Jun 23 05:29:59 EDT 2017
create: Preserve shortcodes in archetype templates Fixes #3623
--- a/create/content_template_handler.go
+++ b/create/content_template_handler.go
@@ -16,6 +16,7 @@
import (
"bytes"
"fmt"
+ "strings"
"time"
"github.com/gohugoio/hugo/helpers"
@@ -57,6 +58,20 @@
`
)
+var (
+ archetypeShortcodeReplacementsPre = strings.NewReplacer(
+ "{{<", "{x{<",
+ "{{%", "{x{%",
+ ">}}", ">}x}",
+ "%}}", "%}x}")
+
+ archetypeShortcodeReplacementsPost = strings.NewReplacer(
+ "{x{<", "{{<",
+ "{x{%", "{{%",
+ ">}x}", ">}}",
+ "%}x}", "%}}")
+)
+
func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFilename string) ([]byte, error) {
var (
@@ -86,6 +101,10 @@
}
+ // The archetype template may contain shortcodes, and these does not play well
+ // with the Go templates. Need to set some temporary delimiters.
+ archetypeTemplate = []byte(archetypeShortcodeReplacementsPre.Replace(string(archetypeTemplate)))
+
// Reuse the Hugo template setup to get the template funcs properly set up.
templateHandler := s.Deps.Tmpl.(tpl.TemplateHandler)
templateName := "_text/" + helpers.Filename(archetypeFilename)
@@ -100,7 +119,7 @@
return nil, fmt.Errorf("Failed to process archetype file %q: %s", archetypeFilename, err)
}
- archetypeContent = buff.Bytes()
+ archetypeContent = []byte(archetypeShortcodeReplacementsPost.Replace(buff.String()))
if !bytes.Contains(archetypeContent, []byte("date")) || !bytes.Contains(archetypeContent, []byte("title")) {
// TODO(bep) remove some time in the future.
--- a/create/content_test.go
+++ b/create/content_test.go
@@ -49,6 +49,11 @@
{"stump", "stump/sample-2.md", []string{`title: "Sample 2"`}}, // no archetype file
{"", "sample-3.md", []string{`title: "Sample 3"`}}, // no archetype
{"product", "product/sample-4.md", []string{`title = "SAMPLE-4"`}}, // empty archetype front matter
+ {"shortcodes", "shortcodes/go.md", []string{
+ `title = "GO"`,
+ "{{< myshortcode >}}",
+ "{{% myshortcode %}}",
+ "{{</* comment */>}}\n{{%/* comment */%}}"}}, // shortcodes
}
for _, c := range cases {
@@ -125,6 +130,24 @@
{
path: filepath.Join("archetypes", "emptydate.md"),
content: "+++\ndate =\"\"\ntitle = \"Empty Date Arch title\"\ntest = \"test1\"\n+++\n",
+ },
+ // #3623x
+ {
+ path: filepath.Join("archetypes", "shortcodes.md"),
+ content: `+++
+title = "{{ .BaseFileName | upper }}"
++++
+
+{{< myshortcode >}}
+
+Some text.
+
+{{% myshortcode %}}
+{{</* comment */>}}
+{{%/* comment */%}}
+
+
+`,
},
} {
f, err := fs.Source.Create(v.path)