1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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",
)
}
|