mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 17:12:07 -05:00
Adding support for date field in front matter as date (as TOML provides)
This commit is contained in:
parent
f3c816eabd
commit
471fb1ff69
3 changed files with 31 additions and 21 deletions
|
@ -8,10 +8,35 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func interfaceToTime(i interface{}) time.Time {
|
||||
switch s := i.(type) {
|
||||
case time.Time:
|
||||
return s
|
||||
case string:
|
||||
d, e := stringToDate(s)
|
||||
if e == nil {
|
||||
return d
|
||||
}
|
||||
errorf("Invalid Time/Date format")
|
||||
default:
|
||||
errorf("Only Time is supported for this key")
|
||||
}
|
||||
|
||||
return *new(time.Time)
|
||||
}
|
||||
|
||||
func interfaceToStringToDate(i interface{}) time.Time {
|
||||
s := interfaceToString(i)
|
||||
|
||||
if d, e := parseDateWith(s, []string{
|
||||
if d, e := stringToDate(s); e == nil {
|
||||
return d
|
||||
}
|
||||
|
||||
return time.Unix(0, 0)
|
||||
}
|
||||
|
||||
func stringToDate(s string) (time.Time, error) {
|
||||
return parseDateWith(s, []string{
|
||||
time.RFC3339,
|
||||
time.RFC1123Z,
|
||||
time.RFC1123,
|
||||
|
@ -24,11 +49,7 @@ func interfaceToStringToDate(i interface{}) time.Time {
|
|||
"02 Jan 06 15:04 MST",
|
||||
"2006-01-02",
|
||||
"02 Jan 2006",
|
||||
}); e == nil {
|
||||
return d
|
||||
}
|
||||
|
||||
return time.Unix(0, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// TODO remove this and return a proper error.
|
||||
|
@ -118,17 +139,6 @@ func interfaceToInt(i interface{}) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
func interfaceToTime(i interface{}) time.Time {
|
||||
switch s := i.(type) {
|
||||
case time.Time:
|
||||
return s
|
||||
default:
|
||||
errorf("Only Time is supported for this key")
|
||||
}
|
||||
|
||||
return *new(time.Time)
|
||||
}
|
||||
|
||||
func interfaceToString(i interface{}) string {
|
||||
switch s := i.(type) {
|
||||
case string:
|
||||
|
|
|
@ -348,7 +348,7 @@ func (page *Page) update(f interface{}) error {
|
|||
case "keywords":
|
||||
page.Keywords = interfaceArrayToStringArray(v)
|
||||
case "date", "pubdate":
|
||||
page.Date = interfaceToStringToDate(v)
|
||||
page.Date = interfaceToTime(v)
|
||||
case "draft":
|
||||
page.Draft = interfaceToBool(v)
|
||||
case "layout":
|
||||
|
@ -384,7 +384,7 @@ func (page *Page) update(f interface{}) error {
|
|||
for i, u := range vvv {
|
||||
a[i] = interfaceToString(u)
|
||||
}
|
||||
page.Params[strings.ToLower(k)] = a
|
||||
page.Params[loki] = a
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,8 +75,8 @@ Page With Date HugoLong`
|
|||
|
||||
func TestDegenerateDateFrontMatter(t *testing.T) {
|
||||
p, _ := ReadFrom(strings.NewReader(PAGE_WITH_INVALID_DATE), "page/with/invalid/date")
|
||||
if p.Date != time.Unix(0, 0) {
|
||||
t.Fatalf("Date should be set to computer epoch. Got: %s", p.Date)
|
||||
if p.Date != *new(time.Time) {
|
||||
t.Fatalf("Date should be set to time.Time zero value. Got: %s", p.Date)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue