-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
363 lines (341 loc) · 10.9 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
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
package main
import (
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/beeceej/beeceej.com/posts"
)
func contentPagePath(file string) string {
return filepath.Join("content", file)
}
func commitHash() string {
hash := os.Getenv("GIT_COMMIT_HASH")
if hash == "" {
panic("no commit hash")
}
return hash
}
var (
templates = map[string]RenderData{
"robots.txt": {
PageToRender: "robots.txt",
},
"about.html": {
Description: "about beeceej",
Keywords: []string{},
PageID: "about",
PageToRender: "index.html",
},
"error.html": {
Description: "How'd you get here? this is an error page",
Keywords: []string{},
PageID: "error",
PageToRender: "index.html",
},
"contact.html": {
Description: "Contact beeceej",
Keywords: []string{},
PageID: "contact",
PageToRender: "index.html",
},
"index.css": {
Keywords: []string{},
PageID: "index-css",
PageToRender: "index.css",
},
"notes/l.html": {
Keywords: []string{},
PageID: "notes-l",
PageToRender: "index.html",
Other: struct{ Posts []posts.Post }{
Posts: posts.Posts,
},
},
"notes/0-hello-world.html": {
ContentPagePath: contentPagePath("0-hello-world.html"),
Description: "First blog post",
Keywords: []string{
"programming",
"Hello world",
"simple",
"frontend",
"blog",
"go",
"golang",
},
Other: posts.HelloWorld,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/1-mountain-goats.html": {
ContentPagePath: contentPagePath("1-mountain-goats.html"),
Description: "Mountain Goat escapes enclosure, bound for greener greener grass, and taller mountains",
Keywords: []string{
"programming",
"allegory",
"goat",
"cloud",
"mountain",
},
Other: posts.MountainGoat,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/2-ackerman-function-expansions.html": {
ContentPagePath: contentPagePath("2-ackerman-function-expansions.html"),
Description: "Ackermann function expansion in scheme",
Keywords: []string{
"programming",
"lisp",
"guile",
"scheme",
"sicp",
"mit",
},
Other: posts.Ackermann,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/3-good-advice.html": {
ContentPagePath: contentPagePath("3-good-advice.html"),
Description: "Guy Clark on good advice",
Keywords: []string{
"Guy Clark",
"outlaw",
"music",
"random",
"thoughts",
"mit",
"texas",
"country",
},
Other: posts.GoodAdvice,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/4-matching-parens.html": {
ContentPagePath: contentPagePath("4-matching-parens.html"),
Description: "javascript demo of parentheses matching",
Keywords: []string{},
Other: posts.MatchingParens,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/5-peppers-intro.html": {
ContentPagePath: contentPagePath("5-peppers-intro.html"),
Description: "Log of growing peppers entry number 1",
Keywords: []string{"Hot pepper", "capsaicin", "Trinidad Moruga Scorpion", "Aerogarden"},
Other: posts.PeppersIntro,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/6-mutable-call-args-in-python.html": {
ContentPagePath: contentPagePath("6-mutable-call-args-in-python.html"),
Description: "How Mutable arguments in python caused test assertions to behave unexpectedly",
Keywords: []string{"python", "mutability"},
Other: posts.MutableCallArgsInPython,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/7-peppers-part-2.html": {
ContentPagePath: contentPagePath("7-peppers-part-2.html"),
Description: "Log of growing peppers entry number 2",
Keywords: []string{"Hot pepper", "capsaicin", "Trinidad Moruga Scorpion", "Aerogarden"},
Other: posts.PeppersPartTwo,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/8-the-problem-with-modern-web-development.html": {
ContentPagePath: contentPagePath("8-the-problem-with-modern-web-development.html"),
Description: "Maintainability of large javascript based web projects over time is called into question",
Keywords: []string{"React", "Javascript", "modern", "frontend", "web", "development"},
Other: posts.ModernFrontendProblems,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/9-maple-leaf-coffee-nicaraguan-el-finca.html": {
ContentPagePath: contentPagePath("9-maple-leaf-coffee-nicaraguan-el-finca.html"),
Description: "Maple leaf Coffee Roasters, Nicaraguan El Finca Review",
Keywords: []string{"coffee", "light roast", "aeropress", "light roast", "brew", "maple leaf coffee roasters"},
Other: posts.CoffeeFromAnOldCoworker,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/10-peppers-part-3-fruits.html": {
ContentPagePath: contentPagePath("10-peppers-part-3-fruits.html"),
Description: "Log of growing peppers entry number 3",
Keywords: []string{"Hot pepper", "capsaicin", "Trinidad Moruga Scorpion", "Aerogarden"},
Other: posts.PeppersPartThree,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/11-alacritty-spawn-new-window.html": {
ContentPagePath: contentPagePath("11-alacritty-spawn-new-window.html"),
Description: "Spawning New Windows on Fedora 35 with Alacritty 0.9.0",
Keywords: []string{"Terminal Emulator", "alacritty", "linux"},
Other: posts.AlacrittySpawnNewWindow,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/12-tic-tac-toe.html": {
ContentPagePath: contentPagePath("12-tic-tac-toe.html"),
Description: "tic-tac-toe game",
Keywords: []string{"game", "tic-tac-toe", "javsacript"},
Other: posts.TicTacToe,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/13-peppers-part-four-end-of-a-season.html": {
ContentPagePath: contentPagePath("13-peppers-part-four-end-of-a-season.html"),
Description: "Peppers End of 2021",
Keywords: []string{"Hot pepper", "capsaicin", "Trinidad Moruga Scorpion", "Aerogarden"},
Other: posts.PeppersPartFour,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/14-khao-piak-sen.html": {
ContentPagePath: contentPagePath("14-khao-piak-sen.html"),
Description: "Lao Chicken Noodle Soup",
Keywords: []string{"Lao", "Noodle", "Soup", "chicken"},
Other: posts.KhaoPiakSen,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/15-lgtm.html": {
ContentPagePath: contentPagePath("15-lgtm.html"),
Description: "LGTM a Silly OCAML github action",
Keywords: []string{"ocaml", "oss", "github", "github action"},
Other: posts.LGTM,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/16-music-background.html": {
ContentPagePath: contentPagePath("16-music-background.html"),
Description: "Background of Musical Interests",
Keywords: []string{"music"},
Other: posts.MusicBackground,
PageID: "notes-note",
PageToRender: "index.html",
},
"notes/17-emacs-and-back-again.html": {
ContentPagePath: contentPagePath("17-emacs-and-back-again.html"),
Description: "Emacs and back Again",
Keywords: []string{"emacs", "editor", "ide", "code"},
Other: posts.EmacsAndBackAgain,
PageID: "notes-note",
PageToRender: "index.html",
},
}
)
// RenderData acts as a manifest for making run time decisions on which files to render,
// and how to render a given file
type RenderData struct {
// CommitHash is the commit hash of the repository, used for
// exposing the version of the statically generated site.
CommitHash string
// ContentPagePath is the path of the content page
// This page will be loaded in if requested, and used as input into a template file
ContentPagePath string
// Description is the description of a page, inserted into the description meta tag
Description string
// Keywords is a set of keywords inserted into the keywords meta tag
Keywords []string
// Other is an arbitrary structure used for rendering dynamic pages
Other interface{}
// PageID is used for the templating engine to
// make runtime decisions on which template to render
PageID string
// PageToRender is the name of template to use when rendering.
PageToRender string
}
func main() {
filename := filepath.Base(os.Args[1])
filename = filepath.Join(strings.Split(filepath.FromSlash(os.Args[1]), string(os.PathSeparator))[1:]...)
path := filepath.Dir(os.Args[1])
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
panic(err.Error())
}
must(makeOutputFile(filename))
}
func augmentRenderData(d *RenderData) error {
if d == nil { // No RenderData, so nothing to do
return nil
}
if d.ContentPagePath != "" {
f, err := os.Open(d.ContentPagePath)
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
postData := d.Other.(posts.Post)
postData.Content = template.HTML(string(b))
d.Other = postData
}
d.CommitHash = commitHash()
return nil
}
func makeOutputFile(path string) error {
var (
f *os.File
err error
tpl *template.Template
)
renderData, exists := templates[path]
if !exists {
return nil
}
if err = augmentRenderData(&renderData); err != nil {
return err
}
if renderData.PageToRender != "" {
if f, err = os.Create(filepath.Join("output", path)); err != nil {
return err
}
defer func() {
if err = f.Close(); err != nil {
panic(err.Error())
}
}()
tpl = template.Must(findAndParseTemplates("templates", nil))
return tpl.Lookup(renderData.PageToRender).Execute(f, renderData)
}
return nil
}
func must(errs ...error) {
for _, err := range errs {
if err != nil {
panic(err.Error())
}
}
}
// findAndParseTemplates walks the file system recursively parsing templates as it goes.
func findAndParseTemplates(rootDir string, funcMap template.FuncMap) (*template.Template, error) {
cleanRoot := filepath.Clean(rootDir)
root := template.New("")
err := filepath.Walk(cleanRoot, func(path string, info os.FileInfo, walkErr error) (err error) {
if walkErr != nil {
return walkErr
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) == ".png" || filepath.Ext(path) == ".webp" || filepath.Ext(path) == ".jpg" {
return nil
}
var templateBytes []byte
if templateBytes, err = ioutil.ReadFile(path); err != nil {
return err
}
name := path[len(cleanRoot)+1:]
t := root.New(name).Funcs(funcMap)
t.Funcs(template.FuncMap{"JoinStr": strings.Join})
_, err = t.Parse(string(templateBytes))
return err
})
return root, err
}