-
Notifications
You must be signed in to change notification settings - Fork 0
/
regen_md.go
127 lines (113 loc) · 2.74 KB
/
regen_md.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
package main
import (
"bytes"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var (
mdOutWhitelist = make(map[string]bool)
)
func isMarkdownFile(path string) bool {
path = strings.ToLower(path)
return strings.HasSuffix(path, ".md")
}
func getFilesRecur(dir string, shouldInclude func(s string) bool) ([]string, error) {
var res []string
dirsToVisit := []string{dir}
for len(dirsToVisit) > 0 {
dir := dirsToVisit[0]
dirsToVisit = dirsToVisit[1:]
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
return res, err
}
for _, fi := range fileInfos {
path := filepath.Join(dir, fi.Name())
if fi.IsDir() {
dirsToVisit = append(dirsToVisit, path)
continue
}
if fi.Mode().IsRegular() {
if (shouldInclude == nil) || shouldInclude(path) {
res = append(res, path)
}
}
}
}
return res, nil
}
func parseMd(d []byte) ([]byte, map[string]string) {
meta := make(map[string]string)
d = normalizeNewlines(d)
lines := bytes.Split(d, []byte{10})
for len(lines) > 0 {
l := lines[0]
parts := bytes.SplitN(l, []byte{':'}, 2)
if len(parts) != 2 {
break
}
k := string(bytes.TrimSpace(parts[0]))
k = capitalize(k)
v := strings.TrimSpace(string(parts[1]))
meta[k] = v
lines = lines[1:]
}
// remove empty lines at the top
for len(lines) > 0 {
l := lines[0]
if len(l) > 0 {
break
}
lines = lines[1:]
}
d = bytes.Join(lines, []byte{10})
return d, meta
}
const keyTitle = "Title"
func mdToHTML(mdFile, templateFile, htmlFile string) {
md, err := ioutil.ReadFile(mdFile)
panicIfErr(err)
md, meta := parseMd(md)
body := markdownToHTML(md, "")
model := make(map[string]interface{})
title := meta[keyTitle]
model[keyTitle] = title
model["BodyHTML"] = template.HTML(body)
templateName := filepath.Base(templateFile)
templates = template.Must(template.ParseFiles(templateFile))
var buf bytes.Buffer
err = templates.ExecuteTemplate(&buf, templateName, model)
panicIfErr(err)
err = ioutil.WriteFile(htmlFile, buf.Bytes(), 0644)
panicIfErr(err)
verbose("%s => %s\n", mdFile, htmlFile)
}
// template is a _md.tmpl.html file in the same directory
func findMdTemplate(mdFile string) string {
dir := filepath.Dir(mdFile)
path := filepath.Join(dir, "_md.tmpl.html")
_, err := os.Stat(path)
if err != nil {
return ""
}
return path
}
func regenMd() {
mdFiles, err := getFilesRecur("www", isMarkdownFile)
panicIfErr(err)
for _, mdFile := range mdFiles {
htmlFile := replaceExt(mdFile, ".html")
templateFile := findMdTemplate(mdFile)
if templateFile == "" {
verbose("%s : skipping because no template file\n", mdFile)
continue
}
verbose("%s\n", mdFile)
mdToHTML(mdFile, templateFile, htmlFile)
verbose("Whitelisted: %s\n", htmlFile)
mdOutWhitelist[htmlFile] = true
}
}