allow different time formats in posts

This commit is contained in:
Niko Abeler 2022-09-11 17:25:26 +02:00
parent 3c669d0d5f
commit 9ca50eafff
6 changed files with 75 additions and 17 deletions

60
post.go
View File

@ -28,10 +28,62 @@ type Post struct {
} }
type PostMeta struct { type PostMeta struct {
Title string `yaml:"title"` Title string `yaml:"title"`
Aliases []string `yaml:"aliases"` Aliases []string `yaml:"aliases"`
Date string `yaml:"date"` Date time.Time `yaml:"date"`
Draft bool `yaml:"draft"` 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 { type PostWebmetions struct {

View File

@ -156,7 +156,7 @@ func TestLoadMeta(t *testing.T) {
t.Errorf("Expected title: %v, got %v", []string{"foo/bar/"}, post.Meta().Aliases) 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) t.Errorf("Expected title: %s, got %s", "Wed, 17 Aug 2022 10:50:02 +0000", post.Meta().Title)
} }

3
rss.go
View File

@ -3,6 +3,7 @@ package owl
import ( import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"time"
) )
type RSS struct { type RSS struct {
@ -46,7 +47,7 @@ func RenderRSSFeed(user User) (string, error) {
Guid: post.FullUrl(), Guid: post.FullUrl(),
Title: post.Title(), Title: post.Title(),
Link: post.FullUrl(), Link: post.FullUrl(),
PubDate: meta.Date, PubDate: meta.Date.Format(time.RFC1123Z),
}) })
} }

View File

@ -71,7 +71,7 @@ func TestRenderRSSFeedPostData(t *testing.T) {
if !strings.Contains(res, post.FullUrl()) { if !strings.Contains(res, post.FullUrl()) {
t.Error("SubTitle not rendered. Got: " + res) 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) t.Error("Date not rendered. Got: " + res)
} }
} }

21
user.go
View File

@ -110,12 +110,7 @@ func (user User) Posts() ([]*Post, error) {
postDates := make([]PostWithDate, len(posts)) postDates := make([]PostWithDate, len(posts))
for i, post := range posts { for i, post := range posts {
meta := post.Meta() meta := post.Meta()
date, err := time.Parse(time.RFC1123Z, meta.Date) postDates[i] = PostWithDate{post: post, date: meta.Date}
if err != nil {
// invalid date -> use 1970-01-01
date = time.Time{}
}
postDates[i] = PostWithDate{post: post, date: date}
} }
// sort posts by 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} post := Post{user: &user, id: folder_name, title: title}
meta := PostMeta{
Title: title,
Date: time.Now(),
Aliases: []string{},
Draft: false,
}
initial_content := "" initial_content := ""
initial_content += "---\n" initial_content += "---\n"
initial_content += "title: " + title + "\n" // write meta
initial_content += "date: " + time.Now().UTC().Format(time.RFC1123Z) + "\n" 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 += "\n" initial_content += "\n"
initial_content += "Write your post here.\n" initial_content += "Write your post here.\n"

View File

@ -42,8 +42,8 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
posts, _ := user.Posts() posts, _ := user.Posts()
post, _ := user.GetPost(posts[0].Id()) post, _ := user.GetPost(posts[0].Id())
meta := post.Meta() meta := post.Meta()
if meta.Date == "" { if meta.Date.IsZero() {
t.Error("Found no date. Got: " + meta.Date) t.Errorf("Found no date. Got: %v", meta.Date)
} }
} }