Skip to content

Commit

Permalink
hugolib: Extract date and slug from filename
Browse files Browse the repository at this point in the history
This commit adds a new config option  which, when enabled and no date is set in front matter, will make Hugo try to parse the date from the content filename.

Also, the filenames in these cases will make for very poor permalinks, so we will also use the remaining part as the page `slug` if that value is not set in front matter.

This should make it easier to move content from Jekyll to Hugo.

To enable, put this in your `config.toml`:

```toml
[frontmatter]
date  = [":filename", "date"]
```

This commit also creates a testable unit from the date front matter handling. We should try to get `:git` as a keyword in the same config map, but that will have to wait for a later time.

Fixes gohugoio#285
Closes gohugoio#3310
Closes gohugoio#3762
Closes gohugoio#4340
  • Loading branch information
bep committed Mar 11, 2018
1 parent f8dc47e commit 6889713
Show file tree
Hide file tree
Showing 7 changed files with 882 additions and 259 deletions.
133 changes: 45 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,48 @@ 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()
}

if p.GitInfo != nil {
fmt.Println(">>>> > > > > > ")
}

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 +1174,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 +1185,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 +1206,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 +1324,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 +1830,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

0 comments on commit 6889713

Please sign in to comment.