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

hugolib: Extract date and slug from filename #4494

Merged
merged 1 commit into from
Mar 11, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions helpers/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ func InitLoggers() {
// plenty of time to fix their templates.
func Deprecated(object, item, alternative string, err bool) {
if err {
DistinctErrorLog.Printf("%s's %s is deprecated and will be removed in Hugo %s. %s.", object, item, CurrentHugoVersion.Next().ReleaseVersion(), alternative)
DistinctErrorLog.Printf("%s's %s is deprecated and will be removed in Hugo %s. %s", object, item, CurrentHugoVersion.Next().ReleaseVersion(), alternative)

} else {
// Make sure the users see this while avoiding build breakage. This will not lead to an os.Exit(-1)
DistinctFeedbackLog.Printf("WARNING: %s's %s is deprecated and will be removed in a future release. %s.", object, item, alternative)
DistinctFeedbackLog.Printf("WARNING: %s's %s is deprecated and will be removed in a future release. %s", object, item, alternative)
}
}

Expand Down
11 changes: 9 additions & 2 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,16 @@ func loadDefaultSettingsFor(v *viper.Viper) error {
v.SetDefault("debug", false)
v.SetDefault("disableFastRender", false)

// Remove in Hugo 0.37
// Remove in Hugo 0.39

if v.GetBool("useModTimeAsFallback") {
helpers.Deprecated("Site config", "useModTimeAsFallback", "Try --enableGitInfo or set lastMod in front matter", false)

helpers.Deprecated("Site config", "useModTimeAsFallback", `Replace with this in your config.toml:

[frontmatter]
date = [ "date",":fileModTime", ":default"]
lastmod = ["lastmod" ,":fileModTime", ":default"]
`, false)

}

Expand Down
129 changes: 41 additions & 88 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 All @@ -25,6 +25,7 @@ import (
"github.com/bep/gitmap"

"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugolib/pagemeta"
"github.com/gohugoio/hugo/resource"

"github.com/gohugoio/hugo/output"
Expand Down Expand Up @@ -140,9 +141,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,11 +221,12 @@ type Page struct {
Keywords []string
Data map[string]interface{}

Date time.Time
Lastmod time.Time
pagemeta.PageDates

Sitemap Sitemap
URLPath
pagemeta.URLPath
frontMatterURL string

permalink string
relPermalink string

Expand Down Expand Up @@ -1115,12 +1114,44 @@ 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 := &pagemeta.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 All @@ -1139,7 +1170,7 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
return fmt.Errorf("Only relative URLs are supported, %v provided", url)
}
p.URLPath.URL = cast.ToString(v)
p.URLPath.frontMatterURL = p.URLPath.URL
p.frontMatterURL = p.URLPath.URL
p.params[loki] = p.URLPath.URL
case "type":
p.contentType = cast.ToString(v)
Expand All @@ -1150,32 +1181,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 +1202,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 +1320,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 Expand Up @@ -1865,14 +1826,6 @@ func (p *Page) String() string {
return fmt.Sprintf("Page(%q)", p.title)
}

type URLPath struct {
URL string
frontMatterURL string
Permalink string
Slug string
Section string
}

// Scratch returns the writable context associated with this Page.
func (p *Page) Scratch() *Scratch {
if p.scratch == nil {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (p *Page) initTargetPathDescriptor() error {
Sections: p.sections,
UglyURLs: p.s.Info.uglyURLs(p),
Dir: filepath.ToSlash(p.Source.Dir()),
URL: p.URLPath.frontMatterURL,
URL: p.frontMatterURL,
IsMultihost: p.s.owner.IsMultihost(),
}

Expand Down
Loading