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

Allow dates to be parsed from filenames #3310

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 1 deletion docs/content/content/front-matter.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
aliases:
- /doc/front-matter/
lastmod: 2015-12-23
lastmod: 2017-04-09
date: 2013-07-01
menu:
main:
Expand Down Expand Up @@ -117,3 +117,5 @@ It's possible to set some options for Markdown rendering in the page's front mat

See [Configuration]({{< ref "overview/configuration.md#configure-blackfriday-rendering" >}}) for more.

## Fallback date variable from filenames
If you're migrating content to Hugo, you may have content with dates in the filename. For example `2017-01-31-myblog.md`. You can optionally enable the `useFilenameDateAsFallback` configuration option. This will attempt to parse the datestamp in the filename and use it as a fallback to providing a date variable in the frontmatter.
8 changes: 7 additions & 1 deletion docs/content/overview/configuration.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
aliases:
- /doc/configuration/
lastmod: 2016-09-17
lastmod: 2017-04-09
date: 2013-07-01
linktitle: Configuration
menu:
Expand Down Expand Up @@ -139,6 +139,10 @@ along with their current, default values:
enableEmoji: false
# Show a placeholder instead of the default value or an empty string if a translation is missing
enableMissingTranslationPlaceholders: false
# Regex to use for dates in filenames. Only used with `useFilenameDateAsFallback`
filenameDateFallbackPattern: "(?P<year>\\d{4})\\-(?P<month>\\d{2})\\-(?P<day>\\d{2})"
# Time format for custom dates in filenames. Only used with `useFilenameDateAsFallback` and must match `filenameDateFallbackPattern`
filenameDateFallbackFormat: "2006-01-02"
footnoteAnchorPrefix: ""
footnoteReturnLinkContents: ""
# google analytics tracking id
Expand Down Expand Up @@ -187,6 +191,8 @@ along with their current, default values:
title: ""
# if true, use /filename.html instead of /filename/
uglyURLs: false
#if true, use dates in filenames e.g. 2017-01-31-mypostname.md
useFilenameDateAsFallback: false
# Do not make the url/path to lowercase
disablePathToLower: false
# if true, auto-detect Chinese/Japanese/Korean Languages in the content. (.Summary and .WordCount can work properly in CJKLanguage)
Expand Down
3 changes: 3 additions & 0 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func loadDefaultSettingsFor(v *viper.Viper) {
v.SetDefault("enableEmoji", false)
v.SetDefault("pygmentsCodeFencesGuessSyntax", false)
v.SetDefault("useModTimeAsFallback", false)
v.SetDefault("useFilenameDateAsFallback", false)
v.SetDefault("filenameDateFallbackPattern", "(?P<year>\\d{4})\\-(?P<month>\\d{2})\\-(?P<day>\\d{2})")
v.SetDefault("filenameDateFallbackFormat", "2006-01-02")
v.SetDefault("defaultContentLanguage", "en")
v.SetDefault("defaultContentLanguageInSubdir", false)
v.SetDefault("enableMissingTranslationPlaceholders", false)
Expand Down
8 changes: 8 additions & 0 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ func (p *Page) update(f interface{}) error {
p.Date = fi.ModTime()
p.Params["date"] = p.Date
}
} else if p.Date.IsZero() && p.s.Cfg.GetBool("useFilenameDateAsFallback") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a little too many config variables as is, and I think we can drop this one and just check that filenameDateFallbackPattern is set. But other than this minor detail, this looks very good.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bep thats a good point. With that in mind, perhaps one config value to provide the regex that defaults to nil? That way the parsing (regardless of format separator) can be configured but there's BC compat if Nil or not configured?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow the logic here. Are you saying we could reduce the number of configs to 1? How?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking so - its balance between config and 'magic'.

If the regex and time format is always the same, you can infer it from named groups.

For example:

(?P<datestamp>(?P<stdYear>\d{4})\-(?P<stdZeroMonth>\d{2})-(?P<stdZeroDay>\d{2}))

The catch is that time format const's aren't public so there would need to be a copy of them in Hugo for mapping (probably just day/month/year with the 0 padded variants too).

So its either:
a) add 2x config params and let people choose format and pattern
b) add 1x config param and force people to use the same format as the pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot this, as this will likely be copy-pasted from the documentation:

  • If the code to create a time format string from that regexp is not too complex, then 1 config is fine.
  • else just add two config vars.

dateExp := regexp.MustCompile(p.s.Cfg.GetString("filenameDateFallbackPattern"))
dateString := dateExp.FindString(p.File.Path())
filenameDate, err := time.Parse(p.s.Cfg.GetString("filenameDateFallbackFormat"), dateString)
if err == nil {
p.Date = filenameDate
p.Params["date"] = p.Date
}
}

if p.Lastmod.IsZero() {
Expand Down
17 changes: 17 additions & 0 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Content of the file goes Here
Content of the file goes Here
`
simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
simplePageNoDate = "---\ntitle: Path Date\n---\n Date param from url"
simplePageJSONMultiple = `
{
"title": "foobar",
Expand Down Expand Up @@ -847,6 +848,22 @@ func TestPageWithDate(t *testing.T) {
checkPageDate(t, p, d)
}

func TestPageWithDateInFilename(t *testing.T) {
t.Parallel()
cfg, fs := newTestCfg()

writeSource(t, fs, filepath.Join("content", "2017-01-31-simple.md"), simplePageNoDate)

s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})

require.Len(t, s.RegularPages, 1)

p := s.RegularPages[0]
d, _ := time.Parse(time.RFC3339, "2017-01-31")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this always fails as RFC3339 requires timezone info.


checkPageDate(t, p, d)
}

func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) {
t.Parallel()
assertFunc := func(t *testing.T, ext string, pages Pages) {
Expand Down