Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 24, 2018
1 parent 0bb9de7 commit 352e787
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 12 deletions.
70 changes: 70 additions & 0 deletions hugolib/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"strings"
"time"

"github.com/mitchellh/mapstructure"

"github.com/gohugoio/hugo/helpers"

"github.com/gohugoio/hugo/config"
Expand Down Expand Up @@ -198,6 +200,64 @@ func (f frontmatterHandler) newChainedFrontMatterFieldHandler(handlers ...frontM
}
}

type frontmatterConfig struct {
date []string
lastMod []string
publishDate []string
expiryDate []string
}

const (
fmDate = "date"
fmPubDate = "publishdate"
fmLastMod = "lastmod"
fmExpiryDate = "expirydate"
)

func newDefaultFrontmatterConfig() frontmatterConfig {
return frontmatterConfig{
date: []string{fmDate, fmPubDate, fmLastMod},
lastMod: []string{fmLastMod, fmDate},
publishDate: []string{fmPubDate, fmDate},
expiryDate: []string{fmExpiryDate},
}
}

func newFrontmatterConfig(cfg config.Provider) (frontmatterConfig, error) {
c := newDefaultFrontmatterConfig()

if cfg.IsSet("frontmatter") {
fm := cfg.GetStringMap("frontmatter")
if fm != nil {
for k, v := range fm {
loki := strings.ToLower(k)
switch loki {
case fmDate:
c.date = toLowerSlice(v)
case fmPubDate:
c.publishDate = toLowerSlice(v)
case fmLastMod:
c.lastMod = toLowerSlice(v)
case fmExpiryDate:
c.expiryDate = toLowerSlice(v)
}
}
}
err := mapstructure.WeakDecode(fm, &c)
return c, err
}
return c, nil
}

func toLowerSlice(in interface{}) []string {
out := cast.ToStringSlice(in)
for i := 0; i < len(out); i++ {
out[i] = strings.ToLower(out[i])
}

return out
}

func newFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (frontmatterHandler, error) {

if logger == nil {
Expand All @@ -208,6 +268,16 @@ func newFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (frontmatte

handlers := &frontmatterFieldHandlers{logger: logger}

/*
[frontmatter]
date = ["date", "publishDate", "lastMod"]
lastMod = ["lastMod", "date"]
publishDate = ["publishDate", "date"]
expiryDate = ["expiryDate"]
*/

dateHandlers := []frontMatterFieldHandler{handlers.defaultDateHandler}

defaultDate := cfg.Get("frontmatter.defaultdate")
Expand Down
50 changes: 38 additions & 12 deletions hugolib/page_frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,53 @@ func TestDateAndSlugFromBaseFilename(t *testing.T) {
}
}

func TestFrontMatterDates(t *testing.T) {
t.Parallel()
func TestFrontMatterNewConfig(t *testing.T) {
assert := require.New(t)

defaultDateSettings := []string{"none", "file"}
cfg := viper.New()

for _, defaultDateSetting := range defaultDateSettings {
t.Run(fmt.Sprintf("defaultDate=%s", defaultDateSetting), func(t *testing.T) {
doTestFrontMatterDates(t, defaultDateSetting)
})
}
}
cfg.Set("frontmatter", map[string]interface{}{
"date": []string{"publishDate", "LastMod"},
"Lastmod": []string{"publishDate"},
"expiryDate": []string{"lastMod"},
"publishDate": []string{"date"},
})

func doTestFrontMatterDates(t *testing.T, defaultDateSetting string) {
assert := require.New(t)
fc, err := newFrontmatterConfig(cfg)
assert.NoError(err)
assert.Equal([]string{"publishdate", "lastmod"}, fc.date)
assert.Equal([]string{"publishdate"}, fc.lastMod)
assert.Equal([]string{"lastmod"}, fc.expiryDate)
assert.Equal([]string{"date"}, fc.publishDate)

// Default
cfg = viper.New()
fc, err = newFrontmatterConfig(cfg)
assert.NoError(err)
assert.Equal(3, len(fc.date))
assert.Equal(2, len(fc.lastMod))
assert.Equal(2, len(fc.publishDate))
assert.Equal(1, len(fc.expiryDate))

}

func TestFrontMatterDatesConfigVariations(t *testing.T) {
cfg := viper.New()

cfg.Set("frontmatter", map[string]interface{}{
"defaultDate": []string{defaultDateSetting},
"defaultDate": []string{"date"},
})

fmt.Println(">>", cfg)
}

func TestFrontMatterDates(t *testing.T) {
t.Parallel()

assert := require.New(t)

cfg := viper.New()

handler, err := newFrontmatterHandler(newWarningLogger(), cfg)
assert.NoError(err)

Expand Down

0 comments on commit 352e787

Please sign in to comment.