allow different time formats in posts
This commit is contained in:
parent
3c669d0d5f
commit
9ca50eafff
60
post.go
60
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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
3
rss.go
3
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),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
21
user.go
21
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"
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue