-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
132 lines (115 loc) · 2.76 KB
/
post.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package grout
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"time"
)
type Post struct {
*HTMLDocument
datetime time.Time
date string
xmldate string
url string
atomid string
metadata M
}
var postNameRE = regexp.MustCompile(`^([0-9]{4})-([0-9]{2})-([0-9]{2})-([0-9A-z\-]+)$`)
func (p *Post) Read(data M) error {
err := p.HTMLDocument.Read(data)
if err != nil {
return err
}
buf := bytes.NewBuffer(make([]byte, 0, 512))
data["page"] = p.FrontMatter
err = p.Template.Execute(buf, data)
delete(data, "page")
if err != nil {
return err
}
p.metadata = M{
"title": p.FrontMatter["title"],
"date": p.date,
"xmldate": p.xmldate,
"url": p.url,
"atomid": p.atomid,
"content": string(buf.Bytes()),
}
return nil
}
func (p *Post) PostRead(data M, collection []Content, i int) error {
return nil
}
func (p *Post) Write(dir, cachedir string, data M) error {
// TODO: ideally we wouldn't Mkdir for every post...but the
// OS/filesystem caches probably work well enough. We'll measure
// everything later in any case.
newdir := filepath.Dir(filepath.Join(dir, p.Path()))
err := os.MkdirAll(newdir, 0700)
if err != nil {
return err
}
err = p.HTMLDocument.Write(dir, cachedir, data)
if err != nil {
return err
}
return err
}
func (p *Post) Metadata() M {
return p.metadata
}
func (p *Post) Less(other Collectable) bool {
// reverse order
otherPost := other.(*Post)
return p.datetime.Unix() > otherPost.datetime.Unix()
}
func GeneratePost(sitecfg, cfg M, info ContentInfo) (Content, error) {
path := info.Path()
ext := filepath.Ext(path)
switch ext {
case ".html", ".htm":
withoutExt := path[:len(path)-len(ext)]
matches := postNameRE.FindStringSubmatch(withoutExt)
if len(matches) < 5 {
return nil, fmt.Errorf("bad post name: %s", path)
}
// TODO: support custom permalinks, and date format for
// metadata
info.SetPath(fmt.Sprintf("%s/%s/%s/%s.html",
matches[1], matches[2], matches[3], matches[4]))
//baseurl := sitecfg.String("url", "")
baseurl := ""
url, err := BuildURL(baseurl, info.Path())
if err != nil {
return nil, err
}
atomid, err := BuildURL(baseurl, fmt.Sprintf("%s-%s-%s-%s",
matches[1], matches[2], matches[3], matches[4]))
if err != nil {
return nil, err
}
datetime, err := time.Parse("2006 01 02",
fmt.Sprintf("%s %s %s", matches[1], matches[2],
matches[3]))
if err != nil {
return nil, err
}
return &Post{
HTMLDocument: &HTMLDocument{ContentInfo: info},
datetime: datetime,
date: fmt.Sprintf("%s-%s-%s",
matches[1], matches[2], matches[3]),
xmldate: XMLDate(datetime),
url: url,
atomid: atomid,
}, nil
default:
return nil, ErrIgnore
}
panic("unreachable")
}
func init() {
RegisterGenerator("post", GeneratePost)
}