Skip to content

Commit

Permalink
Fix Resource output in multihost setups
Browse files Browse the repository at this point in the history
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 gohugoio#5058
  • Loading branch information
bep committed Aug 14, 2018
1 parent 78f8475 commit e862939
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
53 changes: 38 additions & 15 deletions tpl/tplimpl/template_ast_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

}

Expand Down
11 changes: 10 additions & 1 deletion tpl/tplimpl/template_ast_transformers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 }}
Expand Down Expand Up @@ -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) {
Expand All @@ -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{}{
Expand Down

0 comments on commit e862939

Please sign in to comment.