-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
333 lines (303 loc) · 13.5 KB
/
build.py
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
from html import escape
from datetime import datetime
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from pathlib import Path
from pygments import highlight
from pygments.lexers import RustLexer
from pygments.formatters import HtmlFormatter
# To manually change
RECOMMENDED_BLOG_POST_URL = ""
def has_arg_flag(flag):
for i in range(len(sys.argv)):
if (sys.argv[i] == flag):
return True
return False
POST_NAME = sys.argv[1]
HAS_CODE_BLOCK = has_arg_flag("--code-block")
HAS_KATEX = has_arg_flag("--katex")
# All our blog posts end like this. The date is the building date
def the_hackerbirds_love_you():
return "<p><i>the hackerbirds love you - . . - "+datetime.today().strftime('%Y-%m-%d')+"</i></p>"
# We still keep header.html in another file so that our IDE can prettify it
footer_html = """<br><br></main>
<footer>
"""+the_hackerbirds_love_you()+"""
<a href="#">scroll up</a>   <a href="../">homepage</a>   <a href="""+RECOMMENDED_BLOG_POST_URL+""">check out our previous blog post</a>
</footer>
</body>
</html>"""
def parse_inline(escaped_line):
# This is for inline code blocks
if '`' in escaped_line:
split_code_line = escaped_line.split("`")
if len(split_code_line) % 2 == 0:
# Means there's an odd amount of '`` which shouldn't happen
raise Exception("Code tag weirdly formatted at line \""+escaped_line+"\". Are you sure there is the right amount of '`' in your line?")
open_tag = True
# For loop for all the `
for thing in split_code_line:
# Actually the loop goes one extra round so
# we need this if condition for when there are no more `
if '`' not in escaped_line:
break
code_tick_index = escaped_line.index('`')
if open_tag is True:
escaped_line = escaped_line[:code_tick_index] + "<code>" + escaped_line[code_tick_index+1:]
open_tag = False
else:
escaped_line = escaped_line[:code_tick_index] + "</code>" + escaped_line[code_tick_index+1:]
open_tag = True
# Inline italic text
if '__' in escaped_line:
split_code_line = escaped_line.split("__")
if len(split_code_line) % 2 == 0:
raise Exception("Italics formatted at line \""+escaped_line+"\". Are you sure there is the right amount of '__' in your line?")
open_tag = True
# For loop for all the `
for thing in split_code_line:
# Actually the loop goes one extra round so
# we need this if condition for when there are no more `
if '__' not in escaped_line:
break
code_tick_index = escaped_line.index('__')
if open_tag is True:
escaped_line = escaped_line[:code_tick_index] + "<i>" + escaped_line[code_tick_index+2:]
open_tag = False
else:
escaped_line = escaped_line[:code_tick_index] + "</i>" + escaped_line[code_tick_index+2:]
open_tag = True
# Inline bold text
if ('*' in escaped_line):
split_code_line = escaped_line.split("*")
if len(split_code_line) % 2 == 0:
raise Exception("Bold formatted at line \""+escaped_line+"\". Are you sure there is the right amount of '*' in your line?")
open_tag = True
# For loop for all the `
for thing in split_code_line:
# Actually the loop goes one extra round so
# we need this if condition for when there are no more `
if '*' not in escaped_line:
break
code_tick_index = escaped_line.index('*')
if open_tag is True:
escaped_line = escaped_line[:code_tick_index] + "<b>" + escaped_line[code_tick_index+1:]
open_tag = False
else:
escaped_line = escaped_line[:code_tick_index] + "</b>" + escaped_line[code_tick_index+1:]
open_tag = True
# Inline strikethrough text
if ('~~' in escaped_line):
split_code_line = escaped_line.split("~~")
if len(split_code_line) % 2 == 0:
raise Exception("Strikethrough formatted at line \""+line+"\". Are you sure there is the right amount of '~~' in your line?")
open_tag = True
# For loop for all the `
for thing in split_code_line:
# Actually the loop goes one extra round so
# we need this if condition for when there are no more `
if "~~" not in escaped_line:
break
code_tick_index = escaped_line.index("~~")
if open_tag is True:
escaped_line = escaped_line[:code_tick_index] + "<s>" + escaped_line[code_tick_index+2:]
open_tag = False
else:
escaped_line = escaped_line[:code_tick_index] + "</s>" + escaped_line[code_tick_index+2:]
open_tag = True
return escaped_line
# Parses each line into html.
def parse(line):
html = ""
escaped_line = escape(line)
if escaped_line == "\n":
return "<br>"
else:
# Headers: id is used to scroll
if escaped_line.startswith("# "):
text = escaped_line.rstrip()[2:]
html = "<h1>"+parse_inline(text)+"</h1>"
elif escaped_line.startswith("## "):
text = escaped_line.rstrip()[3:]
html = "<h2>"+parse_inline(text)+"</h2>"
elif escaped_line.startswith("### "):
text = escaped_line.rstrip()[4:]
html = "<h3>"+parse_inline(text)+"</h3>"
elif escaped_line.startswith("% "):
text = escaped_line.rstrip()[2:]
html = "<div class=\"f-v2\"><div class=\"bird neutral\"></div><p>"+parse_inline(text)+"</p></div>"
elif escaped_line.startswith("-> ") or escaped_line.startswith("-> "):
if " | " in escaped_line:
split_line = escaped_line.rstrip().split(" | ")
url = ' '.join(split_line[0].split(' ')[1:])
text = ""
img_quote_html = ""
if len(split_line) > 1: # There is text after the URL
text = " alt=\""+split_line[1]+"\""
if len(split_line) > 2: # There is a quote after the text
img_quote_html = "<i>"+split_line[2]+"</i>"
html = "<div class=\"attachment-div\"><img onclick=\"window.open(\'"+url+"\', \'_blank\');\" class=\"attachment\" src=\""+url+"\""+text+"/>"+parse_inline(img_quote_html)+"</div>"
else:
split_line = escaped_line.rstrip().split(" ")
url = split_line[1]
text = ""
if len(split_line) > 2: # There is text after the URL
text = ' '.join(split_line[2:])
html = "<img class=\"attachment\" src=\""+url+"\" alt=\""+parse_inline(text)+"\"/>"
# Link/URL (" => ")
elif escaped_line.startswith("=> ") or escaped_line.startswith("=> "):
split_line = escaped_line.rstrip().split(" ")
url = split_line[1]
text = url
if len(split_line) > 2: # There is text after the URL
text = ' '.join(split_line[2:])
html = "<a class=\"arrow\" href=\""+url+"\">"+parse_inline(text)+"</a><br>"
# List element
elif escaped_line.startswith("* "):
html = "<li>"+parse_inline(line[2:]).rstrip()+"</li>"
elif escaped_line.startswith("*) "):
html = "<li>"+parse_inline(line[3:]).rstrip()+"</li>"
# Blockquote (" > ")
elif (escaped_line.startswith("> ") and not escaped_line.startswith("=> ")) or (escaped_line.startswith("> ") and not escaped_line.startswith("=> ")):
html = "<blockquote><p>"+parse_inline(escaped_line[4:]).rstrip()+"</p></blockquote>"
# Bar line
elif escaped_line == "---\n":
html = "<hr>"
else:
html = "<p>"+parse_inline(escaped_line).rstrip()+"</p>"
html = html.replace("\star", "*")
html = html.replace("\\tick", "`")
return html
def write_index_html(post, output_path):
# Erases contents of index.html
open(output_path, "w").close()
with open(output_path, "a") as index_html:
# Write header.html to index.html
with open("header.html", "r") as header_html:
if HAS_KATEX:
header_html = header_html.read()
i = header_html.find('</script>\n') + 10
index_html.write(header_html[:i] + """
<link rel="stylesheet" href="../assets/katex/katex.min.css">
<script defer src="../assets/katex/katex.min.js"></script>
<script defer src="../assets/katex/contrib/auto-render.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
],
throwOnError: false
});
});
</script>
""" + header_html[i:])
else:
index_html.write(header_html.read())
# Write post contents
index_html.write(post)
# Write footer
index_html.write(footer_html)
def build(post_name):
global HAS_CODE_BLOCK
with open("posts/"+post_name+".md", "r") as f:
html = ""
is_in_rust_code_block = False
is_in_code_block = False
is_in_html_block = False
is_in_list = False
is_in_ordered_list = False
code_block = ""
if HAS_CODE_BLOCK or is_in_rust_code_block:
html += "<style>"+HtmlFormatter(style="lightbulb").get_style_defs('.highlight')+"</style>"
for line in f:
# Beginning OR end of a code block
if line == "```\n":
if is_in_code_block is True:
is_in_code_block = False
# Close code bock
html += "</pre>"
elif is_in_rust_code_block is True:
# Write code block
html += highlight(code_block, RustLexer(), HtmlFormatter())
code_block = ""
is_in_rust_code_block = False
else:
is_in_code_block = True
# Open code block
html += "<pre aria-label=\"\">"
elif HAS_CODE_BLOCK and line == "```rust\n":
if is_in_rust_code_block is not True:
is_in_rust_code_block = True
# Beginning OR end of an HTML block
elif line.startswith("<>"):
if is_in_html_block is True:
is_in_html_block = False
else:
is_in_html_block = True
# List element
elif line.startswith("* "):
# If not in a list already, put us in list mode
if is_in_list is False:
html += "<ul>"
is_in_list = True
html += parse(line)
elif line.startswith("*) "):
# If not in a list already, put us in list mode
if is_in_ordered_list is False:
html += "<ol>"
is_in_ordered_list = True
html += parse(line)
else:
# Line isn't ``` (code block) or starts with "*" (list)
if is_in_html_block is True:
html += line
elif is_in_code_block is True:
html += escape(line)
elif is_in_rust_code_block is True:
code_block += line
elif is_in_list is True:
html += "</ul>"
html += parse(line)
# We exit list mode
is_in_list = False
elif is_in_ordered_list is True:
html += "</ol>"
html += parse(line)
# We exit ordered list mode
is_in_ordered_list = False
else:
html += parse(line)
return html
def compile():
ugly_html = build(POST_NAME)
write_index_html(ugly_html, "results/"+POST_NAME+"/index.html")
class FileHandler(FileSystemEventHandler):
def on_modified(self, event):
print("Modification detected. Rebuilding post.md...")
compile()
print("Done!")
if __name__ == "__main__":
print("Compiling post \""+POST_NAME+"\"")
print("Compiling with Katex:", HAS_KATEX)
print("Compiling with formatted code blocks:", HAS_CODE_BLOCK)
# Create folders if they don't exist already
Path("posts/").mkdir(parents=True, exist_ok=True)
Path("results/"+POST_NAME).mkdir(parents=True, exist_ok=True)
compile()
print("Done! Now observing changes...")
event_handler = FileHandler()
observer = Observer()
observer.schedule(event_handler, path='posts/'+POST_NAME+'.md', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()