/
tpldata.go
166 lines (152 loc) · 3.65 KB
/
tpldata.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bufio"
"bytes"
"fmt"
"html/template"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/russross/blackfriday"
)
var (
ErrEmptyPost = fmt.Errorf("empty post file")
ErrInvalidFrontMatter = fmt.Errorf("invalid front matter")
ErrMissingFrontMatter = fmt.Errorf("missing front matter")
// Lookup table to find the format based on the length of the date in the front matter
pubDtFmt = map[int]string{
10: "2006-01-02",
13: "2006-01-02 15h",
14: "2006-01-02 15h",
15: "2006-01-02 15:04",
16: "2006-01-02 15:04",
25: time.RFC3339,
}
)
// The TemplateData structure contains all the relevant information passed to the
// template to generate the static HTML file.
type TemplateData struct {
SiteName string
TagLine string
RssURL string
Post *LongPost
Recent []*LongPost
Prev *ShortPost
Next *ShortPost
}
// Create a new TemplateData for the specified post.
func newTemplateData(p *LongPost, i int, r []*LongPost, all []*LongPost) *TemplateData {
td := &TemplateData{
SiteName: Options.SiteName,
TagLine: Options.TagLine,
RssURL: RssURL,
Post: p,
Recent: r,
}
if i > 0 {
td.Prev = all[i-1].ShortPost
}
if i < len(all)-1 {
td.Next = all[i+1].ShortPost
}
return td
}
// The ShortPost structure defines the basic metadata of a post.
type ShortPost struct {
Slug string
Author string
Title string
Description string
Lang string
PubTime time.Time
ModTime time.Time
}
// The LongPost structure adds the parsed content of the post to the embedded ShortPost information.
type LongPost struct {
*ShortPost
Content template.HTML
}
// Replace special characters to form a valid slug (post path)
var rxSlug = regexp.MustCompile(`[^a-zA-Z\-_0-9]`)
// Return a valid slug from the file name of the post.
func getSlug(fnm string) string {
return rxSlug.ReplaceAllString(strings.Replace(fnm, filepath.Ext(fnm), "", 1), "-")
}
// Read the front matter from the post. If there is no front matter, this is
// not a valid post.
func readFrontMatter(s *bufio.Scanner) (map[string]string, error) {
m := make(map[string]string)
infm := false
for s.Scan() {
l := strings.Trim(s.Text(), " ")
if l == "---" { // The front matter is delimited by 3 dashes
if infm {
// This signals the end of the front matter
return m, nil
} else {
// This is the start of the front matter
infm = true
}
} else if infm {
sections := strings.SplitN(l, ":", 2)
if len(sections) != 2 {
// Invalid front matter line
return nil, ErrInvalidFrontMatter
}
m[sections[0]] = strings.Trim(sections[1], " ")
} else if l != "" {
// No front matter, quit
return nil, ErrMissingFrontMatter
}
}
if err := s.Err(); err != nil {
return nil, err
}
return nil, ErrEmptyPost
}
// Create a LongPost from the specified FileInfo.
func newLongPost(fi os.FileInfo) (*LongPost, error) {
f, err := os.Open(filepath.Join(PostsDir, fi.Name()))
if err != nil {
return nil, err
}
defer f.Close()
s := bufio.NewScanner(f)
m, err := readFrontMatter(s)
if err != nil {
return nil, err
}
slug := getSlug(fi.Name())
pubdt := fi.ModTime()
if dt, ok := m["Date"]; ok && len(dt) > 0 {
pubdt, err = time.Parse(pubDtFmt[len(dt)], dt)
if err != nil {
return nil, err
}
}
sp := &ShortPost{
slug,
m["Author"],
m["Title"],
m["Description"],
m["Lang"],
pubdt,
fi.ModTime(),
}
// Read rest of file
buf := bytes.NewBuffer(nil)
for s.Scan() {
buf.WriteString(s.Text() + "\n")
}
if err = s.Err(); err != nil {
return nil, err
}
res := blackfriday.MarkdownCommon(buf.Bytes())
lp := &LongPost{
sp,
template.HTML(res),
}
return lp, nil
}