mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 17:33:31 +00:00
parent
b3daa1f4bf
commit
503ca6de6c
3 changed files with 57 additions and 15 deletions
|
@ -32,6 +32,7 @@ func TestAllTemplateEngines(t *testing.T) {
|
||||||
amberFixer := func(s string) string {
|
amberFixer := func(s string) string {
|
||||||
fixed := strings.Replace(s, "{{ .Title", "{{ Title", -1)
|
fixed := strings.Replace(s, "{{ .Title", "{{ Title", -1)
|
||||||
fixed = strings.Replace(fixed, ".Content", "Content", -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, "}}", "}", -1)
|
fixed = strings.Replace(fixed, "}}", "}", -1)
|
||||||
fixed = strings.Replace(fixed, `title "hello world"`, `title("hello world")`, -1)
|
fixed = strings.Replace(fixed, `title "hello world"`, `title("hello world")`, -1)
|
||||||
|
@ -47,8 +48,10 @@ func TestAllTemplateEngines(t *testing.T) {
|
||||||
{"html", noOp},
|
{"html", noOp},
|
||||||
{"ace", 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()
|
cfg, fs := newTestCfg()
|
||||||
|
|
||||||
writeSource(t, fs, filepath.Join("content", "p.md"), `
|
|
||||||
---
|
|
||||||
title: My Title
|
|
||||||
---
|
|
||||||
My Content
|
|
||||||
`)
|
|
||||||
|
|
||||||
t.Log("Testing", suffix)
|
t.Log("Testing", suffix)
|
||||||
|
|
||||||
templTemplate := `
|
templTemplate := `
|
||||||
|
@ -77,11 +73,27 @@ p
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
templ := templateFixer(templTemplate)
|
templShortcodeTemplate := `
|
||||||
|
p
|
||||||
|
|
|
||||||
|
| Shortcode: {{ .IsNamedParams }}
|
||||||
|
`
|
||||||
|
|
||||||
t.Log(templ)
|
templ := templateFixer(templTemplate)
|
||||||
|
shortcodeTempl := templateFixer(templShortcodeTemplate)
|
||||||
|
|
||||||
|
writeSource(t, fs, filepath.Join("content", "p.md"), `
|
||||||
|
---
|
||||||
|
title: My Title
|
||||||
|
---
|
||||||
|
My Content
|
||||||
|
|
||||||
|
Shortcode: {{< myShort >}}
|
||||||
|
|
||||||
|
`)
|
||||||
|
|
||||||
writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ)
|
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{})
|
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
|
||||||
th := testHelper{s.Cfg, s.Fs, t}
|
th := testHelper{s.Cfg, s.Fs, t}
|
||||||
|
@ -90,6 +102,7 @@ p
|
||||||
"Page Title: My Title",
|
"Page Title: My Title",
|
||||||
"My Content",
|
"My Content",
|
||||||
"Hello World",
|
"Hello World",
|
||||||
|
"Shortcode: false",
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
package tplimpl
|
package tplimpl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"html/template"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -24,7 +25,8 @@ import (
|
||||||
func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error {
|
func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error {
|
||||||
t.checkState()
|
t.checkState()
|
||||||
var base, inner *ace.File
|
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
|
// Fixes issue #1178
|
||||||
basePath = strings.Replace(basePath, "\\", "/", -1)
|
basePath = strings.Replace(basePath, "\\", "/", -1)
|
||||||
|
@ -37,15 +39,29 @@ func (t *templateHandler) addAceTemplate(name, basePath, innerPath string, baseC
|
||||||
base = ace.NewFile(innerPath, innerContent)
|
base = ace.NewFile(innerPath, innerContent)
|
||||||
inner = ace.NewFile("", []byte{})
|
inner = ace.NewFile("", []byte{})
|
||||||
}
|
}
|
||||||
|
|
||||||
parsed, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil)
|
parsed, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
templ, err := ace.CompileResultWithTemplate(t.html.t.New(name), parsed, nil)
|
templ, err := ace.CompileResultWithTemplate(t.html.t.New(name), parsed, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
||||||
return 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -622,7 +622,8 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".amber":
|
case ".amber":
|
||||||
// Only HTML support for 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)
|
b, err := afero.ReadFile(t.Fs.Source, path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -636,7 +637,19 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
|
||||||
return err
|
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":
|
case ".ace":
|
||||||
// Only HTML support for Ace
|
// Only HTML support for Ace
|
||||||
var innerContent, baseContent []byte
|
var innerContent, baseContent []byte
|
||||||
|
|
Loading…
Add table
Reference in a new issue