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

Work In Progress: hugolib: Extract date and slug from filename #4436

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 42 additions & 78 deletions hugolib/page.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Hugo Authors. All rights reserved.
// Copyright 2018 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -140,9 +140,6 @@ type Page struct {
Draft bool
Status string

PublishDate time.Time
ExpiryDate time.Time

// PageMeta contains page stats such as word count etc.
PageMeta

Expand Down Expand Up @@ -223,8 +220,7 @@ type Page struct {
Keywords []string
Data map[string]interface{}

Date time.Time
Lastmod time.Time
PageDates

Sitemap Sitemap
URLPath
Expand Down Expand Up @@ -264,6 +260,13 @@ type Page struct {
targetPathDescriptorPrototype *targetPathDescriptor
}

type PageDates struct {
Date time.Time
Lastmod time.Time
PublishDate time.Time
ExpiryDate time.Time
}

// SearchKeywords implements the related.Document interface needed for fast page searches.
func (p *Page) SearchKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {

Expand Down Expand Up @@ -1115,12 +1118,43 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
// Needed for case insensitive fetching of params values
helpers.ToLowerMap(frontmatter)

var modified time.Time
var mtime time.Time
if p.Source.FileInfo() != nil {
mtime = p.Source.FileInfo().ModTime()
}

descriptor := frontMatterDescriptor{
frontmatter: frontmatter,
params: p.params,
dates: &p.PageDates,
pageURLs: &p.URLPath,
baseFilename: p.BaseFileName(), modTime: mtime}

// Handle the date separately
// TODO(bep) we need to "do more" in this area so this can be split up and
// more easily tested without the Page, but the coupling is strong.
err := p.s.frontmatterHandler.handleDates(descriptor)
if err != nil {
p.s.Log.ERROR.Printf("Failed to handle dates for page %q: %s", p.Path(), err)
}

var err error
var draft, published, isCJKLanguage *bool
for k, v := range frontmatter {
loki := strings.ToLower(k)

if loki == "published" { // Intentionally undocumented
vv, err := cast.ToBoolE(v)
if err == nil {
published = &vv
}
// published may also be a date
continue
}

if p.s.frontmatterHandler.isDateKey(loki) {
continue
}

switch loki {
case "title":
p.title = cast.ToString(v)
Expand Down Expand Up @@ -1150,32 +1184,13 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
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 "headless":
// For now, only the leaf bundles ("index.md") can be headless (i.e. produce no output).
// We may expand on this in the future, but that gets more complex pretty fast.
if p.TranslationBaseName() == "index" {
p.headless = cast.ToBool(v)
}
p.params[loki] = p.headless
case "lastmod":
p.Lastmod, err = cast.ToTimeE(v)
if err != nil {
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":
o := cast.ToStringSlice(v)
if len(o) > 0 {
Expand All @@ -1190,34 +1205,9 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
}

}
case "publishdate", "pubdate":
p.PublishDate, err = cast.ToTimeE(v)
if err != nil {
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":
p.ExpiryDate, err = cast.ToTimeE(v)
if err != nil {
p.s.Log.ERROR.Printf("Failed to parse expirydate '%v' in page %s", v, p.File.Path())
}
case "draft":
draft = new(bool)
*draft = cast.ToBool(v)
case "published": // Intentionally undocumented
vv, err := cast.ToBoolE(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":
p.Layout = cast.ToString(v)
p.params[loki] = p.Layout
Expand Down Expand Up @@ -1333,32 +1323,6 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
}
p.params["draft"] = p.Draft

if p.Date.IsZero() {
p.Date = p.PublishDate
}

if p.PublishDate.IsZero() {
p.PublishDate = p.Date
}

if p.Date.IsZero() && p.s.Cfg.GetBool("useModTimeAsFallback") {
p.Date = p.Source.FileInfo().ModTime()
}

if p.Lastmod.IsZero() {
if !modified.IsZero() {
p.Lastmod = modified
} else {
p.Lastmod = p.Date
}

}

p.params["date"] = p.Date
p.params["lastmod"] = p.Lastmod
p.params["publishdate"] = p.PublishDate
p.params["expirydate"] = p.ExpiryDate

if isCJKLanguage != nil {
p.isCJKLanguage = *isCJKLanguage
} else if p.s.Cfg.GetBool("hasCJKLanguage") {
Expand Down
Loading