-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.jl
334 lines (287 loc) · 11 KB
/
sync.jl
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
import URIs
import Base64
import Base.Filesystem
struct Post
name::String
date::String
title::String
tags::Vector{String}
rss::String
end
const posts = [
# NEWEST TO OLDESTS
Post("animating list shuffle with FLIP technique", "2024/12/25", "Animating a list shuffle with the FLIP technique", [], "Explaining how to use the FLIP technique to animate a list shuffle"),
Post("more gradient descent", "2024/04/07", "More gradient descent", [], "My attempt to understand gradient descent a little better"),
Post("scp completion in zsh", "2024/03/02", "How does `scp` completion work in zsh?", [], "My attempt to understand the zsh completion system"),
Post("autograd", "2024/01/05", "A Rigorous (and Not-So-Rigorous) Look at JAX's Autograd", [], "My attempt to understand how JAX does autograd."),
Post("stoichiometry", "2023/12/08", "A Linear Algebra Perspective on High School Chemistry", [], "My attempt to understand what happened in high school with the tools of linear algebra."),
Post("professor chan's band coloring", "2023/11/16", "Professor Chan's Band Coloring", [], "My recollection of my advisor's graph theory application from freshman year."),
Post("purely functional dijkstras", "2023/11/05", "Asymptotic Analysis of Dijkstras in Haskell", [], "My analysis of whether it is possible to have optimal* Dijkstras in Haskell"),
Post("wave equation", "2023/08/01", "Wave equation simulation", ["website"], ""),
Post("hashfs", "2023/07/31", "hashfs", ["website"], ""),
Post("understanding GLFRAMEBUFFERSRGB in glium", "2023/07/31", "Colorspace in glium", [], ""),
Post("voronoi blog post", "2023/07/20", "Voronoi Diagrams", ["website"], ""),
Post("confetti", "2023/07/20", "Confetti", ["website"], ""),
Post("why a blog", "2023/07/20", "Why a Blog?", ["website"], ""),
Post("chicken coop website", "2023/07/18", "Hilarious Random Number Generator", [], ""),
Post("bayes rule", "2023/07/17", "Bayes' Rule (in real life)", ["math"], ""),
Post("picks theorem", "2021/09/03", "Pick's Theorem", ["math"], "Pick's Theorem is a formula for the area of a closed polygon with integer vertices.", ),
Post("five color theorem", "2021/07/16", "Five Color Theorem", ["math"], "The Five Color Theorem asserts that no planar graphs are 5-colorable.", ),
Post("linear regression", "2021/07/11", "Linear Regression", ["math"], ""),
]
const notes_dir = "/users/jason/notes/"
function frontmatter(post)
"""+++
title = "$(post.title)"
date = Date("$(post.date)", "yyyy/mm/dd")
rss = "$(post.rss)"
tags = $(post.tags)
descr = true
githubsource = "https://github.com/jasoneveleth/blog/blob/dev/$(post2filename(post)).md"
+++
~~~
<details class="toc">
<summary>Table of Contents</summary>
~~~
\\toc
~~~
</details>
~~~
"""
end
function resolve_post_title(title)
for post in posts
if post.name == title
return post2filename(post)
end
end
"404"
end
function remove_existing!()
# all years are 20xx, I want to make my own millenium problem
existing_folders = filter(x -> x[1:2] == "20", readdir("."))
if length(existing_folders) == 0
return
end
println("Existing folders:")
println(existing_folders)
println("Is it okay to delete these (y/n): ")
response = readline(stdin)[1]
if response != 'y'
println("exiting")
return
end
for path in existing_folders
Filesystem.rm(path, recursive=true)
end
end
function post2filename(p)
p.date * "/" * escape_uri(p.name)
end
function filepaths_and_frontmatter(posts_list)::Vector{Tuple{String, String, String}}
ret = []
for p in posts_list
src = notes_dir * p.name * ".md"
dest = post2filename(p) * ".md"
push!(ret, (src, dest, frontmatter(p)))
end
ret
end
function escape_uri(str)
URIs.escapeuri(replace(str, " " => "-", "'" => ""))
end
function process_file(file_contents)
# we can't use replace(s, regex => substituionstr ) because we
# need the capture group to be evaluated before we URI encode it
# replace() assumes that the second part of the pair is a string
# so there's no way to arrange the computation such that the URI encode
# happens after the capture
function f(m)
link_name, section, alias = m.captures
if alias !== nothing
alias = alias[2:end] # cut off '|'
end
if section !== nothing
section = section[2:end] # cut off '#'
end
s = ""
# display
if alias !== nothing
s *= "[$(alias)]"
elseif section !== nothing
s *= "[$(section)]"
else
s *= "[$(link_name)]"
end
# href
if section !== nothing
s *= "(/$(resolve_post_title(link_name))/#$(replace(lowercase(section), ' ' => '_')))"
else
s *= "(/$(resolve_post_title(link_name)))"
end
s
end
function g(m)
note_name = m.captures[1]
included_text = read(notes_dir * note_name * ".md", String)
# TODO: this function won't work for recursive includes, we
# want to recurse, but we can't call process_file() since we
# only want to do the other substitutions once (once all of the
# file has been assembled)
@assert match(r"\!\[\[([a-zA-Z0-9 .'=!-]+)\]\]", included_text) === nothing
included_text
end
function dotsrc2imgsrc(m)
text = m.captures[1]
inpath, io = mktemp()
write(io, text)
close(io)
outpath, io = mktemp()
close(io)
run(pipeline(inpath, `dot -Tpng -Gdpi=300`, `base64`, outpath))
"![](data:image/png;base64,$(read(outpath, String)))"
end
function tikzsrc2imgsrc(m)
text = m.captures[1]
inpath, io = mktemp()
write(io, "\\documentclass[tikz,border=2mm]{standalone}\n")
write(io, text)
write(io, "\n")
close(io)
outpath, io = mktemp()
close(io)
run(pipeline(inpath, `pwrap pdflatex -interaction batchmode -halt-on-error file.tex tex pdf`, `convert -density 300 pdf:- png:-`, `pwrap mogrify -strip file.png png png`, `base64`, outpath))
"![](data:image/png;base64,$(read(outpath, String)))"
end
# change `mathbfit` to `bm`
file_contents = replace(file_contents, r"\\mathbfit" => "\\bm")
file_contents = replace(file_contents, r"\\argmax" => "\\operatorname*{argmax}\\,")
# note inclusion: ![[note]]
rx = r"\!\[\[([a-zA-Z0-9 .'=!-]+)\]\]"
file_contents = replace(file_contents, rx => s -> g(match(rx, s)))
# wikilinks: [[link name#section|alias]] => [alias](/2021/07/11/link-name/#section)
# [[link name|alias]] => [alias](/2021/07/11/link-name)
# [[link name#section]] => [section](/2021/07/11/link-name/#section)
# [[link name]] => [link name](/2021/07/11/link-name/)
rx = r"\[\[([a-zA-Z0-9 .'=!-]+)(#[a-zA-Z0-9 ()]+)?(\|[a-zA-Z0-9 ]+)?\]\]"
file_contents = replace(file_contents, rx => s -> f(match(rx, s)))
# convert images
# ![stuff](images/something) => ![stuff](/assets/something)
rx = r"!\[([a-zA-Z0-9 .'-=!|]*)\]\(images/([a-zA-Z0-9 .'_=!-]+)\)"
file_contents = replace(file_contents, rx => s"![\1](/assets/\2)")
# videos
# ![stuff](videos/something) => ~~~\n<videos>stuff](/assets/something)</video>\n~~~
rx = r"!\[([a-zA-Z0-9 .'-=!|]*)\]\(videos/([a-zA-Z0-9 .'_=!-]+)\)"
file_contents = replace(file_contents, rx => s"~~~\n<video controls src=\"/assets/\2\" alt=\"\1\"></video>\n~~~")
# turn `zsh` into `bash`
rx = r"```zsh\n"
file_contents = replace(file_contents, rx => "```bash\n")
# remove `dot` code block
rx = r"```dot\n([^`]*)\n```"
file_contents = replace(file_contents, rx => s -> dotsrc2imgsrc(match(rx, s)))
# remove `tikz` code block
rx = r"```tikz\n([^`]*)\n```"
file_contents = replace(file_contents, rx => s -> tikzsrc2imgsrc(match(rx, s)))
end
function copy_images!(file_contents)
# images: ![](images/file-path.jpg)
rx = r"!\[[a-zA-Z0-9 .'=!-|]*\]\(/assets/([a-zA-Z0-9 .'_=!-]+)\)"
for m in eachmatch(rx, file_contents)
x = m.captures[1]
if !isfile("_assets/$(x)")
cp(notes_dir * "images/$(x)", "_assets/$(x)")
end
end
end
function make_space!(file_path)
directory_path = dirname(file_path)
mkpath(directory_path)
end
function make_contents(note_contents, frontmatter)
"""
$(frontmatter)
$(process_file(note_contents))
{{ addcomments }}
"""
end
function copy_files!(data::Vector{Tuple{String, String, String}})
for (src, dest, frontmatter) in data
newcontent = make_contents(read(src, String), frontmatter)
make_space!(dest)
overwrite_if_diff!(dest, newcontent)
copy_images!(newcontent)
end
end
function overwrite_if_diff!(file_path, content)
if isfile(file_path) && read(file_path, String) == content
println("skipped: ", file_path)
else
write(file_path, content)
println("written ", file_path)
end
end
function get_posts(posts, year)
for p in posts
if p.date[1:4] == year
end
end
end
function new_index!()
index = """
@def title = "Archive"
"""
f(l, x) = push!(l, x.date[1:4])
years = unique(foldl(f, posts, init=[]))
for y in years
index *= "## $(y)\n"
for p in filter(x-> x.date[1:4] == y, posts)
index *= "~~~<span class='date'>$(p.date[6:end])</span>~~~ [$(p.title)]($(p.date)/$(escape_uri(p.name)))\n\n"
end
end
overwrite_if_diff!("index.md", index)
end
function new_search!()
search = """
@def title = "Search"
~~~
<script>
const mystuff = [
"""
for p in posts
contents = read(post2filename(p) * ".md", String)
contents = replace(contents, "\\" => "\\\\")
contents = replace(contents, "\"" => "\\\"")
# note we use the s flag to make . match newlines, .*? is non-greedy, which is what you want for multiple matches
contents = replace(contents, r"~~~\n.*?~~~\n"s => "")
contents = replace(contents, r"\+\+\+[^+]*\+\+\+" => "")
contents = replace(contents, "```" => "")
contents = replace(contents, "\n" => " ")
contents = replace(contents, r"!\[\]\(data:image/png;base64,[-A-Za-z0-9+/]*=* \)" => "")
# this is the beginning of every document: " <details> <summary>Table of Contents</summary> \\toc </details> "
contents = contents[70:end]
search *= "{title: \"$(p.title)\", content: \"$(contents)\"},\n"
end
search *= """
]
</script>
<input oninput="search(this.value, mystuff)"/>
<div id="results-div"></div>
~~~
"""
overwrite_if_diff!("search.md", search)
end
function main()
println("syncing...")
data = filepaths_and_frontmatter(posts)
for (src, _, _) in data
if !isfile(src)
println("not found: ", src)
return 1
end
end
# remove_existing!()
copy_files!(data)
new_index!()
new_search!()
end
main()