-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindices.go
324 lines (300 loc) · 7.01 KB
/
indices.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
package main
import (
"bytes"
"gopkg.in/russross/blackfriday.v2"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
// uses text/template to allow for scripts or HTML in .md files
"text/template"
)
type Index struct {
Path string
Title string
Content string
VisiblePath string
NavItems map[string]string
}
type Item struct {
Path string
Title string
Content string
VisiblePath string
}
var indices map[string]*Index
var items []Item
// reads a mardkown file, gets HTML from blackfriday, and returns the HTML and
// title
func markdownToHtml(path string, info os.FileInfo) (string, string) {
markdown, err := ioutil.ReadFile(path)
markdown = bytes.Replace(markdown, []byte("\r"), nil, -1)
lines := strings.Split(string(markdown), "\n")
unsafeTitle := lines[0]
regex, err := regexp.Compile("[^a-zA-Z0-9 .'-]+")
if err != nil {
panic(err)
}
title := regex.ReplaceAllString(unsafeTitle, "")
if []rune(title)[0] == ' ' {
title = strings.Replace(title, " ", "", 1)
}
if err != nil {
panic(err)
}
var html []byte = blackfriday.Run(markdown, blackfriday.WithExtensions(blackfriday.BackslashLineBreak))
return string(html), title
}
func replaceSuffix(str string, old string, new string) string {
str = strings.TrimRight(str, old)
return str + new
}
func copyFile(srcPath string, dstPath string) int64 {
reader, err := os.Open(srcPath)
if err != nil {
panic(err)
}
defer reader.Close()
writer, err := os.Create(dstPath)
if err != nil {
panic(err)
}
defer writer.Close()
length, err := io.Copy(writer, reader)
if err != nil {
panic(err)
}
return length
}
func processFilesIn(dir string) {
indices = make(map[string]*Index)
items = []Item{}
processFile := func(path string, info os.FileInfo, err error) error {
path = filepath.ToSlash(path)
if err != nil {
panic(err)
}
path = filepath.ToSlash(path)
staticPath := strings.Replace(path, dir, dir+"_static", 1)
relPath := strings.Split(path, dir)[1]
visiblePath := strings.TrimRight(relPath, info.Name())
// handle root directory visibility
if visiblePath == "" {
visiblePath = "/"
} else if info.IsDir() {
// if this isn't the root but is a directory, the visiblePath should include
// its name
visiblePath = visiblePath + info.Name() + "/"
}
// create an "empty" index for every directory
if info.IsDir() {
err := os.Mkdir(staticPath, os.ModePerm)
if err != nil {
panic(err)
}
index := Index{
relPath + "/index.html",
"Filed in " + visiblePath,
"",
visiblePath,
make(map[string]string, 0)}
indices[visiblePath] = &index
if visiblePath != "/" {
withoutTrailingSlash := strings.TrimRight(visiblePath, "/")
pathWords := strings.Split(withoutTrailingSlash, "/")
parentPathWords := pathWords[:len(pathWords)-1]
parentVisiblePath := strings.Join(parentPathWords, "/") + "/"
indices[parentVisiblePath].NavItems[relPath+"/"] = "Filed in " + visiblePath
}
} else if filepath.Ext(info.Name()) == ".md" {
// this is a markdown file; send it off
var htmlPath string
html, title := markdownToHtml(path, info)
if info.Name() == "index.md" {
htmlPath = replaceSuffix(relPath, ".md", ".html")
indices[visiblePath].Path = htmlPath
indices[visiblePath].Title = title
indices[visiblePath].Content = html
indices[visiblePath].VisiblePath = visiblePath
if visiblePath != "/" {
withoutTrailingSlash := strings.TrimRight(visiblePath, "/")
pathWords := strings.Split(withoutTrailingSlash, "/")
parentPathWords := pathWords[:len(pathWords)-1]
parentVisiblePath := strings.Join(parentPathWords, "/") + "/"
indices[parentVisiblePath].NavItems[strings.TrimRight(relPath, "index.md")] = title
}
} else {
htmlPath = replaceSuffix(relPath, ".md", "/index.html")
// make a new folder and index.html for clean URLs
err := os.Mkdir(strings.TrimRight(staticPath, ".md"), os.ModePerm)
if err != nil {
if !os.IsExist(err) {
panic(err)
}
}
item := Item{
htmlPath,
title,
html,
visiblePath}
items = append(items, item)
indices[visiblePath].NavItems[strings.TrimRight(relPath, ".md")+"/"] = item.Title
}
if err != nil {
panic(err)
}
} else {
// if it's not markdown or a directory, include it in the final static site
// note: it might be overwritten by another file!
copyFile(path, staticPath)
}
return nil
}
// prepare structs
err := filepath.Walk(dir, processFile)
if err != nil {
panic(err)
}
}
func writeHtml(dir string) {
var indexHtml = `
<!DOCTYPE html>
<html lang=en>
<head>
<title>{{.Title}}</title>
<meta name="viewport" content="width=device-width">
<link rel=icon href=data:,>
<style>
body {
word-wrap: break-word;
font-family: Verdana, Arial, sans-serif;
font-size: 1.5em;
color: #3b3837;
}
main {
max-width: 70ch;
padding: 2ch;
margin: auto;
}
img {
display: block;
margin-left: auto;
margin-right: auto;
max-width: 70%;
}
nav {
list-style-type: none;
padding: 1rem 0;
}
a {
text-decoration: none;
outline: 0;
}
a:hover {
text-decoration: underline;
}
::selection {
background-color: #e5e5e5;
}
</style>
</head>
<body>
<main>
{{.Content}}
</hr>
<nav>
<h2>Filed in <a href="{{.VisiblePath}}">{{.VisiblePath}}</a></h2>
{{ range $Path, $Title := .NavItems }}
<li><a href="{{ $Path }}">{{ $Title }}</a></li>
{{end}}
</nav>
</main>
</body>
</html>
`
var itemHtml = `
<!DOCTYPE html>
<html lang=en>
<head>
<title>{{.Title}}</title>
<meta name="viewport" content="width=device-width">
<link rel=icon href=data:,>
<style>
body {
word-wrap: break-word;
font-family: Verdana, Arial, sans-serif;
font-size: 1.5em;
color: #3b3837;
}
main {
max-width: 70ch;
padding: 2ch;
margin: auto;
}
img {
display: block;
margin-left: auto;
margin-right: auto;
max-width: 70%;
}
a {
text-decoration: none;
outline: 0;
}
a:hover {
text-decoration: underline;
}
::selection {
background-color: #e5e5e5;
}
</style>
</head>
<body>
<main>
{{.Content}}
<h4>Filed in <a href="{{.VisiblePath}}">{{.VisiblePath}}</a></h4>
</main>
</body>
</html>
`
for _, index := range indices {
tmpl, err := template.New("index").Parse(indexHtml)
if err != nil {
panic(err)
}
writer, err := os.Create(dir + index.Path)
if err != nil {
panic(err)
}
err = tmpl.Execute(writer, index)
if err != nil {
panic(err)
}
}
for _, item := range items {
tmpl, err := template.New("item").Parse(itemHtml)
if err != nil {
panic(err)
}
writer, err := os.Create(dir + item.Path)
if err != nil {
panic(err)
}
err = tmpl.Execute(writer, item)
if err != nil {
panic(err)
}
}
}
func main() {
args := os.Args
if len(args) != 2 {
panic("Please provide exactly one argument, the directory with your content.")
}
dir := filepath.ToSlash(args[1])
processFilesIn(dir)
staticDir := dir + "_static"
writeHtml(staticDir)
}