hugolib: Fix "published" front matter handling

Also related:

* support "modified" as an optional way to signal "last modified"
* make sure all relevant page dates are also added to params

Fixes #3867
This commit is contained in:
Bjørn Erik Pedersen 2017-09-08 12:28:15 +02:00
parent 573deca089
commit 202510fdc9

View file

@ -971,6 +971,8 @@ func (p *Page) update(f interface{}) error {
// Needed for case insensitive fetching of params values // Needed for case insensitive fetching of params values
helpers.ToLowerMap(m) helpers.ToLowerMap(m)
var modified time.Time
var err error var err error
var draft, published, isCJKLanguage *bool var draft, published, isCJKLanguage *bool
for k, v := range m { for k, v := range m {
@ -1014,6 +1016,14 @@ func (p *Page) update(f interface{}) error {
if err != nil { if err != nil {
p.s.Log.ERROR.Printf("Failed to parse lastmod '%v' in page %s", v, p.File.Path()) p.s.Log.ERROR.Printf("Failed to parse lastmod '%v' in page %s", v, p.File.Path())
} }
case "modified":
vv, err := cast.ToTimeE(v)
if err == nil {
p.Params[loki] = vv
modified = vv
} else {
p.Params[loki] = cast.ToString(v)
}
case "outputs": case "outputs":
o := cast.ToStringSlice(v) o := cast.ToStringSlice(v)
if len(o) > 0 { if len(o) > 0 {
@ -1034,6 +1044,7 @@ func (p *Page) update(f interface{}) error {
if err != nil { if err != nil {
p.s.Log.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path()) p.s.Log.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path())
} }
p.Params[loki] = p.PublishDate
case "expirydate", "unpublishdate": case "expirydate", "unpublishdate":
p.ExpiryDate, err = cast.ToTimeE(v) p.ExpiryDate, err = cast.ToTimeE(v)
if err != nil { if err != nil {
@ -1043,8 +1054,19 @@ func (p *Page) update(f interface{}) error {
draft = new(bool) draft = new(bool)
*draft = cast.ToBool(v) *draft = cast.ToBool(v)
case "published": // Intentionally undocumented case "published": // Intentionally undocumented
published = new(bool) vv, err := cast.ToBoolE(v)
*published = cast.ToBool(v) if err == nil {
published = &vv
} else {
// Some sites use this as the publishdate
vv, err := cast.ToTimeE(v)
if err == nil {
p.PublishDate = vv
p.Params[loki] = p.PublishDate
} else {
p.Params[loki] = cast.ToString(v)
}
}
case "layout": case "layout":
p.Layout = cast.ToString(v) p.Layout = cast.ToString(v)
p.Params[loki] = p.Layout p.Params[loki] = p.Layout
@ -1133,9 +1155,16 @@ func (p *Page) update(f interface{}) error {
} }
if p.Lastmod.IsZero() { if p.Lastmod.IsZero() {
p.Lastmod = p.Date if !modified.IsZero() {
p.Lastmod = modified
} else {
p.Lastmod = p.Date
}
} }
p.Params["lastmod"] = p.Lastmod p.Params["lastmod"] = p.Lastmod
p.Params["publishdate"] = p.PublishDate
p.Params["expirydate"] = p.ExpiryDate
if isCJKLanguage != nil { if isCJKLanguage != nil {
p.isCJKLanguage = *isCJKLanguage p.isCJKLanguage = *isCJKLanguage