mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
Fix Params case handling in the index, sort and where func
This means that you can now do: ``` {{ range where .Site.Pages "Params.MYPARAM" "foo" }} ```
This commit is contained in:
@@ -61,7 +61,7 @@ angledQuotes = false
|
||||
hrefTargetBlank = false
|
||||
[Languages.en.Colors]
|
||||
BLUE = "blues"
|
||||
yellow = "golden"
|
||||
Yellow = "golden"
|
||||
`
|
||||
caseMixingPage1En = `
|
||||
---
|
||||
@@ -137,18 +137,6 @@ func TestCaseInsensitiveConfigurationVariations(t *testing.T) {
|
||||
|
||||
c := qt.New(t)
|
||||
|
||||
// See issues 2615, 1129, 2590 and maybe some others
|
||||
// Also see 2598
|
||||
//
|
||||
// Viper is now, at least for the Hugo part, case insensitive
|
||||
// So we need tests for all of it, with needed adjustments on the Hugo side.
|
||||
// Not sure what that will be. Let us see.
|
||||
|
||||
// So all the below with case variations:
|
||||
// config: regular fields, blackfriday config, param with nested map
|
||||
// language: new and overridden values, in regular fields and nested paramsmap
|
||||
// page frontmatter: regular fields, blackfriday config, param with nested map
|
||||
|
||||
mm := afero.NewMemMapFs()
|
||||
|
||||
caseMixingTestsWriteCommonSources(t, mm)
|
||||
@@ -168,17 +156,27 @@ Block Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}
|
||||
{{ define "main"}}
|
||||
Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }}
|
||||
Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }}
|
||||
{{ template "index-color" (dict "name" "Page" "params" .Params) }}
|
||||
{{ template "index-color" (dict "name" "Site" "params" .Site.Params) }}
|
||||
|
||||
{{ .Content }}
|
||||
{{ partial "partial.html" . }}
|
||||
{{ end }}
|
||||
{{ define "index-color" }}
|
||||
{{ $yellow := index .params "COLoRS" "yELLOW" }}
|
||||
{{ $colors := index .params "COLoRS" }}
|
||||
{{ $yellow2 := index $colors "yEllow" }}
|
||||
index1|{{ .name }}: {{ $yellow }}|
|
||||
index2|{{ .name }}: {{ $yellow2 }}|
|
||||
{{ end }}
|
||||
`)
|
||||
|
||||
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `
|
||||
Page Title: {{ .Title }}
|
||||
Site Title: {{ .Site.Title }}
|
||||
Site Lang Mood: {{ .Site.Language.Params.MOoD }}
|
||||
Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}
|
||||
Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}
|
||||
Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}|{{ index .Params "ColOR" }}
|
||||
Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}|{{ index .Site.Params "ColOR" }}
|
||||
{{ $page2 := .Site.GetPage "/sect2/page2" }}
|
||||
{{ if $page2 }}
|
||||
Page2: {{ $page2.Params.ColoR }}
|
||||
@@ -200,8 +198,8 @@ Page2: {{ $page2.Params.ColoR }}
|
||||
}
|
||||
|
||||
th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"),
|
||||
"Page Colors: red|heavenly",
|
||||
"Site Colors: green|yellow",
|
||||
"Page Colors: red|heavenly|red",
|
||||
"Site Colors: green|yellow|green",
|
||||
"Site Lang Mood: Happy",
|
||||
"Shortcode Page: red|heavenly",
|
||||
"Shortcode Site: green|yellow",
|
||||
@@ -230,6 +228,10 @@ Page2: {{ $page2.Params.ColoR }}
|
||||
"Block Page Colors: black|sky",
|
||||
"Partial Page: black|sky",
|
||||
"Partial Site: green|yellow",
|
||||
"index1|Page: flower|",
|
||||
"index1|Site: yellow|",
|
||||
"index2|Page: flower|",
|
||||
"index2|Site: yellow|",
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -228,7 +228,7 @@ func (p *pageMeta) Param(key interface{}) (interface{}, error) {
|
||||
return resource.Param(p, p.s.Info.Params(), key)
|
||||
}
|
||||
|
||||
func (p *pageMeta) Params() map[string]interface{} {
|
||||
func (p *pageMeta) Params() maps.Params {
|
||||
return p.params
|
||||
}
|
||||
|
||||
@@ -312,7 +312,7 @@ func (pm *pageMeta) setMetadata(bucket *pagesMapBucket, p *pageState, frontmatte
|
||||
return errors.New("missing frontmatter data")
|
||||
}
|
||||
|
||||
pm.params = make(map[string]interface{})
|
||||
pm.params = make(maps.Params)
|
||||
|
||||
if frontmatter != nil {
|
||||
// Needed for case insensitive fetching of params values
|
||||
@@ -320,7 +320,7 @@ func (pm *pageMeta) setMetadata(bucket *pagesMapBucket, p *pageState, frontmatte
|
||||
if p.IsNode() {
|
||||
// Check for any cascade define on itself.
|
||||
if cv, found := frontmatter["cascade"]; found {
|
||||
cvm := cast.ToStringMap(cv)
|
||||
cvm := maps.ToStringMap(cv)
|
||||
if bucket.cascade == nil {
|
||||
bucket.cascade = cvm
|
||||
} else {
|
||||
@@ -479,7 +479,7 @@ func (pm *pageMeta) setMetadata(bucket *pagesMapBucket, p *pageState, frontmatte
|
||||
}
|
||||
pm.params[loki] = pm.aliases
|
||||
case "sitemap":
|
||||
p.m.sitemap = config.DecodeSitemap(p.s.siteCfg.sitemap, cast.ToStringMap(v))
|
||||
p.m.sitemap = config.DecodeSitemap(p.s.siteCfg.sitemap, maps.ToStringMap(v))
|
||||
pm.params[loki] = p.m.sitemap
|
||||
sitemapSet = true
|
||||
case "iscjklanguage":
|
||||
@@ -495,7 +495,7 @@ func (pm *pageMeta) setMetadata(bucket *pagesMapBucket, p *pageState, frontmatte
|
||||
switch vv := v.(type) {
|
||||
case []map[interface{}]interface{}:
|
||||
for _, vvv := range vv {
|
||||
resources = append(resources, cast.ToStringMap(vvv))
|
||||
resources = append(resources, maps.ToStringMap(vvv))
|
||||
}
|
||||
case []map[string]interface{}:
|
||||
resources = append(resources, vv...)
|
||||
@@ -503,7 +503,7 @@ func (pm *pageMeta) setMetadata(bucket *pagesMapBucket, p *pageState, frontmatte
|
||||
for _, vvv := range vv {
|
||||
switch vvvv := vvv.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
resources = append(resources, cast.ToStringMap(vvvv))
|
||||
resources = append(resources, maps.ToStringMap(vvvv))
|
||||
case map[string]interface{}:
|
||||
resources = append(resources, vvvv)
|
||||
}
|
||||
@@ -642,7 +642,7 @@ func (p *pageMeta) applyDefaultValues() error {
|
||||
var renderingConfigOverrides map[string]interface{}
|
||||
bfParam := getParamToLower(p, "blackfriday")
|
||||
if bfParam != nil {
|
||||
renderingConfigOverrides = cast.ToStringMap(bfParam)
|
||||
renderingConfigOverrides = maps.ToStringMap(bfParam)
|
||||
}
|
||||
|
||||
cp := p.s.ContentSpec.Converters.Get(p.markup)
|
||||
@@ -705,14 +705,9 @@ func getParam(m resource.ResourceParamsProvider, key string, stringToLower bool)
|
||||
return helpers.SliceToLower(val)
|
||||
}
|
||||
return v
|
||||
case map[string]interface{}: // JSON and TOML
|
||||
return v
|
||||
case map[interface{}]interface{}: // YAML
|
||||
default:
|
||||
return v
|
||||
}
|
||||
|
||||
//p.s.Log.ERROR.Printf("GetParam(\"%s\"): Unknown type %s\n", key, reflect.TypeOf(v))
|
||||
return nil
|
||||
}
|
||||
|
||||
func getParamToLower(m resource.ResourceParamsProvider, key string) interface{} {
|
||||
|
@@ -1573,7 +1573,8 @@ baseURL = "https://example.org"
|
||||
{{ $withStringParam := .Site.GetPage "withstringparam" }}
|
||||
|
||||
Author page: {{ $withParam.Param "author.name" }}
|
||||
Author page string: {{ $withStringParam.Param "author.name" }}|
|
||||
Author name page string: {{ $withStringParam.Param "author.name" }}|
|
||||
Author page string: {{ $withStringParam.Param "author" }}|
|
||||
Author site config: {{ $noParam.Param "author.name" }}
|
||||
|
||||
`,
|
||||
@@ -1603,8 +1604,10 @@ author = "Jo Nesbø"
|
||||
`)
|
||||
b.Build(BuildCfg{})
|
||||
|
||||
b.AssertFileContent("public/index.html", "Author page: Ernest Miller Hemingway")
|
||||
b.AssertFileContent("public/index.html", "Author page string: |")
|
||||
b.AssertFileContent("public/index.html", "Author site config: Kurt Vonnegut")
|
||||
b.AssertFileContent("public/index.html",
|
||||
"Author page: Ernest Miller Hemingway",
|
||||
"Author name page string: Kurt Vonnegut|",
|
||||
"Author page string: Jo Nesbø|",
|
||||
"Author site config: Kurt Vonnegut")
|
||||
|
||||
}
|
||||
|
@@ -20,6 +20,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gohugoio/hugo/common/maps"
|
||||
|
||||
radix "github.com/armon/go-radix"
|
||||
"github.com/spf13/cast"
|
||||
|
||||
@@ -359,7 +361,7 @@ func (m *pagesMap) cleanKey(key string) string {
|
||||
|
||||
func (m *pagesMap) mergeCascades(b1, b2 *pagesMapBucket) {
|
||||
if b1.cascade == nil {
|
||||
b1.cascade = make(map[string]interface{})
|
||||
b1.cascade = make(maps.Params)
|
||||
}
|
||||
if b2 != nil && b2.cascade != nil {
|
||||
for k, v := range b2.cascade {
|
||||
|
@@ -28,6 +28,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gohugoio/hugo/resources/resource"
|
||||
|
||||
"github.com/gohugoio/hugo/markup/converter"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs/files"
|
||||
@@ -581,7 +583,7 @@ func (s *SiteInfo) Taxonomies() interface{} {
|
||||
return s.s.Taxonomies
|
||||
}
|
||||
|
||||
func (s *SiteInfo) Params() map[string]interface{} {
|
||||
func (s *SiteInfo) Params() maps.Params {
|
||||
return s.s.Language().Params()
|
||||
}
|
||||
|
||||
@@ -654,14 +656,9 @@ type SiteSocial map[string]string
|
||||
|
||||
// Param is a convenience method to do lookups in SiteInfo's Params map.
|
||||
//
|
||||
// This method is also implemented on Page and Node.
|
||||
// This method is also implemented on Page.
|
||||
func (s *SiteInfo) Param(key interface{}) (interface{}, error) {
|
||||
keyStr, err := cast.ToStringE(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyStr = strings.ToLower(keyStr)
|
||||
return s.Params()[keyStr], nil
|
||||
return resource.Param(s, nil, key)
|
||||
}
|
||||
|
||||
func (s *SiteInfo) IsMultiLingual() bool {
|
||||
@@ -1272,7 +1269,7 @@ func (s *Site) getMenusFromConfig() navigation.Menus {
|
||||
s.Log.DEBUG.Printf("found menu: %q, in site config\n", name)
|
||||
|
||||
menuEntry := navigation.MenuEntry{Menu: name}
|
||||
ime, err := cast.ToStringMapE(entry)
|
||||
ime, err := maps.ToStringMapE(entry)
|
||||
if err != nil {
|
||||
s.Log.ERROR.Printf("unable to process menus in site config\n")
|
||||
s.Log.ERROR.Println(err)
|
||||
|
Reference in New Issue
Block a user