mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
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
This commit is contained in:
@@ -18,6 +18,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"fmt"
|
||||
@@ -75,6 +77,21 @@ disableKinds = ["page", "section", "taxonomy", "taxonomyTerm", "RSS", "sitemap",
|
||||
[Taxonomies]
|
||||
tag = "tags"
|
||||
category = "categories"
|
||||
|
||||
defaultContentLanguage = "en"
|
||||
|
||||
[languages]
|
||||
|
||||
[languages.en]
|
||||
title = "Title in English"
|
||||
languageName = "English"
|
||||
weight = 1
|
||||
|
||||
[languages.nn]
|
||||
languageName = "Nynorsk"
|
||||
weight = 2
|
||||
title = "Tittel på Nynorsk"
|
||||
|
||||
`
|
||||
|
||||
pageTemplate := `---
|
||||
@@ -84,27 +101,59 @@ outputs: %s
|
||||
# Doc
|
||||
`
|
||||
|
||||
th, h := newTestSitesFromConfig(t, siteConfig,
|
||||
"layouts/_default/list.json", `List JSON|{{ .Title }}|{{ .Content }}|Alt formats: {{ len .AlternativeOutputFormats -}}|
|
||||
mf := afero.NewMemMapFs()
|
||||
|
||||
writeToFs(t, mf, "i18n/en.toml", `
|
||||
[elbow]
|
||||
other = "Elbow"
|
||||
`)
|
||||
writeToFs(t, mf, "i18n/nn.toml", `
|
||||
[elbow]
|
||||
other = "Olboge"
|
||||
`)
|
||||
|
||||
th, h := newTestSitesFromConfig(t, mf, siteConfig,
|
||||
|
||||
"layouts/_default/baseof.json", `START JSON:{{block "main" .}}default content{{ end }}:END JSON`,
|
||||
"layouts/_default/baseof.html", `START HTML:{{block "main" .}}default content{{ end }}:END HTML`,
|
||||
|
||||
"layouts/_default/list.json", `{{ define "main" }}
|
||||
List JSON|{{ .Title }}|{{ .Content }}|Alt formats: {{ len .AlternativeOutputFormats -}}|
|
||||
{{- range .AlternativeOutputFormats -}}
|
||||
Alt Output: {{ .Name -}}|
|
||||
{{- end -}}|
|
||||
{{- range .OutputFormats -}}
|
||||
Output/Rel: {{ .Name -}}/{{ .Rel }}|
|
||||
Output/Rel: {{ .Name -}}/{{ .Rel }}|{{ .MediaType }}
|
||||
{{- end -}}
|
||||
{{ with .OutputFormats.Get "JSON" }}
|
||||
<atom:link href={{ .Permalink }} rel="self" type="{{ .MediaType }}" />
|
||||
{{ end }}
|
||||
{{ .Site.Language.Lang }}: {{ T "elbow" -}}
|
||||
{{ end }}
|
||||
`,
|
||||
"layouts/_default/list.html", `{{ define "main" }}
|
||||
List HTML|{{.Title }}|
|
||||
{{- with .OutputFormats.Get "HTML" -}}
|
||||
<atom:link href={{ .Permalink }} rel="self" type="{{ .MediaType }}" />
|
||||
{{- end -}}
|
||||
{{ .Site.Language.Lang }}: {{ T "elbow" -}}
|
||||
{{ end }}
|
||||
`,
|
||||
)
|
||||
require.Len(t, h.Sites, 1)
|
||||
require.Len(t, h.Sites, 2)
|
||||
|
||||
fs := th.Fs
|
||||
|
||||
writeSource(t, fs, "content/_index.md", fmt.Sprintf(pageTemplate, "JSON Home", outputsStr))
|
||||
writeSource(t, fs, "content/_index.nn.md", fmt.Sprintf(pageTemplate, "JSON Nynorsk Heim", outputsStr))
|
||||
|
||||
err := h.Build(BuildCfg{})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
s := h.Sites[0]
|
||||
require.Equal(t, "en", s.Language.Lang)
|
||||
|
||||
home := s.getPage(KindHome)
|
||||
|
||||
require.NotNil(t, home)
|
||||
@@ -113,7 +162,6 @@ Output/Rel: {{ .Name -}}/{{ .Rel }}|
|
||||
|
||||
require.Len(t, home.outputFormats, lenOut)
|
||||
|
||||
// TODO(bep) output assert template/text
|
||||
// There is currently always a JSON output to make it simpler ...
|
||||
altFormats := lenOut - 1
|
||||
hasHTML := helpers.InStringArray(outputs, "html")
|
||||
@@ -127,10 +175,27 @@ Output/Rel: {{ .Name -}}/{{ .Rel }}|
|
||||
"Alt Output: HTML",
|
||||
"Output/Rel: JSON/alternate|",
|
||||
"Output/Rel: HTML/canonical|",
|
||||
"en: Elbow",
|
||||
)
|
||||
|
||||
th.assertFileContent("public/index.html",
|
||||
// The HTML entity is a deliberate part of this test: The HTML templates are
|
||||
// parsed with html/template.
|
||||
`List HTML|JSON Home|<atom:link href=http://example.com/blog/ rel="self" type="text/html+html" />`,
|
||||
"en: Elbow",
|
||||
)
|
||||
th.assertFileContent("public/nn/index.html",
|
||||
"List HTML|JSON Nynorsk Heim|",
|
||||
"nn: Olboge")
|
||||
} else {
|
||||
th.assertFileContent("public/index.json",
|
||||
"Output/Rel: JSON/canonical|",
|
||||
// JSON is plain text, so no need to safeHTML this and that
|
||||
`<atom:link href=http://example.com/blog/index.json rel="self" type="application/json+json" />`,
|
||||
)
|
||||
th.assertFileContent("public/nn/index.json",
|
||||
"List JSON|JSON Nynorsk Heim|",
|
||||
"nn: Olboge",
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user