From d8b12cf17a43e700d841402f712efa8666e6137f Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 5 May 2022 21:42:46 -0600 Subject: Begin work on post package --- srv/src/post/post.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 srv/src/post/post.go (limited to 'srv/src/post/post.go') diff --git a/srv/src/post/post.go b/srv/src/post/post.go new file mode 100644 index 0000000..54555c3 --- /dev/null +++ b/srv/src/post/post.go @@ -0,0 +1,64 @@ +// Package post deals with the storage and rending of blog post. +package post + +import ( + "fmt" + "path" + "regexp" + "strings" + "time" +) + +// Date represents a calendar date with no timezone information attached. +type Date struct { + Year int + Month time.Month + Day int +} + +// DateFromTime converts a Time into a Date, truncating all non-date +// information. +func DateFromTime(t time.Time) Date { + return Date{ + Year: t.Year(), + Month: t.Month(), + Day: t.Day(), + } +} + +var titleCleanRegexp = regexp.MustCompile(`[^a-z ]`) + +// NewID generates a (hopefully) unique ID based on the given title. +func NewID(title string) string { + title = strings.ToLower(title) + title = titleCleanRegexp.ReplaceAllString(title, "") + title = strings.ReplaceAll(title, " ", "-") + return title +} + +// Post contains all information having to do with a blog post. +type Post struct { + ID string + Title string + Description string + Tags []string + Series string + + PublishedAt Date + LastUpdatedAt Date + + Body string +} + +// URL returns the relative URL of the Post. +func (p Post) URL() string { + return path.Join( + fmt.Sprintf( + "%d/%0d/%0d", + p.PublishedAt.Year, + p.PublishedAt.Month, + p.PublishedAt.Day, + ), + p.ID+".html", + ) +} -- cgit v1.2.3