From aa6b1b9be7c9d7322333893b642aaf8c7a5f2c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 2 Jul 2017 10:46:28 +0200 Subject: output: Support templates per site/language This applies to both regular templates and shortcodes. So, if the site language is French and the output format is AMP, this is the (start) of the lookup order for the home page: 1. index.fr.amp.html 2. index.amp.html 3. index.fr.html 4. index.html 5. ... Fixes #3360 --- output/docshelper.go | 1 + output/layout.go | 84 ++++++++++++++++++++++++++++++++++----------------- output/layout_test.go | 2 ++ 3 files changed, 59 insertions(+), 28 deletions(-) (limited to 'output') diff --git a/output/docshelper.go b/output/docshelper.go index 1df45726c..c45e956e6 100644 --- a/output/docshelper.go +++ b/output/docshelper.go @@ -44,6 +44,7 @@ func createLayoutExamples() interface{} { f Format }{ {`AMP home, with theme "demoTheme".`, LayoutDescriptor{Kind: "home"}, true, "", AMPFormat}, + {`AMP home, French language".`, LayoutDescriptor{Kind: "home", Lang: "fr"}, false, "", AMPFormat}, {"JSON home, no theme.", LayoutDescriptor{Kind: "home"}, false, "", JSONFormat}, {fmt.Sprintf(`CSV regular, "layout: %s" in front matter.`, demoLayout), LayoutDescriptor{Kind: "page", Layout: demoLayout}, false, "", CSVFormat}, {fmt.Sprintf(`JSON regular, "type: %s" in front matter.`, demoType), LayoutDescriptor{Kind: "page", Type: demoType}, false, "", JSONFormat}, diff --git a/output/layout.go b/output/layout.go index cacb92b80..6c054b6c4 100644 --- a/output/layout.go +++ b/output/layout.go @@ -26,6 +26,7 @@ type LayoutDescriptor struct { Type string Section string Kind string + Lang string Layout string } @@ -55,31 +56,33 @@ func NewLayoutHandler(hasTheme bool) *LayoutHandler { const ( + // TODO(bep) variations reduce to 1 "." + // The RSS templates doesn't map easily into the regular pages. - layoutsRSSHome = `NAME.SUFFIX _default/NAME.SUFFIX _internal/_default/rss.xml` - layoutsRSSSection = `section/SECTION.NAME.SUFFIX _default/NAME.SUFFIX NAME.SUFFIX _internal/_default/rss.xml` - layoutsRSSTaxonomy = `taxonomy/SECTION.NAME.SUFFIX _default/NAME.SUFFIX NAME.SUFFIX _internal/_default/rss.xml` - layoutsRSSTaxonomyTerm = `taxonomy/SECTION.terms.NAME.SUFFIX _default/NAME.SUFFIX NAME.SUFFIX _internal/_default/rss.xml` + layoutsRSSHome = `VARIATIONS _default/VARIATIONS _internal/_default/rss.xml` + layoutsRSSSection = `section/SECTION.VARIATIONS _default/VARIATIONS VARIATIONS _internal/_default/rss.xml` + layoutsRSSTaxonomy = `taxonomy/SECTION.VARIATIONS _default/VARIATIONS VARIATIONS _internal/_default/rss.xml` + layoutsRSSTaxonomyTerm = `taxonomy/SECTION.terms.VARIATIONS _default/VARIATIONS VARIATIONS _internal/_default/rss.xml` - layoutsHome = "index.NAME.SUFFIX index.SUFFIX _default/list.NAME.SUFFIX _default/list.SUFFIX" + layoutsHome = "index.VARIATIONS _default/list.VARIATIONS" layoutsSection = ` -section/SECTION.NAME.SUFFIX section/SECTION.SUFFIX -SECTION/list.NAME.SUFFIX SECTION/list.SUFFIX -_default/section.NAME.SUFFIX _default/section.SUFFIX -_default/list.NAME.SUFFIX _default/list.SUFFIX -indexes/SECTION.NAME.SUFFIX indexes/SECTION.SUFFIX -_default/indexes.NAME.SUFFIX _default/indexes.SUFFIX +section/SECTION.VARIATIONS +SECTION/list.VARIATIONS +_default/section.VARIATIONS +_default/list.VARIATIONS +indexes/SECTION.VARIATIONS +_default/indexes.VARIATIONS ` layoutsTaxonomy = ` -taxonomy/SECTION.NAME.SUFFIX taxonomy/SECTION.SUFFIX -indexes/SECTION.NAME.SUFFIX indexes/SECTION.SUFFIX -_default/taxonomy.NAME.SUFFIX _default/taxonomy.SUFFIX -_default/list.NAME.SUFFIX _default/list.SUFFIX +taxonomy/SECTION.VARIATIONS +indexes/SECTION.VARIATIONS +_default/taxonomy.VARIATIONS +_default/list.VARIATIONS ` layoutsTaxonomyTerm = ` -taxonomy/SECTION.terms.NAME.SUFFIX taxonomy/SECTION.terms.SUFFIX -_default/terms.NAME.SUFFIX _default/terms.SUFFIX -indexes/indexes.NAME.SUFFIX indexes/indexes.SUFFIX +taxonomy/SECTION.terms.VARIATIONS +_default/terms.VARIATIONS +indexes/indexes.VARIATIONS ` ) @@ -185,14 +188,41 @@ func resolveListTemplate(d LayoutDescriptor, f Format, } func resolveTemplate(templ string, d LayoutDescriptor, f Format) []string { - delim := "." - if f.MediaType.Delimiter == "" { - delim = "" + + // VARIATIONS will be replaced with + // .lang.name.suffix + // .name.suffix + // .lang.suffix + // .suffix + var replacementValues []string + + name := strings.ToLower(f.Name) + + if d.Lang != "" { + replacementValues = append(replacementValues, fmt.Sprintf("%s.%s.%s", d.Lang, name, f.MediaType.Suffix)) + } + + replacementValues = append(replacementValues, fmt.Sprintf("%s.%s", name, f.MediaType.Suffix)) + + if d.Lang != "" { + replacementValues = append(replacementValues, fmt.Sprintf("%s.%s", d.Lang, f.MediaType.Suffix)) + } + + isRSS := f.Name == RSSFormat.Name + + if !isRSS { + replacementValues = append(replacementValues, f.MediaType.Suffix) + } + + var layouts []string + + templFields := strings.Fields(templ) + + for _, field := range templFields { + for _, replacements := range replacementValues { + layouts = append(layouts, replaceKeyValues(field, "VARIATIONS", replacements, "SECTION", d.Section)) + } } - layouts := strings.Fields(replaceKeyValues(templ, - ".SUFFIX", delim+f.MediaType.Suffix, - "NAME", strings.ToLower(f.Name), - "SECTION", d.Section)) return filterDotLess(layouts) } @@ -201,9 +231,7 @@ func filterDotLess(layouts []string) []string { var filteredLayouts []string for _, l := range layouts { - // This may be constructed, but media types can be suffix-less, but can contain - // a delimiter. - l = strings.TrimSuffix(l, ".") + l = strings.Trim(l, ".") // If media type has no suffix, we have "index" type of layouts in this list, which // doesn't make much sense. if strings.Contains(l, ".") { diff --git a/output/layout_test.go b/output/layout_test.go index 9d4d2f6d5..6fb958c9d 100644 --- a/output/layout_test.go +++ b/output/layout_test.go @@ -59,6 +59,8 @@ func TestLayout(t *testing.T) { }{ {"Home", LayoutDescriptor{Kind: "home"}, true, "", ampType, []string{"index.amp.html", "index.html", "_default/list.amp.html", "_default/list.html", "theme/index.amp.html", "theme/index.html"}}, + {"Home, french language", LayoutDescriptor{Kind: "home", Lang: "fr"}, true, "", ampType, + []string{"index.fr.amp.html", "index.amp.html", "index.fr.html", "index.html", "_default/list.fr.amp.html", "_default/list.amp.html", "_default/list.fr.html", "_default/list.html", "theme/index.fr.amp.html", "theme/index.amp.html", "theme/index.fr.html"}}, {"Home, no ext or delim", LayoutDescriptor{Kind: "home"}, true, "", noExtDelimFormat, []string{"index.nem", "_default/list.nem"}}, {"Home, no ext", LayoutDescriptor{Kind: "home"}, true, "", noExt, -- cgit v1.2.3