Fix language handling in ExecuteAsTemplate

Fixes #6331
This commit is contained in:
Bjørn Erik Pedersen 2019-11-26 09:44:31 +01:00
parent 03b369e672
commit 96f09659ce
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
5 changed files with 51 additions and 26 deletions

View file

@ -1445,5 +1445,17 @@ weight: 2002
PNG Data PNG Data
`) `)
i18nContent := func(id, value string) string {
return fmt.Sprintf(`
[%s]
other = %q
`, id, value)
}
b.WithSourceFile("i18n/en.toml", i18nContent("hello", "Hello"))
b.WithSourceFile("i18n/fr.toml", i18nContent("hello", "Bonjour"))
b.WithSourceFile("i18n/nb.toml", i18nContent("hello", "Hallo"))
b.WithSourceFile("i18n/nn.toml", i18nContent("hello", "Hallo"))
return &multiSiteTestBuilder{sitesBuilder: b, configFormat: configFormat, config: config, configData: configData} return &multiSiteTestBuilder{sitesBuilder: b, configFormat: configFormat, config: config, configData: configData}
} }

View file

@ -418,8 +418,7 @@ Fingerprinted: {{ $fingerprinted.RelPermalink }}
}}, }},
{"execute-as-template", func() bool { {"execute-as-template", func() bool {
// TODO(bep) eventually remove return true
return isGo111()
}, func(b *sitesBuilder) { }, func(b *sitesBuilder) {
b.WithTemplates("home.html", ` b.WithTemplates("home.html", `
{{ $var := "Hugo Page" }} {{ $var := "Hugo Page" }}
@ -668,3 +667,30 @@ JSON: {{ $json.RelPermalink }}: {{ $json.Content }}
"JSON: /jsons/data1.json: json1 content", "JSON: /jsons/data1.json: json1 content",
"JSONS: 2", "/jsons/data1.json: json1 content") "JSONS: 2", "/jsons/data1.json: json1 content")
} }
func TestExecuteAsTemplateWithLanguage(t *testing.T) {
b := newMultiSiteTestDefaultBuilder(t)
indexContent := `
Lang: {{ site.Language.Lang }}
{{ $templ := "{{T \"hello\"}}" | resources.FromString "f1.html" }}
{{ $helloResource := $templ | resources.ExecuteAsTemplate (print "f%s.html" .Lang) . }}
Hello1: {{T "hello"}}
Hello2: {{ $helloResource.Content }}
LangURL: {{ relLangURL "foo" }}
`
b.WithTemplatesAdded("index.html", indexContent)
b.WithTemplatesAdded("index.fr.html", indexContent)
b.Build(BuildCfg{})
b.AssertFileContent("public/en/index.html", `
Hello1: Hello
Hello2: Hello
`)
b.AssertFileContent("public/fr/index.html", `
Hello1: Bonjour
Hello2: Bonjour
`)
}

View file

@ -50,8 +50,6 @@ type TemplateHandler interface {
AddLateTemplate(name, tpl string) error AddLateTemplate(name, tpl string) error
LoadTemplates(prefix string) error LoadTemplates(prefix string) error
NewTextTemplate() TemplateParseFinder
MarkReady() error MarkReady() error
RebuildClone() RebuildClone()
} }

View file

