diff --git a/post.go b/post.go index 242ffd7..3bb2e59 100644 --- a/post.go +++ b/post.go @@ -28,10 +28,62 @@ type Post struct { } type PostMeta struct { - Title string `yaml:"title"` - Aliases []string `yaml:"aliases"` - Date string `yaml:"date"` - Draft bool `yaml:"draft"` + Title string `yaml:"title"` + Aliases []string `yaml:"aliases"` + Date time.Time `yaml:"date"` + Draft bool `yaml:"draft"` +} + +func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { + type T struct { + Title string `yaml:"title"` + Aliases []string `yaml:"aliases"` + Draft bool `yaml:"draft"` + } + type S struct { + Date string `yaml:"date"` + } + + var t T + var s S + if err := unmarshal(&t); err != nil { + return err + } + if err := unmarshal(&s); err != nil { + return err + } + + pm.Title = t.Title + pm.Aliases = t.Aliases + pm.Draft = t.Draft + + possibleFormats := []string{ + "2006-01-02", + time.Layout, + time.ANSIC, + time.UnixDate, + time.RubyDate, + time.RFC822, + time.RFC822Z, + time.RFC850, + time.RFC1123, + time.RFC1123Z, + time.RFC3339, + time.RFC3339Nano, + time.Stamp, + time.StampMilli, + time.StampMicro, + time.StampNano, + } + + for _, format := range possibleFormats { + if t, err := time.Parse(format, s.Date); err == nil { + pm.Date = t + break + } + } + + return nil } type PostWebmetions struct { diff --git a/post_test.go b/post_test.go index 97fcc89..a2217dc 100644 --- a/post_test.go +++ b/post_test.go @@ -156,7 +156,7 @@ func TestLoadMeta(t *testing.T) { t.Errorf("Expected title: %v, got %v", []string{"foo/bar/"}, post.Meta().Aliases) } - if post.Meta().Date != "Wed, 17 Aug 2022 10:50:02 +0000" { + if post.Meta().Date.Format(time.RFC1123Z) != "Wed, 17 Aug 2022 10:50:02 +0000" { t.Errorf("Expected title: %s, got %s", "Wed, 17 Aug 2022 10:50:02 +0000", post.Meta().Title) } diff --git a/rss.go b/rss.go index 68405f6..f91f607 100644 --- a/rss.go +++ b/rss.go @@ -3,6 +3,7 @@ package owl import ( "bytes" "encoding/xml" + "time" ) type RSS struct { @@ -46,7 +47,7 @@ func RenderRSSFeed(user User) (string, error) { Guid: post.FullUrl(), Title: post.Title(), Link: post.FullUrl(), - PubDate: meta.Date, + PubDate: meta.Date.Format(time.RFC1123Z), }) } diff --git a/rss_test.go b/rss_test.go index 6073cef..fa86cd2 100644 --- a/rss_test.go +++ b/rss_test.go @@ -71,7 +71,7 @@ func TestRenderRSSFeedPostData(t *testing.T) { if !strings.Contains(res, post.FullUrl()) { t.Error("SubTitle not rendered. Got: " + res) } - if !strings.Contains(res, "2015-01-01") { + if !strings.Contains(res, "Thu, 01 Jan 2015 00:00:00 +0000") { t.Error("Date not rendered. Got: " + res) } } diff --git a/user.go b/user.go index fbff29e..8bb5ce7 100644 --- a/user.go +++ b/user.go @@ -110,12 +110,7 @@ func (user User) Posts() ([]*Post, error) { postDates := make([]PostWithDate, len(posts)) for i, post := range posts { meta := post.Meta() - date, err := time.Parse(time.RFC1123Z, meta.Date) - if err != nil { - // invalid date -> use 1970-01-01 - date = time.Time{} - } - postDates[i] = PostWithDate{post: post, date: date} + postDates[i] = PostWithDate{post: post, date: meta.Date} } // sort posts by date @@ -162,11 +157,21 @@ func (user User) CreateNewPost(title string) (Post, error) { } } post := Post{user: &user, id: folder_name, title: title} + meta := PostMeta{ + Title: title, + Date: time.Now(), + Aliases: []string{}, + Draft: false, + } initial_content := "" initial_content += "---\n" - initial_content += "title: " + title + "\n" - initial_content += "date: " + time.Now().UTC().Format(time.RFC1123Z) + "\n" + // write meta + meta_bytes, err := yaml.Marshal(meta) + if err != nil { + return Post{}, err + } + initial_content += string(meta_bytes) initial_content += "---\n" initial_content += "\n" initial_content += "Write your post here.\n" diff --git a/user_test.go b/user_test.go index 3dacfdb..0906f0b 100644 --- a/user_test.go +++ b/user_test.go @@ -42,8 +42,8 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) { posts, _ := user.Posts() post, _ := user.GetPost(posts[0].Id()) meta := post.Meta() - if meta.Date == "" { - t.Error("Found no date. Got: " + meta.Date) + if meta.Date.IsZero() { + t.Errorf("Found no date. Got: %v", meta.Date) } }