Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Page.Params more consistently when adding metadata #3033

Merged
merged 1 commit into from Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion hugolib/page.go
Expand Up @@ -919,29 +919,37 @@ func (p *Page) update(f interface{}) error {
switch loki {
case "title":
p.Title = cast.ToString(v)
p.Params[loki] = p.Title
case "linktitle":
p.linkTitle = cast.ToString(v)
p.Params[loki] = p.linkTitle
case "description":
p.Description = cast.ToString(v)
p.Params["description"] = p.Description
p.Params[loki] = p.Description
case "slug":
p.Slug = cast.ToString(v)
p.Params[loki] = p.Slug
case "url":
if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return fmt.Errorf("Only relative URLs are supported, %v provided", url)
}
p.URLPath.URL = cast.ToString(v)
p.Params[loki] = p.URLPath.URL
case "type":
p.contentType = cast.ToString(v)
p.Params[loki] = p.contentType
case "extension", "ext":
p.extension = cast.ToString(v)
p.Params[loki] = p.extension
case "keywords":
p.Keywords = cast.ToStringSlice(v)
p.Params[loki] = p.Keywords
case "date":
p.Date, err = cast.ToTimeE(v)
if err != nil {
p.s.Log.ERROR.Printf("Failed to parse date '%v' in page %s", v, p.File.Path())
}
p.Params[loki] = p.Date
case "lastmod":
p.Lastmod, err = cast.ToTimeE(v)
if err != nil {
Expand All @@ -965,21 +973,27 @@ func (p *Page) update(f interface{}) error {
*published = cast.ToBool(v)
case "layout":
p.Layout = cast.ToString(v)
p.Params[loki] = p.Layout
case "markup":
p.Markup = cast.ToString(v)
p.Params[loki] = p.Markup
case "weight":
p.Weight = cast.ToInt(v)
p.Params[loki] = p.Weight
case "aliases":
p.Aliases = cast.ToStringSlice(v)
for _, alias := range p.Aliases {
if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") {
return fmt.Errorf("Only relative aliases are supported, %v provided", alias)
}
}
p.Params[loki] = p.Aliases
case "status":
p.Status = cast.ToString(v)
p.Params[loki] = p.Status
case "sitemap":
p.Sitemap = parseSitemap(cast.ToStringMap(v))
p.Params[loki] = p.Sitemap
case "iscjklanguage":
isCJKLanguage = new(bool)
*isCJKLanguage = cast.ToBool(v)
Expand Down Expand Up @@ -1034,17 +1048,20 @@ func (p *Page) update(f interface{}) error {
} else if published != nil {
p.Draft = !*published
}
p.Params["draft"] = p.Draft

if p.Date.IsZero() && p.s.Cfg.GetBool("useModTimeAsFallback") {
fi, err := p.s.Fs.Source.Stat(filepath.Join(p.s.PathSpec.AbsPathify(p.s.Cfg.GetString("contentDir")), p.File.Path()))
if err == nil {
p.Date = fi.ModTime()
p.Params["date"] = p.Date
}
}

if p.Lastmod.IsZero() {
p.Lastmod = p.Date
}
p.Params["lastmod"] = p.Lastmod

if isCJKLanguage != nil {
p.isCJKLanguage = *isCJKLanguage
Expand All @@ -1055,6 +1072,7 @@ func (p *Page) update(f interface{}) error {
p.isCJKLanguage = false
}
}
p.Params["iscjklanguage"] = p.isCJKLanguage

return nil

Expand Down
6 changes: 4 additions & 2 deletions hugolib/page_test.go
Expand Up @@ -1336,7 +1336,7 @@ some content
func TestPageParams(t *testing.T) {
t.Parallel()
s := newTestSite(t)
want := map[string]interface{}{
wantedMap := map[string]interface{}{
"tags": []string{"hugo", "web"},
// Issue #2752
"social": []interface{}{
Expand All @@ -1348,7 +1348,9 @@ func TestPageParams(t *testing.T) {
for i, c := range pagesParamsTemplate {
p, err := s.NewPageFrom(strings.NewReader(c), "content/post/params.md")
require.NoError(t, err, "err during parse", "#%d", i)
assert.Equal(t, want, p.Params, "#%d", i)
for key, _ := range wantedMap {
assert.Equal(t, wantedMap[key], p.Params[key], "#%d", key)
}
}
}

Expand Down