shithub: hugo

Download patch

ref: 471fb1ff699473d7821812490b0ee472ecebb8b2
parent: f3c816eabdaddc0939ddc70ac4e2aa3c16abcc11
author: spf13 <[email protected]>
date: Thu Oct 24 11:18:57 EDT 2013

Adding support for date field in front matter as date (as TOML provides)

--- a/hugolib/metadata.go
+++ b/hugolib/metadata.go
@@ -8,10 +8,35 @@
 	"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 @@
 		"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.
@@ -116,17 +137,6 @@
 	}
 
 	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 {
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -348,7 +348,7 @@
 		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 @@
 					for i, u := range vvv {
 						a[i] = interfaceToString(u)
 					}
-					page.Params[strings.ToLower(k)] = a
+					page.Params[loki] = a
 				}
 			}
 		}
--- a/hugolib/page_time_integration_test.go
+++ b/hugolib/page_time_integration_test.go
@@ -75,8 +75,8 @@
 
 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)
 	}
 }