-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.pls
executable file
·382 lines (294 loc) · 11.4 KB
/
build.pls
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
#!/usr/bin/env polaris
options {
"--force-rebuild" as forceRebuild: "Always rebuild every file even if it has not changed"
}
ensure("pandoc")
ensure("sassc")
ensure("jq")
ensure("dot")
module List = import("../polaris/lib/list.pls")
module Async = import("../polaris/lib/async.pls")
let idCounter = ref 0
let freshId() = {
idCounter := idCounter! + 1
idCounter! - 1
}
!mkdir "-p" ".build"
data FloraEnv = String
let _ = !flora "--write-env" ".build/flora_env_base" "flora/base.flora"
let newFloraEnv() = {
let file = ".build/flora_env_${toString(freshId())}"
!cp ".build/flora_env_base" file
FloraEnv(file)
}
let runFlora(flags : List(String), content) = {
let continuationFile = ".build/flora_continuation_" ~ toString(freshId())
let flags = List.append(["--effects", continuationFile], flags)
let handleEffect(stdout) = {
let effect = stdout | jq "-er" ".effect | strings"
let result = match effect {
"include" -> {
let file = stdout | jq "-er" ".arguments[0] | strings"
!cat file
}
"dot" -> {
let argument = stdout | jq "-er" ".arguments[0] | strings"
let svgOutput = argument | dot "-Tsvg"
let preprocessed =
# we would like to do styling ourselves, thank you very much
regexpReplace("(stroke=\"black\")|(fill=\"black\")", "",
# remove the doctype that graphviz adds for some reason
regexpReplace("<!DOCTYPE([^a]|a)*?>", "", svgOutput))
"<div class=\"dot-output\">${preprocessed}</div>"
}
_ -> fail("Unhandled effect: " ~ effect)
}
try {
!flora flags "--continue" "--string" result continuationFile
} with {
CommandFailure(output) as error -> {
if (output.exitCode == 100) then {
handleEffect(output.stdout)
} else {
raise error
}
}
}
}
try {
let result = content | flora flags
result
} with {
CommandFailure(output) as error -> {
if (output.exitCode == 100) then {
handleEffect(output.stdout)
} else {
raise error
}
}
}
}
let evalFlora(env : FloraEnv, code) = {
runFlora(["--read-env", env!], code)
}
let defineFloraVariablesRaw(env : FloraEnv, bindings) = {
let renderBinding((name, binding)) = match binding {
String(value) -> ["--bind", name, "--string", value]
FilePath(path) -> ["--bind", name, "--file", path]
}
let definitionArgs = List.concatMap(renderBinding, bindings)
let _ = runFlora(List.append(["--read-env", env!, "--write-env", env!], definitionArgs), "nil")
}
let defineFloraVariablesWith(env, bindings) = {
let generateFileFor(binding) = match binding {
File(contents) -> {
let file = ".build/arg_" ~ toString(freshId())
writeFile(file, contents)
FilePath(file)
}
x -> x
}
let bindings = List.map(\(name, binding) -> (name, generateFileFor(binding)), bindings)
defineFloraVariablesRaw(env, bindings)
}
let defineFloraVariables(env, vars) = {
defineFloraVariablesRaw(env, List.map(\(name, str) -> (name, String(str)), vars))
}
# TODO: Move this to the standard library
let concatMap : forall a b. (a -> List(b), List(a)) -> List(b)
let concatMap(f, list) = List.foldr(\x r -> List.append(f(x), r), [], list)
let at : forall a. (Number, List(a)) -> < Just(a), Nothing >
let at(index, list) = match (index, list) {
(_, []) -> Nothing
(0, (x :: _)) -> Just(x)
(index, (_ :: xs)) -> at(index - 1, xs)
}
let lookup : forall k v. (k, List((k, v))) -> < Just(v), Nothing >
let lookup(key, list) = match List.find(\(k, v) -> k == key, list) {
Nothing -> Nothing
Just((_, value)) -> Just(value)
}
let doesFileExist(file) = {
try {
!bash "-c" ("stat '${file}' > /dev/null 2> /dev/null")
true
} with {
CommandFailure(_) -> false
}
}
let hasChanged : { source : String, target : String } -> Bool
let hasChanged(params) = {
if forceRebuild then
true
else {
# TODO: Use record destructuring here
let source = params.source
let target = params.target
if not (doesFileExist(source)) then
fail("hasChanged called on non-existent source file '${source}'")
else if not (doesFileExist(target)) then
true
else {
let sourceTimestamp = parseInt(!stat "-c" "%Y" source)
let targetTimestamp = parseInt(!stat "-c" "%Y" target)
sourceTimestamp > targetTimestamp
}
}
}
let extractTags : String -> List((String, String))
let extractTags(file) = {
let keys = lines(!grep "-Po" "(?<=<!--).+?(?=:)" file);
let values = lines(!grep "-Po" "(?<=:).+?(?=-->)" file);
List.zip(keys, values);
};
let escapeHTML(content) = {
let toReplace =
[ ("<", "<")
, (">", ">")
, ("<!--.*?-->", "")
]
List.foldr(\x r -> regexpReplace(List.fst(x), List.snd(x), r), content, toReplace)
};
let collectTooltips : String -> List((String, String))
let collectTooltips(content) = {
let definitionSection = match regexpMatchGroups("<!--#tooltips\\s*((?:[^a]|a)+?)\\s*-->", content) {
[] -> ""
[[_, section]] -> section
sections -> fail("Invalid tooltip definition sections (there might be more than one?): ${toString(sections)}")
}
let tooltips = regexpMatchGroups("°(.+?)°\\s*:\\s*([^°]*)", definitionSection)
concatMap(\[_, def, content] -> [(def, content), (escapeHTML(def), content)], tooltips)
}
let replaceTags : (List((String, String)), String) -> String
let replaceTags(tags, content) = {
List.foldr(\(key, value) r -> replace("{{${key}}}", value, r), content, tags)
};
let tooltipTemplate = !cat "templates/tooltip.html"
let replaceTooltips : (List((String, String)), String) -> String
let replaceTooltips(tooltips, content) = {
let addTooltipHTML(name) = match lookup(name, tooltips) {
Nothing -> fail("Invalid tooltip: °" ~ name ~ "°")
Just(tooltipContent) -> {
replaceTags([("name", name), ("tooltip", tooltipContent)], tooltipTemplate)
}
}
regexpTransformAll("°(.*?)°", \[_, x] -> addTooltipHTML(x), content)
}
let mdTags = [
("lang", !cat "static-html/lang-tag-start.html")
, ("/lang", !cat "static-html/lang-tag-end.html")
, ("haskell-specific", !cat "static-html/haskell-specific-start.html")
, ("/haskell-specific", !cat "static-html/haskell-specific-end.html")
]
let pandoc(contents) = {
contents | pandoc
"--katex"
"--syntax-definition=highlighting/polaris.xml"
"--syntax-definition=highlighting/flora.xml"
}
let evalTemplate(template, env : FloraEnv) = {
regexpTransformAll("\\{\\{((?:a|[^a])*?)\\}\\}", \[_, x] -> evalFlora(env, x), template)
}
let buildPage(templateFile, outputFile, env) = {
print("Building page: " ~ outputFile)
let output = evalTemplate(!cat ("templates/" ~ templateFile), env)
writeFile(outputFile, output)
}
let buildPost(name) = {
# TODO: This is doing a lot of unnecessary work even if the file has not changed
print("Building post: " ~ name);
let markdownPath = "md/${name}.md";
let htmlPath = "posts/${name}.html";
let timestampPath = ".build/${name}.timestamp"
let tooltips = collectTooltips(!cat markdownPath)
let floraEnv = newFloraEnv()
let evaluatedMarkdown = regexpTransformAll
( "\\{\\{((?:a|[^a])*?)\\}\\}"
, \[_, x] ->
runFlora(["--read-env", floraEnv!, "--write-env", floraEnv!], x)
, !cat markdownPath)
let title = evalFlora(floraEnv, "title")
let titleNoCode = regexpTransformAll("<code>(.+?)</code>", \groups -> groups[1], title)
let htmlContent =
if hasChanged({source=markdownPath, target=timestampPath}) then {
!touch timestampPath
let htmlContent = replaceTooltips(tooltips, pandoc(evaluatedMarkdown))
writeFile(".build/${name}_body.html", htmlContent)
htmlContent
} else {
print("No changes to post: ${name}")
# If the file has not changed, we can get its content from the .build directory
!cat (".build/${name}_body.html")
}
defineFloraVariablesWith
( floraEnv
, [ ("body", File(htmlContent))
, ("titleNoCode", String(titleNoCode))
, ("link", String("https://prophetlabs.de/posts/${name}.html"))
])
let postContent = evalTemplate(!cat "templates/post.html", floraEnv)
defineFloraVariablesWith(floraEnv, [("content", File(escapeHTML(htmlContent)))])
writeFile(htmlPath, postContent)
floraEnv
}
let buildIndex(environments) = {
let summaryTemplate = !cat "templates/post-summary.html";
let summaries = List.foldr(\x r -> x ~ r, "", List.map(\env -> evalTemplate(summaryTemplate, env), environments))
let env = newFloraEnv()
defineFloraVariables(env, [("postSummaries", summaries)])
buildPage("index.html", "index.html", env)
}
let buildRSS(environments, pubDate) = {
let itemTemplate = !cat "templates/rss-item.xml";
let items = List.foldr(\x r -> x ~ r, "", List.map(\env -> evalTemplate(itemTemplate, env), environments))
let env = newFloraEnv()
defineFloraVariablesWith
(env
, [ ("rssItems", File(items))
, ("pubDate", String(pubDate))
, ("lastBuildDate", String(pubDate))
])
buildPage("rss.xml", "rss.xml", env)
}
let buildScss() = {
let files = lines(!find "scss/" "-name" "*.scss")
List.for(files, \file -> {
let targetFile = "assets/${!basename "-s" ".scss" file}.css"
if hasChanged({source = file, target = targetFile}) then {
print("Building scss file: " ~ file);
!sassc file targetFile
()
} else {
print("Skipping unchanged scss file: " ~ file)
}
})
}
let buildExtra(path) = {
let renderedPath = "${!dirname path}/" ~ "${!basename "-s" ".md" path}.html"
if hasChanged({source=path, target=renderedPath}) then {
let env = newFloraEnv()
defineFloraVariables(env, [("link", "https://prophetlabs.de/${renderedPath}")])
let evaluatedMarkdown = regexpTransformAll
( "\\{\\{((?:a|[^a])*?)\\}\\}"
, \[_, x] ->
runFlora(["--read-env", env!, "--write-env", env!], x)
, !cat path)
let renderedMarkdown = pandoc(evaluatedMarkdown)
defineFloraVariablesWith(env, [("body", File(renderedMarkdown))])
let rendered = evalTemplate(!cat "templates/extra.html", env)
writeFile(renderedPath , rendered)
}
else {}
}
let _ = async buildScss()
let _ = Async.all([async buildExtra(file) | let file <- lines(!find "extras" "-type" "f" "-name" "*.md")])
let posts = Async.all([
async buildPost("unsafeCoerceDict"),
async buildPost("coherentIP"),
async buildPost("insttypes"),
async buildPost("classTries")
])
let _ = async buildPage("about.html", "about.html", newFloraEnv())
let environments = List.reverse(await posts)
let _ = async buildIndex(environments)
let _ = async buildRSS(environments, "19-02-2024")