-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (63 loc) · 2.21 KB
/
main.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
package main
import (
"strings"
"path/filepath"
"io/ioutil"
"os"
"regexp"
"github.com/russross/blackfriday"
"github.com/flosch/pongo2"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil { return true, nil }
if os.IsNotExist(err) { return false, nil }
return true, err
}
func main() {
public_html, err := exists("./public_html/")
check(err)
if !public_html {
os.Mkdir("./public_html/", 0755)
os.Mkdir("./public_html/blog/", 0755)
os.Mkdir("./public_html/assets/", 0755)
ioutil.WriteFile("./public_html/assets/styles.css", []byte(""), 0644)
}
archive := make([]map[string]string, 0)
files, _ := ioutil.ReadDir("./blog/")
for _, filename := range files {
// Ignore drafts
if strings.HasPrefix(filename.Name(), "draft") {
continue
}
filecontent, err := ioutil.ReadFile("./blog/" + filename.Name())
check(err)
// Read the metadata
r, _ := regexp.Compile("(?m)^Title: (.*)$")
title := r.FindStringSubmatch(string(filecontent))[1]
filecontent = []byte(r.ReplaceAllString(string(filecontent), ""))
r, _ = regexp.Compile("(?m)^Published: (.*)$")
published := r.FindStringSubmatch(string(filecontent))[1]
filecontent = []byte(r.ReplaceAllString(string(filecontent), ""))
tpl, err := pongo2.FromFile("detail.html")
check(err)
f, err := tpl.Execute(pongo2.Context{"title": title, "published": published, "content": string(blackfriday.MarkdownCommon(filecontent))})
check(err)
finalfilename := strings.TrimSuffix(filename.Name(), filepath.Ext(filename.Name()))
ioutil.WriteFile("./public_html/blog/" + finalfilename + ".html", []byte(f), 0644)
m := make(map[string]string)
m["url"] = "./blog/" + finalfilename + ".html"
m["title"] = title
archive = append(archive, m)
}
tpl, err := pongo2.FromFile("index.html")
check(err)
f, err := tpl.Execute(pongo2.Context{"items": archive})
check(err)
ioutil.WriteFile("./public_html/index.html", []byte(f), 0644)
}