Skip to content

Commit

Permalink
create: Preserve shortcodes in archetype templates
Browse files Browse the repository at this point in the history
Fixes #3623
  • Loading branch information
bep committed Jun 24, 2017
1 parent bfa336d commit b63e4ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 20 additions & 1 deletion create/content_template_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package create
import (
"bytes"
"fmt"
"strings"
"time"

"github.com/gohugoio/hugo/helpers"
Expand Down Expand Up @@ -57,6 +58,20 @@ draft: true
`
)

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 (
Expand Down Expand Up @@ -86,6 +101,10 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile

}

// 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)
Expand All @@ -100,7 +119,7 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
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.
Expand Down
23 changes: 23 additions & 0 deletions create/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func TestNewContent(t *testing.T) {
{"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 {
Expand Down Expand Up @@ -126,6 +131,24 @@ title = "{{ .BaseFileName | upper }}"
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)
if err != nil {
Expand Down

0 comments on commit b63e4ee

Please sign in to comment.