From e8629399bbdee599a80ad685640af89c36262995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 13 Aug 2018 11:01:57 +0200 Subject: [PATCH] Fix Resource output in multihost setups In Hugo 0.46 we made the output of what you get from resources.Get and similar static, i.e. language agnostic. This makes total sense, as it is wasteful and time-consuming to do SASS/SCSS/PostCSS processing for lots of languages when the output is lots of duplicates with different filenames. But since we now output the result once only, this had a negative side effect for multihost setups: We publish the resource once only to the root folder (i.e. not to the language "domain folder"). This commit removes the language code from the processed image keys. This creates less duplication in the file cache, but it means that you should do a `hugo --gc` to clean up stale files. Fixes #5058 --- tpl/tplimpl/template_ast_transformers.go | 53 +++++++++++++------ tpl/tplimpl/template_ast_transformers_test.go | 11 +++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go index bbd0f28a46f..cfdc35593a8 100644 --- a/tpl/tplimpl/template_ast_transformers.go +++ b/tpl/tplimpl/template_ast_transformers.go @@ -24,11 +24,21 @@ import ( // decl keeps track of the variable mappings, i.e. $mysite => .Site etc. type decl map[string]string +const ( + paramsIdentifier = "Params" +) + +// Containers that may contain Params that we will not touch. +var reservedContainers = map[string]bool{ + // Aka .Site.Data.Params which must stay case sensitive. + "Data": true, +} + var paramsPaths = [][]string{ {"Params"}, {"Site", "Params"}, - // Site and Pag referenced from shortcodes + // Site and Page referenced from shortcodes {"Page", "Site", "Params"}, {"Page", "Params"}, @@ -175,19 +185,18 @@ func (d decl) indexOfReplacementStart(idents []string) int { return -1 } - if !firstIsVar { - found := false - for _, paramsPath := range paramsPaths { - if first == paramsPath[0] { - found = true - break - } - } - if !found { - return -1 + var lookFurther bool + for _, ident := range idents { + if ident == "Params" || ident[0] == '$' { + lookFurther = true + break } } + if !lookFurther { + return -1 + } + var ( resolvedIdents []string replacements []string @@ -244,13 +253,27 @@ func (d decl) indexOfReplacementStart(idents []string) int { resolvedIdents = append(replaced, idents[1:]...) - for _, paramPath := range paramsPaths { - if index := indexOfFirstRealIdentAfterWords(resolvedIdents, idents, paramPath...); index != -1 { - return index + paramIdx := -1 + for i, ident := range resolvedIdents { + if ident == paramsIdentifier { + if i == len(resolvedIdents)-2 { + if i > 0 { + container := resolvedIdents[i-1] + if reservedContainers[container] { + break + } + } + paramIdx = i + } + break } } - return -1 + if paramIdx == -1 { + return -1 + } + + return len(idents) - 1 } diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go index c3cf549407c..2b8d85f0963 100644 --- a/tpl/tplimpl/template_ast_transformers_test.go +++ b/tpl/tplimpl/template_ast_transformers_test.go @@ -32,6 +32,11 @@ var ( "Params": map[string]interface{}{ "lower": "P1L", }, + "CurrentSection": map[string]interface{}{ + "Params": map[string]interface{}{ + "lower": "pcurrentsection", + }, + }, "Site": map[string]interface{}{ "Params": map[string]interface{}{ "lower": "P2L", @@ -58,6 +63,7 @@ var ( {{ $data := .Site.Data }} {{ $notparam := .NotParam }} +PCurrentSection: {{ .CurrentSection.Params.LOWER }} P1: {{ .Params.LOWER }} P1_2: {{ $.Params.LOWER }} P1_3: {{ $page.Params.LOWER }} @@ -164,6 +170,9 @@ func TestParamsKeysToLower(t *testing.T) { require.Contains(t, result, "F2: themes/P2L-theme") require.Contains(t, result, "F3: themes/P2L-theme") + // Issue #5068 and similar + require.Contains(t, result, "PCurrentSection: pcurrentsection") + } func BenchmarkTemplateParamsKeysToLower(b *testing.B) { @@ -190,7 +199,7 @@ func BenchmarkTemplateParamsKeysToLower(b *testing.B) { } } -func TestParamsKeysToLowerVars(t *testing.T) { +func TestParamsKeysToLowVars(t *testing.T) { t.Parallel() var ( ctx = map[string]interface{}{