diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-27 20:43:49 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-02 23:13:10 +0200 |
commit | 8b5b558bb515e80da640f5e114169874771b61e4 (patch) | |
tree | 5755e77efe3120963d012dccbd91d916180485e0 /hugolib/shortcode.go | |
parent | 27610ddd011e8172d00e02275f948c3f1ed43e4f (diff) |
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
Diffstat (limited to 'hugolib/shortcode.go')
-rw-r--r-- | hugolib/shortcode.go | 16 |
1 files changed, 7 insertions, 9 deletions
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) |