-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
245 lines (193 loc) · 4.57 KB
/
parse.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package blawg
import (
"bufio"
"bytes"
"errors"
"fmt"
"golang.org/x/net/html"
"gopkg.in/russross/blackfriday.v2"
"io"
"strings"
"time"
"gopkg.in/yaml.v2"
"html/template"
"os"
"path/filepath"
)
const dateFormat = "2006-01-02 15:04:05"
var markdownExtensions = blackfriday.WithExtensions(
blackfriday.Footnotes | blackfriday.CommonExtensions,
)
func parse(rawPost io.Reader) (*Post, error) {
post := new(Post)
rawMeta, body, err := split(rawPost)
if err != nil {
return post, err
}
err = addMeta(rawMeta, post)
if err != nil {
return post, err
}
postHTML := blackfriday.Run(body, markdownExtensions)
post.Body = template.HTML(postHTML)
post.Title = htmlTitle(post.Metadata.Title)
post.TitleText, _ = textTitle(post)
return post, nil
}
func parsePage(rawPage io.Reader) (page *Page, err error) {
page = new(Page)
rawMeta, body, err := split(rawPage)
if err != nil {
return
}
pageHTML := blackfriday.Run(body, markdownExtensions)
page.Body = template.HTML(pageHTML)
meta, err := parseMeta(rawMeta)
if err != nil {
return
}
page.Title = htmlTitle(meta.Title)
page.TitleText, err = textFromHTMLTemplate(page.Title)
return
}
func htmlTitle(s string) template.HTML {
title := blackfriday.Run([]byte(s), markdownExtensions)
titleWithoutPtags := title[3 : len(title)-5]
return template.HTML(titleWithoutPtags)
}
func textFromHTMLTemplate(t template.HTML) (text string, err error) {
node, err := html.Parse(strings.NewReader(string(t)))
if err != nil {
return
}
text = nodeTextContent(node)
return
}
func textTitle(p *Post) (string, error) {
var text string
n, err := html.Parse(strings.NewReader(string(p.Title)))
if err != nil {
return text, err
}
text = nodeTextContent(n)
return text, nil
}
func nodeTextContent(n *html.Node) string {
var text []string
var extractText func(*html.Node)
extractText = func(n *html.Node) {
if n.Type == html.TextNode {
text = append(text, strings.TrimSpace(n.Data))
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
extractText(c)
}
}
extractText(n)
return strings.Join(text, " ")
}
func split(page io.Reader) (meta, body []byte, err error) {
var m bytes.Buffer
var b bytes.Buffer
scanner := bufio.NewScanner(page)
scanner.Scan()
if line := scanner.Text(); line != "---" {
return meta, body, errors.New("no metadata block")
}
for scanner.Scan() {
line := scanner.Text()
if line == "---" {
break
}
m.WriteString(fmt.Sprintln(line))
}
if scanner.Text() != "---" {
return meta, body, errors.New("no end to the metadata block")
}
for scanner.Scan() {
line := scanner.Text()
b.WriteString(fmt.Sprintln(line))
}
return m.Bytes(), b.Bytes(), nil
}
func addMeta(rawMeta []byte, post *Post) (err error) {
meta := Metadata{}
err = yaml.Unmarshal(rawMeta, &meta)
if err != nil {
return err
}
date, err := time.Parse(dateFormat, meta.Date)
if err != nil {
return err
}
post.Date = date
post.Metadata = meta
return
}
func parseMeta(rawMeta []byte) (m Metadata, err error) {
m = Metadata{}
err = yaml.Unmarshal(rawMeta, &m)
return
}
// GetTemplates reads in all of the templates in the templates directory
func GetTemplates(templateDirectory string) (*template.Template, error) {
return template.ParseGlob(templateDirectory + "/**")
}
// GetPosts reads in all of the posts files in the posts directory and parses
// them into posts.
func GetPosts(postDir string) (*Posts, error) {
var err error
var posts []Post
err = filepath.Walk(postDir, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if fileInfo.IsDir() {
return nil
}
fileExtension := filepath.Ext(path)
if !(fileExtension == ".md" || fileExtension == ".markdown") {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
post, err := parse(file)
if err != nil {
return fmt.Errorf("error parsing post %s : \n\t%s", fileInfo.Name(), err)
}
if !post.Published {
return nil
}
posts = append(posts, *post)
return nil
})
var p Posts = posts
return &p, err
}
func getPages(pageDir string) ([]Page, error) {
var err error
var pages []Page
err = filepath.Walk(pageDir, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if fileInfo.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
page, err := parsePage(file)
if err != nil {
return fmt.Errorf("error parsing page %s : \n\t%s", fileInfo.Name(), err)
}
pages = append(pages, *page)
return nil
})
return pages, err
}