@ -98,8 +98,6 @@ type templateHandler struct {
text *textTemplates text *textTemplates
html *htmlTemplates html *htmlTemplates
extTextTemplates []*textTemplate
amberFuncMap template.FuncMap amberFuncMap template.FuncMap
errors []*templateErr errors []*templateErr
@ -153,15 +151,7 @@ func (t *templateHandler) addShortcodeVariant(name string, info tpl.Info, templ
} }
} }
// NewTextTemplate provides a text template parser that has all the Hugo func (t *templateHandler) wrapTextTemplate(tt *textTemplate) tpl.TemplateParseFinder {
// template funcs etc. built-in.
func (t *templateHandler) NewTextTemplate() tpl.TemplateParseFinder {
t.mu.Lock()
defer t.mu.Unlock()
tt := &textTemplate{t: texttemplate.New("")}
t.extTextTemplates = append(t.extTextTemplates, tt)
return struct { return struct {
tpl.TemplateParser tpl.TemplateParser
tpl.TemplateLookup tpl.TemplateLookup
@ -283,7 +273,10 @@ func (t *templateHandler) clone(d *deps.Deps) *templateHandler {
shortcodes: make(map[string]*shortcodeTemplates), shortcodes: make(map[string]*shortcodeTemplates),
templateInfo: t.templateInfo, templateInfo: t.templateInfo,
html: &htmlTemplates{t: template.Must(t.html.t.Clone()), overlays: make(map[string]*template.Template), templatesCommon: t.html.templatesCommon}, html: &htmlTemplates{t: template.Must(t.html.t.Clone()), overlays: make(map[string]*template.Template), templatesCommon: t.html.templatesCommon},
text: &textTemplates{textTemplate: &textTemplate{t: texttemplate.Must(t.text.t.Clone())}, overlays: make(map[string]*texttemplate.Template), templatesCommon: t.text.templatesCommon}, text: &textTemplates{
textTemplate: &textTemplate{t: texttemplate.Must(t.text.t.Clone())},
standalone: &textTemplate{t: texttemplate.New("")},
overlays: make(map[string]*texttemplate.Template), templatesCommon: t.text.templatesCommon},
errors: make([]*templateErr, 0), errors: make([]*templateErr, 0),
} }
@ -302,6 +295,7 @@ func (t *templateHandler) clone(d *deps.Deps) *templateHandler {
} }
d.Tmpl = c d.Tmpl = c
d.TextTmpl = c.wrapTextTemplate(c.text.standalone)
c.initFuncs() c.initFuncs()
@ -339,6 +333,7 @@ func newTemplateAdapter(deps *deps.Deps) *templateHandler {
} }
textT := &textTemplates{ textT := &textTemplates{
textTemplate: &textTemplate{t: texttemplate.New("")}, textTemplate: &textTemplate{t: texttemplate.New("")},
standalone: &textTemplate{t: texttemplate.New("")},
overlays: make(map[string]*texttemplate.Template), overlays: make(map[string]*texttemplate.Template),
templatesCommon: common, templatesCommon: common,
} }
@ -431,6 +426,7 @@ func (t *textTemplates) setTemplateFuncster(f *templateFuncster) {
type textTemplates struct { type textTemplates struct {
*templatesCommon *templatesCommon
*textTemplate *textTemplate
standalone *textTemplate
clone *texttemplate.Template clone *texttemplate.Template
cloneClone *texttemplate.Template cloneClone *texttemplate.Template
@ -468,6 +464,7 @@ func (t *textTemplates) lookup(name string) *texttemplate.Template {
func (t *templateHandler) setFuncs(funcMap map[string]interface{}) { func (t *templateHandler) setFuncs(funcMap map[string]interface{}) {
t.html.setFuncs(funcMap) t.html.setFuncs(funcMap)
t.text.setFuncs(funcMap) t.text.setFuncs(funcMap)
t.setFuncMapInTemplate(t.text.standalone.t, funcMap)
} }
// SetFuncs replaces the funcs in the func maps with new definitions. // SetFuncs replaces the funcs in the func maps with new definitions.
@ -781,10 +778,6 @@ func (t *templateHandler) initFuncs() {
} }
} }
for _, extText := range t.extTextTemplates {
extText.t.Funcs(funcMap)
}
// Amber is HTML only. // Amber is HTML only.
t.amberFuncMap = template.FuncMap{} t.amberFuncMap = template.FuncMap{}

View file

@ -26,11 +26,9 @@ var DefaultTemplateProvider *TemplateProvider
// Update updates the Hugo Template System in the provided Deps // Update updates the Hugo Template System in the provided Deps
// with all the additional features, templates & functions. // with all the additional features, templates & functions.
func (*TemplateProvider) Update(deps *deps.Deps) error { func (*TemplateProvider) Update(deps *deps.Deps) error {
newTmpl := newTemplateAdapter(deps) newTmpl := newTemplateAdapter(deps)
deps.Tmpl = newTmpl deps.Tmpl = newTmpl
deps.TextTmpl = newTmpl.wrapTextTemplate(newTmpl.text.standalone)
deps.TextTmpl = newTmpl.NewTextTemplate()
newTmpl.initFuncs() newTmpl.initFuncs()
@ -56,8 +54,6 @@ func (*TemplateProvider) Clone(d *deps.Deps) error {
t := d.Tmpl.(*templateHandler) t := d.Tmpl.(*templateHandler)
clone := t.clone(d) clone := t.clone(d)
d.Tmpl = clone
return clone.MarkReady() return clone.MarkReady()
} }