Skip to content

Commit

Permalink
tpl/tplimpl: Reimplement the ".Params tolower" template transformer
Browse files Browse the repository at this point in the history
All `.Params` are stored lowercase, but it should work to access them `.Page.camelCase` etc. There was, however, some holes in the logic with the old transformer.

This commit fixes that by applying a blacklist instead of the old whitelist logic. `.Param` is a very distinct key. The original case will be kept in `.Data.Params.myParam`, but other than that it will be lowercased.

Fixes gohugoio#5068
  • Loading branch information
bep committed Aug 14, 2018
1 parent 62a2dd2 commit da43ca0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
60 changes: 21 additions & 39 deletions tpl/tplimpl/template_ast_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (c *templateContext) updateIdentsIfNeeded(idents []string) {
for i := index; i < len(idents); i++ {
idents[i] = strings.ToLower(idents[i])
}

}

// indexOfReplacementStart will return the index of where to start doing replacement,
Expand All @@ -187,7 +188,7 @@ func (d decl) indexOfReplacementStart(idents []string) int {

var lookFurther bool
for _, ident := range idents {
if ident == "Params" || ident[0] == '$' {
if ident == paramsIdentifier || ident[0] == '$' {
lookFurther = true
break
}
Expand Down Expand Up @@ -256,15 +257,14 @@ func (d decl) indexOfReplacementStart(idents []string) int {
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
}
if i > 0 {
container := resolvedIdents[i-1]
if reservedContainers[container] {
break
}
paramIdx = i
}
paramIdx = i

break
}
}
Expand All @@ -273,44 +273,26 @@ func (d decl) indexOfReplacementStart(idents []string) int {
return -1
}

return len(idents) - 1

}

func indexOfFirstRealIdentAfterWords(resolvedIdents, idents []string, words ...string) int {
if !sliceStartsWith(resolvedIdents, words...) {
return -1
}

var paramSeen bool
idx := -1
for i, ident := range idents {
if ident == "" || ident[0] == '$' {
continue
}
found := true
for _, word := range words {
if ident == word {
found = false
break
if ident == paramsIdentifier {
paramSeen = true
idx = -1
} else {
if paramSeen {
return i
}
if idx == -1 {
idx = i
}
}
if found {
return i
}
}

return -1
}

func sliceStartsWith(slice []string, words ...string) bool {

if len(slice) < len(words) {
return false
}

for i, word := range words {
if word != slice[i] {
return false
}
}
return true
return idx

}
22 changes: 20 additions & 2 deletions tpl/tplimpl/template_ast_transformers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
"Slice": []int{1, 3},
"Params": map[string]interface{}{
"lower": "P1L",
"slice": []int{1, 3},
},
"CurrentSection": map[string]interface{}{
"Params": map[string]interface{}{
Expand Down Expand Up @@ -115,6 +116,8 @@ RANGE: {{ . }}: {{ $.Params.LOWER }}
F1: {{ printf "themes/%s-theme" .Site.Params.LOWER }}
F2: {{ Echo (printf "themes/%s-theme" $lower) }}
F3: {{ Echo (printf "themes/%s-theme" .Site.Params.LOWER) }}
PSLICE: {{ range .Params.SLICE }}PSLICE{{.}}|{{ end }}
`
)

Expand Down Expand Up @@ -170,7 +173,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, "PSLICE: PSLICE1|PSLICE3|")

// Issue #5068
require.Contains(t, result, "PCurrentSection: pcurrentsection")

}
Expand Down Expand Up @@ -199,13 +204,16 @@ func BenchmarkTemplateParamsKeysToLower(b *testing.B) {
}
}

func TestParamsKeysToLowVars(t *testing.T) {
func TestParamsKeysToLowerVars(t *testing.T) {
t.Parallel()
var (
ctx = map[string]interface{}{
"Params": map[string]interface{}{
"colors": map[string]interface{}{
"blue": "Amber",
"pretty": map[string]interface{}{
"first": "Indigo",
},
},
},
}
Expand All @@ -214,8 +222,14 @@ func TestParamsKeysToLowVars(t *testing.T) {
paramsTempl = `
{{$__amber_1 := .Params.Colors}}
{{$__amber_2 := $__amber_1.Blue}}
{{$__amber_3 := $__amber_1.Pretty}}
{{$__amber_4 := .Params}}
Color: {{$__amber_2}}
Blue: {{ $__amber_1.Blue}}
Pretty First1: {{ $__amber_3.First}}
Pretty First2: {{ $__amber_1.Pretty.First}}
Pretty First3: {{ $__amber_4.COLORS.PRETTY.FIRST}}
`
)

Expand All @@ -234,6 +248,10 @@ Blue: {{ $__amber_1.Blue}}
result := b.String()

require.Contains(t, result, "Color: Amber")
require.Contains(t, result, "Blue: Amber")
require.Contains(t, result, "Pretty First1: Indigo")
require.Contains(t, result, "Pretty First2: Indigo")
require.Contains(t, result, "Pretty First3: Indigo")

}

Expand Down

0 comments on commit da43ca0

Please sign in to comment.