diff options
author | Bjørn Erik Pedersen <[email protected]> | 2017-11-18 10:18:41 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2017-11-18 10:41:31 +0100 |
commit | 503ca6de6ceb0b4af533f9efeff917d6f3871278 (patch) | |
tree | 415b32f7659cfcacfe9ba667a2dece089a11ea65 | |
parent | b3daa1f4bf1b84bcc5da028257ba609be74e3ecc (diff) | |
download | hugo-503ca6de6ceb0b4af533f9efeff917d6f3871278.tar.gz hugo-503ca6de6ceb0b4af533f9efeff917d6f3871278.zip |
Fix broken shortcodes for Ace and Amber
Fixes #4051
-rw-r--r-- | hugolib/template_engines_test.go | 33 | ||||
-rw-r--r-- | tpl/tplimpl/ace.go | 20 | ||||
-rw-r--r-- | tpl/tplimpl/template.go | 17 |
3 files changed, 56 insertions, 14 deletions
diff --git a/hugolib/template_engines_test.go b/hugolib/template_engines_test.go index e2e4ee986..6a046c9f5 100644 --- a/hugolib/template_engines_test.go +++ b/hugolib/template_engines_test.go @@ -32,6 +32,7 @@ func TestAllTemplateEngines(t *testing.T) { amberFixer := func(s string) string { fixed := strings.Replace(s, "{{ .Title", "{{ Title", -1) fixed = strings.Replace(fixed, ".Content", "Content", -1) + fixed = strings.Replace(fixed, ".IsNamedParams", "IsNamedParams", -1) fixed = strings.Replace(fixed, "{{", "#{", -1) fixed = strings.Replace(fixed, "}}", "}", -1) fixed = strings.Replace(fixed, `title "hello world"`, `title("hello world")`, -1) @@ -47,8 +48,10 @@ func TestAllTemplateEngines(t *testing.T) { {"html", noOp}, {"ace", noOp}, } { - doTestTemplateEngine(t, config.suffix, config.templateFixer) - + t.Run(config.suffix, + func(t *testing.T) { + doTestTemplateEngine(t, config.suffix, config.templateFixer) + }) } } @@ -57,13 +60,6 @@ func doTestTemplateEngine(t *testing.T, suffix string, templateFixer func(s stri cfg, fs := newTestCfg() - writeSource(t, fs, filepath.Join("content", "p.md"), ` ---- -title: My Title ---- -My Content -`) - t.Log("Testing", suffix) templTemplate := ` @@ -77,11 +73,27 @@ p ` + templShortcodeTemplate := ` +p + | + | Shortcode: {{ .IsNamedParams }} +` + templ := templateFixer(templTemplate) + shortcodeTempl := templateFixer(templShortcodeTemplate) + + writeSource(t, fs, filepath.Join("content", "p.md"), ` +--- +title: My Title +--- +My Content - t.Log(templ) +Shortcode: {{< myShort >}} + +`) writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ) + writeSource(t, fs, filepath.Join("layouts", "shortcodes", fmt.Sprintf("myShort.%s", suffix)), shortcodeTempl) s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) th := testHelper{s.Cfg, s.Fs, t} @@ -90,6 +102,7 @@ p "Page Title: My Title", "My Content", "Hello World", + "Shortcode: false", ) } diff --git a/tpl/tplimpl/ace.go b/tpl/tplimpl/ace.go index fc3a1e1b1..6fb4ca439 100644 --- a/tpl/tplimpl/ace.go +++ b/tpl/tplimpl/ace.go @@ -14,6 +14,7 @@ package tplimpl import ( + "html/template" "path/filepath" "strings" @@ -24,7 +25,8 @@ import ( func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error { t.checkState() var base, inner *ace.File - name = name[:len(name)-len(filepath.Ext(innerPath))] + ".html" + withoutExt := name[:len(name)-len(filepath.Ext(innerPath))] + name = withoutExt + ".html" // Fixes issue #1178 basePath = strings.Replace(basePath, "\\", "/", -1) @@ -37,15 +39,29 @@ func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseC base = ace.NewFile(innerPath, innerContent) inner = ace.NewFile("", []byte{}) } + parsed, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil) if err != nil { t.errors = append(t.errors, &templateErr{name: name, err: err}) return err } + templ, err := ace.CompileResultWithTemplate(t.html.t.New(name), parsed, nil) if err != nil { t.errors = append(t.errors, &templateErr{name: name, err: err}) return err } - return applyTemplateTransformersToHMLTTemplate(templ) + + if err := applyTemplateTransformersToHMLTTemplate(templ); err != nil { + return err + } + + if strings.Contains(name, "shortcodes") { + // We need to keep track of one ot the output format's shortcode template + // without knowing the rendering context. + clone := template.Must(templ.Clone()) + t.html.t.AddParseTree(withoutExt, clone.Tree) + } + + return nil } diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 46484ed83..e13dd0aaa 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -622,7 +622,8 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e switch ext { case ".amber": // Only HTML support for Amber - templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html" + withoutExt := strings.TrimSuffix(name, filepath.Ext(name)) + templateName := withoutExt + ".html" b, err := afero.ReadFile(t.Fs.Source, path) if err != nil { @@ -636,7 +637,19 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e return err } - return applyTemplateTransformersToHMLTTemplate(templ) + if err := applyTemplateTransformersToHMLTTemplate(templ); err != nil { + return err + } + + if strings.Contains(templateName, "shortcodes") { + // We need to keep track of one ot the output format's shortcode template + // without knowing the rendering context. + clone := template.Must(templ.Clone()) + t.html.t.AddParseTree(withoutExt, clone.Tree) + } + + return nil + case ".ace": // Only HTML support for Ace var innerContent, baseContent []byte |