-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.go
409 lines (346 loc) · 9.69 KB
/
generate.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"github.com/extemporalgenome/slug"
"github.com/mitchellh/cli"
"github.com/russross/blackfriday"
)
const (
TypeMarkdown = "md"
TypeMarkdownLong = "markdown"
TypeHTML = "html"
)
type Posts []MarkdownPage
type FileMapper struct {
SourceFile string
DestinationFile string
Filename string
Filetype string
}
type Context struct {
SiteTitle string `json:"site_title"`
SiteDescription string `json:"site_description"`
Posts *Posts
CurrentPage MarkdownPage
}
type Page interface {
GetType() string
GetFinalHTML() template.HTML
GetTitle() string
}
type MarkdownPage struct {
Title string
Slug string
Date time.Time
Category string
Filename string
DestinationFile string
RelLink string
RawMarkdown string // This is the Markdown sans header
FinalHTML template.HTML // This is the final HTML after the Markdown parser
}
type HTMLPage struct {
RawHTML string
Filename string
FinalHTML template.HTML
}
func (p MarkdownPage) GetType() string {
return TypeMarkdown
}
func (p MarkdownPage) GetFinalHTML() template.HTML {
return p.FinalHTML
}
func (p MarkdownPage) GetTitle() string {
return p.Title
}
func (p MarkdownPage) FormattedDate() string {
return p.Date.Format(time.ANSIC)
}
func (p HTMLPage) GetType() string {
return TypeHTML
}
func (p HTMLPage) GetFinalHTML() template.HTML {
return p.FinalHTML
}
func (p HTMLPage) GetTitle() string {
return ""
}
// NewMarkdownPage takes a string of raw markdown content with an optional header
// in the form of:
//
// ###
// title: this is a post title
// date: 2015-03-20 15:35 PDT
// category: computers
// ###
//
// This will parse out the header and return a new MarkdownPage instance with
// the header fields and raw Markdown content sans-header.
//
// :kyleterry: TODO: thing about returning an error here if something goes wrong
// during parsing of the header. Currently `log.Fatal`s.
func NewMarkdownPage(filename string, rawContent string) MarkdownPage {
log.Printf("Parsing %s", filename)
sd := strings.Split(rawContent, "\n")
page := MarkdownPage{}
page.Filename = filename
if sd[0] == "###" {
sd = sd[1:]
for index, line := range sd {
if line == "###" {
sd = sd[index+1:]
break
}
sl := strings.SplitN(line, ":", 2)
sl[1] = strings.Trim(sl[1], " ")
switch sl[0] {
case "title":
page.Title = sl[1]
case "date":
parsedTime, err := time.Parse(time.RFC822, sl[1])
if err != nil {
log.Fatal("Malformed date string: can't parse date.")
}
page.Date = parsedTime
case "category":
page.Category = sl[1]
default:
// Just ignore things that we don't know about
continue
}
}
}
if len(sd) == 0 {
log.Fatalf("Something went wrong parsing %s: Possible Malformed header. Reached EOF.", filename)
}
page.RawMarkdown = strings.Join(sd, "\n")
page.Slug = slug.Slug(page.Title)
page.DestinationFile = path.Join(DefaultDestinationDir, "posts", page.Slug+".html")
page.RelLink = "posts/" + page.Slug + ".html"
return page
}
func NewHTMLPage(filename string, rawContent string) HTMLPage {
return HTMLPage{RawHTML: rawContent, Filename: filename}
}
func NewContext() *Context {
return &Context{}
}
func NewContextFromSolarwindfile(path string) *Context {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("There was a problem reading the Solarwindfile")
}
context := NewContext()
if err := json.Unmarshal(content, &context); err != nil {
log.Fatal("There was a problem parsing the Solarwindfile")
}
if context.SiteTitle == "" {
context.SiteTitle = "Solarwind Site"
}
if context.SiteDescription == "" {
context.SiteDescription = "This is a static site generated with Solarwind: https://github.com/kyleterry/solarwind"
}
return context
}
func IsMarkdown(ext string) bool {
if ext == TypeMarkdown || ext == TypeMarkdownLong {
return true
}
return false
}
func ListFiles(dir string, extension string) []FileMapper {
files, err := filepath.Glob(fmt.Sprintf("%s/*.%s", dir, extension))
if err != nil {
log.Fatal("There was an error globbing for files")
}
fileMaps := []FileMapper{}
for _, f := range files {
fm := FileMapper{}
fm.Filetype = extension
fm.Filename = strings.Split(filepath.Base(f), ".")[0]
switch dir {
case DefaultContentDir:
fm.SourceFile = f
fm.DestinationFile = path.Join(DefaultDestinationDir, fm.Filename+".html")
case path.Join(DefaultContentDir, "posts"):
fm.SourceFile = f
fm.DestinationFile = path.Join(DefaultDestinationDir, "posts", fm.Filename+".html")
}
fileMaps = append(fileMaps, fm)
}
return fileMaps
}
func MakePublicDir(dir string) {
if _, err := os.Stat(dir); err == nil {
err := os.RemoveAll(dir)
if err != nil {
log.Fatalf("Could not remove dir %s", dir)
}
}
err := os.MkdirAll(path.Join(dir, "posts"), 0755)
if err != nil {
log.Fatal(err)
}
}
func GenerateHTMLFromMarkdown(rawMarkdown string) string {
return string(blackfriday.MarkdownCommon([]byte(rawMarkdown)))
}
func MakeFinalPage(htmlContent string) string {
return ""
}
// Cowboy error handling
func CopyAssets(source, dest string) {
err := os.MkdirAll(dest, 0755)
if err != nil {
log.Fatal(err)
}
err = filepath.Walk(source, func(p string, info os.FileInfo, err error) error {
if p == source {
return nil
}
if info.IsDir() {
os.Mkdir(path.Join(dest, info.Name()), info.Mode())
return nil
}
new_path := strings.Replace(p, source, dest, 1)
r, err := os.Open(p)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(new_path)
if err != nil {
return err
}
if _, err := io.Copy(w, r); err != nil {
w.Close()
return err
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
// Sorting
func SortPostsByDate(posts Posts) Posts {
sort.Sort(posts)
return posts
}
func (p Posts) Len() int {
return len(p)
}
func (p Posts) Less(i, j int) bool {
return p[i].Date.After(p[j].Date)
}
func (p Posts) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
// GenerateCommand code
type GenerateCommand struct {
Ui cli.Ui
}
func (c *GenerateCommand) Help() string {
helpText := `
usage: solarwind generate
This command will build a solarwind project and put everything in ./public.
`
return helpText
}
func (c *GenerateCommand) Synopsis() string {
return "Builds a static site from markdown content."
}
func (c *GenerateCommand) Run(args []string) int {
if _, err := os.Stat(Solarwindfile); err != nil {
log.Fatal("You need to create a `Solarwindfile` in the directory you'd like to serve as your site.")
}
log.Println("Making public directory")
MakePublicDir(DefaultDestinationDir)
context := NewContextFromSolarwindfile(DefaultSolarwindfilePath)
var posts Posts
templateCache := make(map[string][]byte)
log.Println("Caching templates")
for _, tmpl_file := range []string{"index.html", "page.html", "post.html"} {
name := strings.SplitN(tmpl_file, ".", 2)[0]
cache, err := ioutil.ReadFile(path.Join(DefaultTemplateDir, tmpl_file))
if err != nil {
panic(err)
}
templateCache[name] = cache
}
log.Println("Collecting content")
rootShortMarkdownFiles := ListFiles(DefaultContentDir, TypeMarkdown)
rootLongMarkdownFiles := ListFiles(DefaultContentDir, TypeMarkdownLong)
rootMarkdownFiles := append(rootShortMarkdownFiles, rootLongMarkdownFiles...)
rootHTMLFiles := ListFiles(DefaultContentDir, TypeHTML)
postShortMarkdownFiles := ListFiles(DefaultPostsDir, TypeMarkdown)
postLongMarkdownFiles := ListFiles(DefaultPostsDir, TypeMarkdownLong)
postMarkdownFiles := append(postShortMarkdownFiles, postLongMarkdownFiles...)
fileCount := len(rootMarkdownFiles) + len(rootHTMLFiles) + len(postMarkdownFiles)
log.Printf("Found %d files", fileCount)
// Merge root files so one loop is needed
rootFilesToRead := append(rootMarkdownFiles, rootHTMLFiles...)
log.Println("Parsing posts")
for _, file := range postMarkdownFiles {
content, err := ioutil.ReadFile(file.SourceFile)
if err != nil {
log.Fatalf("There was an error reading the file: %s", err)
}
post := NewMarkdownPage(file.Filename, string(content))
post.FinalHTML = template.HTML(GenerateHTMLFromMarkdown(post.RawMarkdown))
posts = append(posts, post)
}
sort.Sort(posts)
context.Posts = &posts
log.Println("Parsing pages and generating site")
for _, file := range rootFilesToRead {
content, err := ioutil.ReadFile(file.SourceFile)
if err != nil {
log.Fatalf("There was an error reading the file: %s", err)
}
var page Page
if IsMarkdown(file.Filetype) {
md := NewMarkdownPage(file.Filename, string(content))
md.FinalHTML = template.HTML(GenerateHTMLFromMarkdown(md.RawMarkdown))
page = md
} else {
html := NewHTMLPage(file.Filename, string(content))
html.FinalHTML = template.HTML(string(content))
page = html
}
t := template.Must(template.New("page").Parse(string(templateCache["index"]) + string(templateCache["page"]) + string(page.GetFinalHTML())))
// TODO: make custom io.Writer to write the template directly to a file
b := &bytes.Buffer{}
t.Execute(b, context)
err = ioutil.WriteFile(file.DestinationFile, b.Bytes(), 0755)
if err != nil {
panic(err)
}
}
for _, post := range *context.Posts {
context.CurrentPage = post
t := template.Must(template.New("page").Parse(string(templateCache["index"]) + string(templateCache["post"])))
b := &bytes.Buffer{}
t.Execute(b, context)
err := ioutil.WriteFile(post.DestinationFile, b.Bytes(), 0755)
if err != nil {
panic(err)
}
}
log.Println("Copying static assets")
CopyAssets(DefaultStaticDir, path.Join(DefaultDestinationDir, "static"))
log.Println("Done!")
return 0
}