Skip to content

Commit 503ca6d

Browse files
committed
Fix broken shortcodes for Ace and Amber
Fixes #4051
1 parent b3daa1f commit 503ca6d

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

‎hugolib/template_engines_test.go‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestAllTemplateEngines(t *testing.T) {
3232
amberFixer := func(s string) string {
3333
fixed := strings.Replace(s, "{{ .Title", "{{ Title", -1)
3434
fixed = strings.Replace(fixed, ".Content", "Content", -1)
35+
fixed = strings.Replace(fixed, ".IsNamedParams", "IsNamedParams", -1)
3536
fixed = strings.Replace(fixed, "{{", "#{", -1)
3637
fixed = strings.Replace(fixed, "}}", "}", -1)
3738
fixed = strings.Replace(fixed, `title "hello world"`, `title("hello world")`, -1)
@@ -47,8 +48,10 @@ func TestAllTemplateEngines(t *testing.T) {
4748
{"html", noOp},
4849
{"ace", noOp},
4950
} {
50-
doTestTemplateEngine(t, config.suffix, config.templateFixer)
51-
51+
t.Run(config.suffix,
52+
func(t *testing.T) {
53+
doTestTemplateEngine(t, config.suffix, config.templateFixer)
54+
})
5255
}
5356

5457
}
@@ -57,13 +60,6 @@ func doTestTemplateEngine(t *testing.T, suffix string, templateFixer func(s stri
5760

5861
cfg, fs := newTestCfg()
5962

60-
writeSource(t, fs, filepath.Join("content", "p.md"), `
61-
---
62-
title: My Title
63-
---
64-
My Content
65-
`)
66-
6763
t.Log("Testing", suffix)
6864

6965
templTemplate := `
@@ -75,13 +71,29 @@ p
7571
br
7672
| {{ title "hello world" }}
7773
74+
`
75+
76+
templShortcodeTemplate := `
77+
p
78+
|
79+
| Shortcode: {{ .IsNamedParams }}
7880
`
7981

8082
templ := templateFixer(templTemplate)
83+
shortcodeTempl := templateFixer(templShortcodeTemplate)
84+
85+
writeSource(t, fs, filepath.Join("content", "p.md"), `
86+
---
87+
title: My Title
88+
---
89+
My Content
8190
82-
t.Log(templ)
91+
Shortcode: {{< myShort >}}
92+
93+
`)
8394

8495
writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ)
96+
writeSource(t, fs, filepath.Join("layouts", "shortcodes", fmt.Sprintf("myShort.%s", suffix)), shortcodeTempl)
8597

8698
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
8799
th := testHelper{s.Cfg, s.Fs, t}
@@ -90,6 +102,7 @@ p
90102
"Page Title: My Title",
91103
"My Content",
92104
"Hello World",
105+
"Shortcode: false",
93106
)
94107

95108
}

��tpl/tplimpl/ace.go‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package tplimpl
1515

1616
import (
17+
"html/template"
1718
"path/filepath"
1819

1920
"strings"
@@ -24,7 +25,8 @@ import (
2425
func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error {
2526
t.checkState()
2627
var base, inner *ace.File
27-
name = name[:len(name)-len(filepath.Ext(innerPath))] + ".html"
28+
withoutExt := name[:len(name)-len(filepath.Ext(innerPath))]
29+
name = withoutExt + ".html"
2830

2931
// Fixes issue #1178
3032
basePath = strings.Replace(basePath, "\\", "/", -1)
@@ -37,15 +39,29 @@ func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseC
3739
base = ace.NewFile(innerPath, innerContent)
3840
inner = ace.NewFile("", []byte{})
3941
}
42+
4043
parsed, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil)
4144
if err != nil {
4245
t.errors = append(t.errors, &templateErr{name: name, err: err})
4346
return err
4447
}
48+
4549
templ, err := ace.CompileResultWithTemplate(t.html.t.New(name), parsed, nil)
4650
if err != nil {
4751
t.errors = append(t.errors, &templateErr{name: name, err: err})
4852
return err
4953
}
50-
return applyTemplateTransformersToHMLTTemplate(templ)
54+
55+
if err := applyTemplateTransformersToHMLTTemplate(templ); err != nil {
56+
return err
57+
}
58+
59+
if strings.Contains(name, "shortcodes") {
60+
// We need to keep track of one ot the output format's shortcode template
61+
// without knowing the rendering context.
62+
clone := template.Must(templ.Clone())
63+
t.html.t.AddParseTree(withoutExt, clone.Tree)
64+
}
65+
66+
return nil
5167
}

‎tpl/tplimpl/template.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
622622
switch ext {
623623
case ".amber":
624624
// Only HTML support for Amber
625-
templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
625+
withoutExt := strings.TrimSuffix(name, filepath.Ext(name))
626+
templateName := withoutExt + ".html"
626627
b, err := afero.ReadFile(t.Fs.Source, path)
627628

628629
if err != nil {
@@ -636,7 +637,19 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
636637
return err
637638
}
638639

639-
return applyTemplateTransformersToHMLTTemplate(templ)
640+
if err := applyTemplateTransformersToHMLTTemplate(templ); err != nil {
641+
return err
642+
}
643+
644+
if strings.Contains(templateName, "shortcodes") {
645+
// We need to keep track of one ot the output format's shortcode template
646+
// without knowing the rendering context.
647+
clone := template.Must(templ.Clone())
648+
t.html.t.AddParseTree(withoutExt, clone.Tree)
649+
}
650+
651+
return nil
652+
640653
case ".ace":
641654
// Only HTML support for Ace
642655
var innerContent, baseContent []byte

0 commit comments

Comments
 (0)