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/site_output_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 5 deletions(-) (limited to 'hugolib/site_output_test.go') diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go index 86e1a55ca..0edd8e1f0 100644 --- a/hugolib/site_output_test.go +++ b/hugolib/site_output_test.go @@ -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" }} + +{{ end }} +{{ .Site.Language.Lang }}: {{ T "elbow" -}} +{{ end }} +`, + "layouts/_default/list.html", `{{ define "main" }} +List HTML|{{.Title }}| +{{- with .OutputFormats.Get "HTML" -}} + {{- 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|`, + "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 + ``, + ) + th.assertFileContent("public/nn/index.json", + "List JSON|JSON Nynorsk Heim|", + "nn: Olboge", ) } -- cgit v1.2.3