Skip to content

Commit

Permalink
hugolib: Do not tolower result from Page.GetParam
Browse files Browse the repository at this point in the history
We still do lowering of the param strings in some internal use of this, but the exported `GetParam` method is changed to a more sensible default.

This was used for the `disqus_title` etc. in the internal Disqus template, which was obviously not right.

If you really want to lowercase your params, do it with `.GetParam "myparam" | lower` or similar.

Fixes #4187
  • Loading branch information
bep committed Dec 29, 2017
1 parent e141294 commit 1c114d5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
6 changes: 5 additions & 1 deletion hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func (p *Page) renderContent(content []byte) []byte {

func (p *Page) getRenderingConfig() *helpers.BlackFriday {
p.renderingConfigInit.Do(func() {
bfParam := p.GetParam("blackfriday")
bfParam := p.getParamToLower("blackfriday")
if bfParam == nil {
p.renderingConfig = p.s.ContentSpec.BlackFriday
return
Expand Down Expand Up @@ -1306,6 +1306,10 @@ func (p *Page) update(f interface{}) error {
}

func (p *Page) GetParam(key string) interface{} {
return p.getParam(key, false)
}

func (p *Page) getParamToLower(key string) interface{} {
return p.getParam(key, true)
}

Expand Down
8 changes: 4 additions & 4 deletions hugolib/pageGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p Pages) GroupByParam(key string, order ...string) (PagesGroup, error) {
var tmp reflect.Value
var keyt reflect.Type
for _, e := range p {
param := e.GetParam(key)
param := e.getParamToLower(key)
if param != nil {
if _, ok := param.([]string); !ok {
keyt = reflect.TypeOf(param)
Expand Down Expand Up @@ -278,21 +278,21 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
sorter := func(p Pages) Pages {
var r Pages
for _, e := range p {
param := e.GetParam(key)
param := e.getParamToLower(key)
if param != nil {
if _, ok := param.(time.Time); ok {
r = append(r, e)
}
}
}
pdate := func(p1, p2 *Page) bool {
return p1.GetParam(key).(time.Time).Unix() < p2.GetParam(key).(time.Time).Unix()
return p1.getParamToLower(key).(time.Time).Unix() < p2.getParamToLower(key).(time.Time).Unix()
}
pageBy(pdate).Sort(r)
return r
}
formatter := func(p *Page) string {
return p.GetParam(key).(time.Time).Format(format)
return p.getParamToLower(key).(time.Time).Format(format)
}
return p.groupByDateField(sorter, formatter, order...)
}
4 changes: 2 additions & 2 deletions hugolib/page_taxonomy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestParseTaxonomies(t *testing.T) {
t.Fatalf("Failed parsing %q: %s", test, err)
}

param := p.GetParam("tags")
param := p.getParamToLower("tags")

if params, ok := param.([]string); ok {
expected := []string{"a", "b", "c"}
Expand All @@ -86,7 +86,7 @@ func TestParseTaxonomies(t *testing.T) {
}
}

param = p.GetParam("categories")
param = p.getParamToLower("categories")
singleparam := param.(string)

if singleparam != "d" {
Expand Down
22 changes: 11 additions & 11 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,22 +1067,22 @@ func TestDifferentFrontMatterVarTypes(t *testing.T) {
_, _ = page.ReadFrom(strings.NewReader(pageWithVariousFrontmatterTypes))

dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
if page.GetParam("a_string") != "bar" {
t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string"))
if page.getParamToLower("a_string") != "bar" {
t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.getParamToLower("a_string"))
}
if page.GetParam("an_integer") != 1 {
t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer"))
if page.getParamToLower("an_integer") != 1 {
t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.getParamToLower("an_integer"))
}
if page.GetParam("a_float") != 1.3 {
t.Errorf("frontmatter not handling floats correctly should be %f, got: %s", 1.3, page.GetParam("a_float"))
if page.getParamToLower("a_float") != 1.3 {
t.Errorf("frontmatter not handling floats correctly should be %f, got: %s", 1.3, page.getParamToLower("a_float"))
}
if page.GetParam("a_bool") != false {
t.Errorf("frontmatter not handling bools correctly should be %t, got: %s", false, page.GetParam("a_bool"))
if page.getParamToLower("a_bool") != false {
t.Errorf("frontmatter not handling bools correctly should be %t, got: %s", false, page.getParamToLower("a_bool"))
}
if page.GetParam("a_date") != dateval {
t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date"))
if page.getParamToLower("a_date") != dateval {
t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.getParamToLower("a_date"))
}
param := page.GetParam("a_table")
param := page.getParamToLower("a_table")
if param == nil {
t.Errorf("frontmatter not handling tables correctly should be type of %v, got: type of %v", reflect.TypeOf(page.Params["a_table"]), reflect.TypeOf(param))
}
Expand Down
2 changes: 1 addition & 1 deletion hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ func (s *Site) assembleTaxonomies() {

for _, p := range s.Pages {
vals := p.getParam(plural, !s.Info.preserveTaxonomyNames)
weight := p.GetParam(plural + "_weight")
weight := p.getParamToLower(plural + "_weight")
if weight == nil {
weight = 0
}
Expand Down

0 comments on commit 1c114d5

Please sign in to comment.