-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwssg.py
367 lines (343 loc) · 12.4 KB
/
wssg.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
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
"""
Generates Static Website
Markdown parsing operates under a few rules:
1. All markup must begin and end on the same line
2. Image references use the img folder as their root
3. Hyperlinks to other parts of the website are done using the live link (no intelligent deduction here)
4. No nested lists
5. Page/file title must be on the first line of the file
Folder structure operates under a few rules:
1. Website navbar are generated using folders in root of website
2. Folders 'static' and 'public' are reserved for static content (images, pdfs, etc.) and the generated html website
3. Markdown files must have the file extension .md
4. The website uses a single css theme located in the root folder, called 'style.css'
"""
import os
import re
import shutil
"""
Parse style.css file and minify
"""
def create_style():
style_html = ''
if os.path.exists('style.css'):
f_style = open('style.css','r')
# strip all newlines, tabs and excess spaces
style = re.sub(r'[\n\t]*', '', f_style.read())
style = re.sub(r'\s{2,}', '', style)
style_html = '<style>\n' + style + '\n</style>\n'
return style_html
"""
Traverse files and directories in website root folder.
Builds the list of navbar elements and begins recursively
visiting subfolders.
root: absolute path to website root
style_html: minified CSS string to be inserted in <head></head>
"""
def traverse_dirs(root, style_html):
files = sorted(os.listdir())
# empty first element is for main page of website
nav = ['']
# first pass to build list of nav elements
for f in files:
if os.path.isdir(f):
if f in {'static', 'js', 'libs'}:
shutil.copytree(f, 'public/' + f)
elif f != 'public' and f[0] != '.':
nav.append(f)
# recursive function to pass over all files
# ignore public folder and static folder, and dot files (.git)
nav_html = create_nav(nav, '')
for f in files:
if os.path.isdir(f) and f not in {'static', 'js', 'libs', 'public'} and f[0] != '.':
os.mkdir('public/' + f)
traverse_dirs_recursive(nav, f, '../', style_html)
elif f[-3:] == '.md':
f_html = f[0:-3] + '.html'
md_to_html(f, 'public/' + f_html, nav_html, style_html)
"""
Recursively traverse files and folders in current folder
nav: names of nav elements
current_path: indicates path relative to root of website
backtrack: sequence of ../, ../../, etc. to get back to root
style_html: css content (until I find a better way of handling this)
"""
def traverse_dirs_recursive(nav, current_path, backtrack, style_html):
files = os.listdir(current_path)
nav_html = create_nav(nav, backtrack)
for f in files:
# (file or dir)
if os.path.isdir(current_path + '/' + f):
os.mkdir('public/' + current_path + '/' + f)
traverse_dirs_recursive(nav,
current_path + '/' + f,
backtrack + '../',
style_html)
elif f[-3:] == '.md':
f_html = f[0:-3] + '.html'
md_to_html(current_path + '/' + f,
'public/' + current_path + '/' + f_html,
nav_html,
style_html)
"""
Create string containing navbar HTML code
nav: list of navigation options
backtrack: sequence of ../, ../../, etc. to get back to root
"""
def create_nav(nav, backtrack):
nav_html = '<header>\n<nav>\n'
# handle first header manually (to main page of website)
nav_html += '<a href=\"' + backtrack + 'index.html\">Home</a>\n'
# skip first element, handle it manually
for i in nav[1:]:
nav_html += '<a href=\"' \
+ backtrack \
+ i \
+ '/index.html' \
+ '\">' \
+ i \
+ '</a>\n'
nav_html += '</nav>\n</header>\n'
return nav_html
"""
Convert the given markdown file into an HTML file
file_md_path: path to target markdown file
file_html_path: path to location of created HTML file
style_html: string containing minified CSS
"""
def md_to_html(file_md_path, file_html_path, nav_html, style_html):
f_md = open(file_md_path, 'r')
f_html = open(file_html_path, 'w')
# insert necessary HTML preamble
f_html.write('<!DOCTYPE html>\n<html>\n<head>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n<meta name="theme-color" content="#c2f4d1">\n<link rel="icon" href="https://joshuaoreilly.com/static/favicon.png">\n')
# sloppy way of getting title
title = f_md.readline().strip('#').strip()
f_html.write('<title>' + title + '</title>\n')
f_md.close()
f_md = open(file_md_path, 'r')
f_html.write(style_html)
f_html.write('</head>\n<body>\n')
# insert header html
f_html.write(nav_html)
f_html.write('<main>\n')
# open returns iterable object, allowing for easy line-by-line
line = f_md.readline()
while line:
if line != '\n':
line = handle_block(line, f_md, f_html)
else:
line = f_md.readline()
# close open HTML tags
f_html.write('</main>\n</body>\n</html>\n')
f_md.close()
f_html.close()
"""
Handle single-depth (no nesting) block
"""
def handle_block(line, f_md, f_html):
# what each line in the block must start with in order to continue the block
markdown_prefix = ''
# what each line will be prefixed/suffixed with in the output file
html_prefix = ''
html_suffix = ''
# what will be placed at the end of the block
block_suffix = ''
# find out what kind of block we're in
if line[0:2] == '> ':
f_html.write('<blockquote>\n')
markdown_prefix = '> '
html_prefix = ''
html_suffix = ''
block_suffix = '</blockquote>\n'
elif line[0:2] == '- ':
f_html.write('<ul>\n')
markdown_prefix = '- '
html_prefix = '<li>'
html_suffix = '</li>'
block_suffix = '</ul>\n'
elif re.match(r"^\d+\. ", line) != None:
f_html.write('<ol>\n')
markdown_prefix = 'ol'
html_prefix = '<li>'
html_suffix = '</li>'
block_suffix = '</ol>\n'
elif re.match(r"#+ ", line) != None:
regex_match = re.match(r"#+ ", line)
markdown_prefix = 'header'
# -1 since the space after the # is included in len()
html_prefix = f'<h{len(regex_match[0])-1}>'
html_suffix = f'</h{len(regex_match[0])-1}>'
block_suffix = ''
elif line[0:3] == '```':
markdown_prefix = '```'
f_html.write('<pre><code>')
block_suffix = '</code></pre>\n'
else:
# basic text paragraph
f_html.write('<p>\n')
block_suffix = '</p>\n'
pass
code_block = False
end_of_block_found = False
while not end_of_block_found:
# check if current block has ended
if markdown_prefix == 'ol':
if re.match(r"^\d+\. ", line) == None:
end_of_block_found = True
else:
line = line.removeprefix(re.match(r"^\d+\. ", line)[0])
elif markdown_prefix == 'header':
if re.match(r"#+ ", line) == None:
end_of_block_found = True
else:
line = line.removeprefix(re.match(r"#+ ", line)[0])
elif markdown_prefix == '> ':
if line[0:2] != '> ':
end_of_block_found = True
else:
line = line[2:]
elif markdown_prefix == '- ':
if line[0:2] != '- ':
end_of_block_found = True
else:
line = line[2:]
elif markdown_prefix == '```':
# special case: multiline code have line with just ```
if line[0:3] == '```':
line = '\n'
if code_block:
end_of_block_found = True
else:
code_block = True
elif not line or line == '\n':
end_of_block_found = True
if end_of_block_found:
# since md_to_html reads a new line after handling a block, go back one line
f_html.write(block_suffix)
return line
else:
if markdown_prefix == '```':
f_html.write(line)
else:
f_html.write(html_prefix + handle_line(line).rstrip() + html_suffix + '\n')
line = f_md.readline()
def handle_line(line):
html = ''
line_stack = ['']
index = 0
while index < len(line):
char = line[index]
if char == '`':
if line_stack[-1] == '</code>':
html += line_stack.pop()
else:
html += '<code>'
line_stack.append('</code>')
elif char == '*':
if index+2 < len(line) and line[index+1] == '*' and line[index+2] == '*':
index += 2
if line_stack[-1] == '</i></b>':
html += line_stack.pop()
else:
html += '<b><i>'
line_stack.append('</i></b>')
elif index+1 < len(line) and line[index+1] == '*':
index += 1
if line_stack[-1] == '</b>':
html += line_stack.pop()
else:
html += '<b>'
line_stack.append('</b>')
else:
if line_stack[-1] == '</i>':
html += line_stack.pop()
else:
html += '<i>'
line_stack.append('</i>')
elif char == '\\':
if index+1 < len(line):
index += 1
next_char = line[index]
html += next_char
elif char == '!':
if index+1 < len(line):
if line[index+1] == '[':
alt_text, link, new_index = handle_link(index+2, line)
index = new_index
html += f'<img src="{link}" alt="{alt_text}">'
else:
html += char
else:
html += '!'
elif char == '[':
if index+1 < len(line):
alt_text, link, new_index = handle_link(index+1, line)
index = new_index
html += f'<a href="{link}">{alt_text}</a>'
else:
html += char
elif char == '~':
if line[index+1] == '~':
index += 1
if line_stack[-1] == '</s>':
html += line_stack.pop()
else:
html += '<s>'
line_stack.append('</s>')
else:
html += char
index += 1
while len(line_stack) != 0:
html += line_stack.pop()
return html
"""
Handle both images and URLs, since both contain an alt text and a link
"""
def handle_link(index, line):
end_found = False
alt_text_done = False
alt_text = ''
link = ''
while not end_found:
char = line[index]
if char == ']' and (index+1) < len(line):
next_char = line[index+1]
if next_char == '(':
alt_text_done = True
index += 1
else:
alt_text += ']'
elif char == ')' and alt_text_done:
end_found = True
else:
if not alt_text_done:
alt_text += char
else:
link += char
index += 1
if index >= len(line):
end_found = True
# minus one since handle_line automatically increments index at the end of each loop iteration
return alt_text, link, index-1
"""
Remove existing public folder, create new one.
Begin file and directory traversal
"""
def prepare_dir():
# check if program called in website folder containing .wssg file
if os.path.exists('.wssg'):
# generate(site_path)
if os.path.exists('public'):
for root, dirs, files in os.walk('public', topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir('public')
os.mkdir('public')
style_html = create_style()
traverse_dirs(os.getcwd(), style_html)
else:
print('No .wssg file found, exiting')
if __name__ == "__main__":
prepare_dir()