Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 22, 2018
1 parent 1d9e1e8 commit f3db894
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
11 changes: 6 additions & 5 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,12 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
// Needed for case insensitive fetching of params values
helpers.ToLowerMap(frontmatter)

descriptor := frontMatterDescriptor{frontmatter: frontmatter, params: p.params, baseFilename: p.BaseFileName()}
var mtime time.Time
if p.Source.FileInfo() != nil {
mtime = p.Source.FileInfo().ModTime()
}

descriptor := frontMatterDescriptor{frontmatter: frontmatter, params: p.params, baseFilename: p.BaseFileName(), modTime: mtime}

// Handle the date separately
dates, err := p.s.frontmatterConfig.handleDates(descriptor)
Expand Down Expand Up @@ -1313,10 +1318,6 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
}
p.params["draft"] = p.Draft

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

if isCJKLanguage != nil {
p.isCJKLanguage = *isCJKLanguage
} else if p.s.Cfg.GetBool("hasCJKLanguage") {
Expand Down
20 changes: 17 additions & 3 deletions hugolib/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type frontMatterDescriptor struct {

// This is the Page's base filename, e.g. page.md.
baseFilename string

// The content file's mod time.
modTime time.Time
}

func (f frontmatterConfig) handleField(handlers []frontmatterFieldHandler, d frontMatterDescriptor) (interface{}, error) {
Expand Down Expand Up @@ -99,7 +102,6 @@ func (f frontmatterConfig) handleDates(d frontMatterDescriptor) (PageDates, erro
if err != nil {
return *pd, err
}

pd.Date = date
pd.Lastmod = f.setParamsAndReturnFirstDate(d, lastModFrontMatterKeys)
pd.PublishDate = f.setParamsAndReturnFirstDate(d, publishDateFrontMatterKeys)
Expand Down Expand Up @@ -206,14 +208,19 @@ func newFrontmatterConfig(logger *jww.Notepad, cfg config.Provider) (frontmatter

for _, v := range slice {
if strings.EqualFold(v, "filename") {
f.dateHandlers = append(f.dateHandlers, handlers.filenameFallbackDateHandler)
f.dateHandlers = append(f.dateHandlers, handlers.defaultDateDateFilenameHandler)
// No more for now.
break
}
}

}

// This is deprecated
if cfg.GetBool("useModTimeAsFallback") {
f.dateHandlers = append(f.dateHandlers, handlers.defaultDateDateModTimeHandler)
}

return f, nil
}

Expand All @@ -237,6 +244,13 @@ func (f *frontmatterFieldHandlers) defaultDateHandler(d frontMatterDescriptor) (
return date, nil
}

func (f *frontmatterFieldHandlers) filenameFallbackDateHandler(d frontMatterDescriptor) (interface{}, error) {
func (f *frontmatterFieldHandlers) defaultDateDateFilenameHandler(d frontMatterDescriptor) (interface{}, error) {
return true, nil
}

func (f *frontmatterFieldHandlers) defaultDateDateModTimeHandler(d frontMatterDescriptor) (interface{}, error) {
if !d.modTime.IsZero() {
return d.modTime, nil
}
return nil, nil
}
43 changes: 21 additions & 22 deletions hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (

"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/source"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1006,6 +1004,9 @@ Page With empty front matter`
*/
func TestMetadataDates(t *testing.T) {
t.Parallel()

assert := require.New(t)

var tests = []struct {
text string
filename string
Expand Down Expand Up @@ -1038,39 +1039,37 @@ func TestMetadataDates(t *testing.T) {
{p_D____, "test.md", false, D, D, D, x, x}, // date copied across
{p_D____, "testy.md", true, D, D, D, x, x},
{p__P___, "test.md", false, P, P, P, x, x}, // pubdate copied across
{p__P___, "testy.md", true, P, P, P, x, x},
{p__P___, "testy.md", true, P, P, P, x, x}, // TODO(bep) date from modTime
{p_DP___, "test.md", false, D, P, D, x, x}, // date -> lastMod
{p_DP___, "testy.md", true, D, P, D, x, x},
{p_DP___, "testy.md", true, D, P, D, x, x}, // TODO(bep) date from modTime
{p__PL__, "test.md", false, P, P, L, x, x}, // pub -> date overrides lastMod -> date code (inconsistent?)
{p__PL__, "testy.md", true, P, P, L, x, x},
{p_DPL__, "test.md", false, D, P, L, x, x}, // three dates
{p_DPL__, "testy.md", true, D, P, L, x, x},
{p_DPL_E, "testy.md", true, D, P, L, x, E}, // lastMod NOT copied to mod (inconsistent?)
{p_DP_ME, "testy.md", true, D, P, M, M, E}, // mod copied to lastMod
{p_DPLME, "testy.md", true, D, P, L, M, E}, // all dates

// TODO(bep) dates
//{emptyFM, "test.md", false, o, o, o, x, x}, // 3 year-one dates, 2 empty dates
//{zero_FM, "test.md", false, o, o, o, x, x},
// {emptyFM, "testy.md", true, s, o, s, x, x}, // 2 filesys, 1 year-one, 2 empty
//{zero_FM, "testy.md", true, s, o, s, x, x},
{emptyFM, "test.md", false, o, o, o, x, x}, // 3 year-one dates, 2 empty dates
{zero_FM, "test.md", false, o, o, o, x, x},
{emptyFM, "testy.md", true, s, o, s, x, x}, // 2 filesys, 1 year-one, 2 empty TODO(bep) date from modTime
{zero_FM, "testy.md", true, s, o, s, x, x}, // TODO(bep) date from modTime
}

for i, test := range tests {
s := newTestSite(t)
s.Cfg.Set("useModTimeAsFallback", test.modFallback)
fs := hugofs.NewMem(s.Cfg)

writeToFs(t, fs.Source, test.filename, test.text)
file, err := fs.Source.Open(test.filename)
if err != nil {
t.Fatal("failed to write test file to test filesystem")
}
fi, _ := fs.Source.Stat(test.filename)
var (
cfg, fs = newTestCfg()
)

writeToFs(t, fs.Source, filepath.Join("content", test.filename), test.text)

cfg.Set("useModTimeAsFallback", test.modFallback)

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

sp := source.NewSourceSpec(s.Cfg, fs)
p := s.newPageFromFile(newFileInfo(sp, "", test.filename, fi, bundleNot))
p.ReadFrom(file)
assert.Equal(1, len(s.RegularPages))
p := s.RegularPages[0]
fi := p.Source.FileInfo()

// check Page Variables
checkDate(t, i+1, "Date", p.Date, test.expDate, fi)
Expand Down

0 comments on commit f3db894

Please sign in to comment.