forked from dylansdaniels/website_redesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
277 lines (232 loc) · 9.11 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
# %%
import os
import re
import pypandoc
import json
from scripts.create_page_index import update_page_index
from scripts.create_navbar import generate_navbar_html
from scripts.convert_notebooks import convert_notebooks_to_html
def compile_page_components():
"""Compile base html components for building webpage"""
templates_folder = os.path.join(os.getcwd(), 'templates')
templates = ['header', 'topbar', 'footer', 'script']
html_parts = {}
for template in templates:
templates_path = os.path.join(templates_folder, f'{template}.html')
with open(templates_path, 'r') as f:
html_parts[template] = f.read()
update_page_index()
navbar_html, ordered_links = generate_navbar_html()
html_parts['navbar'] = navbar_html
return html_parts, ordered_links
def get_page_paths(path=None):
"""Get paths to all .md pages to be converted to html"""
md_pages = {}
if path is None:
path = os.path.join(os.getcwd(), "content")
directories = os.listdir(path)
for item in directories:
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
# add items from new dict into md_pages
md_pages.update(get_page_paths(item_path))
else:
if not item == "README.md" and item.endswith(".md"):
md_pages[item] = item_path
return md_pages
def generate_page_html(page_paths):
"""
"""
# get the .html templates for building pages
html_parts, ordered_links = compile_page_components()
# specify the order of components for assembling pages
order = [
'header',
'navbar',
'topbar',
'body',
'footer',
'script',
]
# print(ordered_links)
for md_page, path in page_paths.items():
page_components = html_parts.copy()
# get the directory containing the markdown file
out_directory = path.split(md_page)[0]
# remove leading `##_` from page and change extension to .html
html_page = md_page.split("_", 1)[1]
html_page = html_page.split(".md")[0] + ".html"
# set the output path
out_path = out_directory + html_page
# update 'header' page_component with the relative stylesheet path
# ------------------------------------------------------------
# set the path from root directory to the stylesheet
css_path = os.path.join(
os.getcwd(),
"content",
"assets",
"styles.css"
)
# get the relative path
relative_css_path = os.path.relpath(
css_path,
start=out_directory
)
# update the 'header' page_component with the correct path
page_components['header'] = page_components['header'].replace(
'<link rel="stylesheet" href="styles.css">',
f'<link rel="stylesheet" href="{relative_css_path}">'
)
# update 'footer' page_component with the correct links
# ------------------------------------------------------------
footer_path = os.path.join(
os.getcwd(),
'templates',
'ordered_page_links.json'
)
with open(footer_path, 'r') as f:
ordered_page_links = json.load(f)
ordered_links = ordered_page_links['links']
ordered_titles = ordered_page_links['titles']
location = None
last_page = len(ordered_links)-1
for i, link in enumerate(ordered_links):
# print(f'{link} | {out_path}')
if link in out_path:
location = i
if location == 0:
prev_page = "None"
prev_title = ""
next_page = ordered_links[location+1]
next_title = ordered_titles[location+1]
elif location == last_page:
prev_page = ordered_links[location-1]
prev_title = ordered_titles[location-1]
next_page = "None"
next_title = "None"
else:
prev_page = ordered_links[location-1]
prev_title = ordered_titles[location-1]
next_page = ordered_links[location+1]
next_title = ordered_titles[location+1]
page_components['footer'] = page_components['footer'].replace(
'<div class="previous-area" data-link="None">',
f'<div class="previous-area" data-link="{prev_page}">'
)
page_components['footer'] = page_components['footer'].replace(
'<div class="next-area" data-link="None">',
f'<div class="next-area" data-link="{next_page}">'
)
page_components['footer'] = page_components['footer'].replace(
'<a>PreviousTitle</a>',
f'<a>{prev_title}</a>'
)
page_components['footer'] = page_components['footer'].replace(
'<a>NextTitle</a>',
f'<a>{next_title}</a>'
)
# convert markdown file to html with pypandoc
# ------------------------------------------------------------
converted_html = pypandoc.convert_file(
path,
format='md',
to='html',
extra_args=[
"--bibliography=textbook-bibliography.bib",
"--citeproc",
"--mathml",
"-f",
"markdown-auto_identifiers",
],
)
# optionally add Jupyter notebook ouptuts to converted html
# ------------------------------------------------------------
def get_html_from_json(
nb_name,
nb_path,
):
"""Get the structured .json output for a specified
.ipynb notebook, extract the relevent html components,
and return the aggregated html as a string.
Arguments
---------
nb_name : str
Jupyter notebook file name
E.g., 'simulate_erps.ipynb'
nb_path : str
Path to notebook
E.g.: 'website/content/erps/simulate_erps.ipynb'
Returns
-------
agg_html : str
"""
json_path = nb_path.split('.ipynb')[0] + '.json'
with open(json_path, 'r') as file:
nb_outputs = json.load(file)
nb_outputs = nb_outputs.get(nb_name, {})
agg_html = ''
for section, content in nb_outputs.items():
if isinstance(content, dict) and 'html' in content:
agg_html += content['html']
return agg_html
def add_notebook_to_html(converted_html):
"""
Function to insert Jupyter notebook html outputs into html
pages converted from markdown files
Arguments
---------
converted_html : str
Returns
-------
combined_html : str
"""
# regex pattern match for "[[notebook_name.ipynb]" with only
# a single closing bracket, as additional parameters may be
# included in the notebook specification line
nb_match_pattern = re.compile(r"\[\[(.+?\.ipynb)\]")
# notebook specifications with additional arguments will
# match the exact pattern ".ipynb][" as defined below
nb_arguments_pattern = ".ipynb]["
output_lines = []
for line in converted_html.splitlines():
match = nb_match_pattern.search(line)
args = nb_arguments_pattern in line
if match and args:
notebook_name = match.group(1)
nb_path = path.split(md_page)[0] + notebook_name
print(f'nb with args found: {line}')
print(
'Argument handling will be added in a '
'future update'
)
output_lines.append(line)
elif match:
notebook_name = match.group(1)
nb_path = path.split(md_page)[0] + notebook_name
notebook_html = get_html_from_json(notebook_name, nb_path)
output_lines.append(notebook_html)
else:
output_lines.append(line)
combined_html = "\n".join(output_lines)
return combined_html
combined_html = add_notebook_to_html(converted_html)
# Aggregate all page components and write output
# ------------------------------------------------------------
page_components['body'] = combined_html
file_contents = ""
for section in order:
file_contents += page_components[section]
file_contents += '\n</body>\n</html>'
with open(out_path, 'w') as out:
out.write(file_contents)
return
# %%
content_path = os.path.join(os.getcwd(), "content")
hash_path = os.path.join(os.getcwd(), "scripts", "notebook_hashes.json")
_ = convert_notebooks_to_html(
input_folder=content_path,
hash_path=hash_path,
write_html=True,
)
page_paths = get_page_paths()
generate_page_html(page_paths)