From 8b5b558bb515e80da640f5e114169874771b61e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 27 Mar 2017 20:43:49 +0200 Subject: tpl: Rework to handle both text and HTML templates Before this commit, Hugo used `html/template` for all Go templates. While this is a fine choice for HTML and maybe also RSS feeds, it is painful for plain text formats such as CSV, JSON etc. This commit fixes that by using the `IsPlainText` attribute on the output format to decide what to use. A couple of notes: * The above requires a nonambiguous template name to type mapping. I.e. `/layouts/_default/list.json` will only work if there is only one JSON output format, `/layouts/_default/list.mytype.json` will always work. * Ambiguous types will fall back to HTML. * Partials inherits the text vs HTML identificator of the container template. This also means that plain text templates can only include plain text partials. * Shortcode templates are, by definition, currently HTML templates only. Fixes #3221 --- hugolib/shortcode.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'hugolib/shortcode.go') diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index d165c778b..d72a96faa 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -177,7 +177,7 @@ var isInnerShortcodeCache = struct { // to avoid potential costly look-aheads for closing tags we look inside the template itself // we could change the syntax to self-closing tags, but that would make users cry // the value found is cached -func isInnerShortcode(t *template.Template) (bool, error) { +func isInnerShortcode(t tpl.TemplateExecutor) (bool, error) { isInnerShortcodeCache.RLock() m, ok := isInnerShortcodeCache.m[t.Name()] isInnerShortcodeCache.RUnlock() @@ -188,10 +188,7 @@ func isInnerShortcode(t *template.Template) (bool, error) { isInnerShortcodeCache.Lock() defer isInnerShortcodeCache.Unlock() - if t.Tree == nil { - return false, errors.New("Template failed to compile") - } - match, _ := regexp.MatchString("{{.*?\\.Inner.*?}}", t.Tree.Root.String()) + match, _ := regexp.MatchString("{{.*?\\.Inner.*?}}", t.Tree()) isInnerShortcodeCache.m[t.Name()] = match return match, nil @@ -398,8 +395,6 @@ Loop: case tScName: sc.name = currItem.val tmpl := getShortcodeTemplate(sc.name, p.s.Tmpl) - { - } if tmpl == nil { return sc, fmt.Errorf("Unable to locate template for shortcode %q in page %q", sc.name, p.Path()) } @@ -570,7 +565,10 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin return source, nil } -func getShortcodeTemplate(name string, t tpl.Template) *template.Template { +func getShortcodeTemplate(name string, t tpl.TemplateHandler) *tpl.TemplateAdapter { + isInnerShortcodeCache.RLock() + defer isInnerShortcodeCache.RUnlock() + if x := t.Lookup("shortcodes/" + name + ".html"); x != nil { return x } @@ -580,7 +578,7 @@ func getShortcodeTemplate(name string, t tpl.Template) *template.Template { return t.Lookup("_internal/shortcodes/" + name + ".html") } -func renderShortcodeWithPage(tmpl *template.Template, data *ShortcodeWithPage) string { +func renderShortcodeWithPage(tmpl tpl.Template, data *ShortcodeWithPage) string { buffer := bp.GetBuffer() defer bp.PutBuffer(buffer) -- cgit v1.2.3