-
Notifications
You must be signed in to change notification settings - Fork 3
/
makesite.py
284 lines (248 loc) · 9.31 KB
/
makesite.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
"""
Script to build a website from a bunch of markdown files.
Inspired by https://github.com/sunainapai/makesite
Tweaked for pyzo.org
"""
import os
import shutil
import webbrowser
import markdown
import pygments
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name
from datetime import datetime
TITLE = "Pyzo"
NAV = {
"Quickstart": "start",
"About Python": {
"": "about_python",
"Why Python": "whypython",
"Python 3": "python3",
"Python vs Matlab": "python_vs_matlab",
"Speed": "speed",
"For whom": "forwhom",
},
"About Pyzo": {
"": "about_pyzo",
"Mission": "mission",
"Features": "features",
"Themes": "pyzo_themes",
"Screenshots": "screenshots",
"Release notes": "https://github.com/pyzo/pyzo/wiki/Release_notes",
},
"Guide": {
"": "guide",
"Short introduction": "pyzo_intro",
"Installing additional packages": "install_packages",
"Configuring shells": "shellconfig",
"Interactive vs script mode": "interactive_vs_script",
"FAQ": "faq",
},
"Learn": "learn",
}
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
OUT_DIR = os.path.join(THIS_DIR, "output")
STATIC_DIR = os.path.join(THIS_DIR, "static")
PAGES_DIR = os.path.join(THIS_DIR, "pages")
def create_menu(page):
""" Create the menu for the given page.
"""
menu = []
# menu.append('<span class="header">Topics</span>')
for title, target in NAV.items():
if isinstance(target, str):
if target.startswith(("https://", "http://", "/")):
menu.append(f"<a href='target'>{title}</a>")
else:
menu.append(f"<a href='{target}.html'>{title}</a>")
if target == page.name:
menu[-1] = menu[-1].replace("<a ", '<a class="current" ')
menu += [
f"<a class='sub' href='#{title.lower()}'>{title}</a>"
for level, title in page.headers
if level == 2
]
elif isinstance(target, dict):
menu.append(f"<a href='{target['']}.html'>{title}</a>")
if target[""] == page.name:
menu[-1] = menu[-1].replace("<a ", '<a class="current" ')
if any(page.name == subtarget for subtarget in target.values()):
for subtitle, subtarget in target.items():
if not subtitle:
continue
if subtarget.startswith(("https://", "http://", "/")):
menu.append(f"<a class='sub' href='{subtarget}'>{subtitle}</a>")
else:
menu.append(
f"<a class='sub' href='{subtarget}.html'>{subtitle}</a>"
)
if subtarget == page.name:
menu[-1] = menu[-1].replace("class='", "class='current ")
else:
raise RuntimeError(f"Unexpected NAV entry {type(target)}")
return "<br />".join(menu)
def create_assets():
""" Returns a dict of all the assets representing the website.
"""
assets = {}
# Load all static files
for root, dirs, files in os.walk(STATIC_DIR):
for fname in files:
filename = os.path.join(root, fname)
with open(filename, "rb") as f:
assets[os.path.relpath(filename, STATIC_DIR)] = f.read()
# Collect pages
pages = {}
for fname in os.listdir(PAGES_DIR):
if fname.lower().endswith(".md"):
name = fname.split(".")[0].lower()
with open(os.path.join(PAGES_DIR, fname), "rb") as f:
md = f.read().decode()
pages[name] = Page(name, md)
# todo: Collect blog posts
# Get template
with open(os.path.join(THIS_DIR, "template.html"), "rb") as f:
html_template = f.read().decode()
with open(os.path.join(THIS_DIR, "style.css"), "rb") as f:
css = f.read().decode()
css += "/* Pygments CSS */\n" + HtmlFormatter(style="vs").get_style_defs(
".highlight"
)
# Generate pages
year = datetime.now().year
for page in pages.values():
page.prepare(pages.keys())
title = TITLE if page.name == "index" else TITLE + " - " + page.name
menu = create_menu(page)
html = html_template.format(
title=title, style=css, body=page.to_html(), menu=menu, year=year
)
print("generating", page.name + ".html")
assets[page.name + ".html"] = html.encode()
# Fix backslashes on Windows
for key in list(assets.keys()):
if "\\" in key:
assets[key.replace("\\", "/")] = assets.pop(key)
return assets
def main():
""" Main function that exports the page to the file system.
"""
# Create / clean output dir
if os.path.isdir(OUT_DIR):
shutil.rmtree(OUT_DIR)
os.mkdir(OUT_DIR)
# Write all assets to the directory
for fname, bb in create_assets().items():
filename = os.path.join(OUT_DIR, fname)
dirname = os.path.dirname(filename)
if not os.path.isdir(dirname):
os.makedirs(dirname)
with open(filename, "wb") as f:
f.write(bb)
class Page:
""" Representation of a page. It takes in markdown and produces HTML.
"""
def __init__(self, name, markdown):
self.name = name
self.md = markdown
self.parts = []
self.headers = []
def prepare(self, page_names):
# Convert markdown to HTML
self.md = self._fix_links(self.md, page_names)
self.md = self._highlight(self.md)
self._split() # populates self.parts and self.headers
def _fix_links(self, text, page_names):
""" Fix the markdown links based on the pages that we know.
"""
for n in page_names:
text = text.replace(f"]({n})", f"]({n}.html)")
text = text.replace(f"]({n}.md)", f"]({n}.html)")
return text
def _highlight(self, text):
""" Apply syntax highlighting.
"""
lines = []
code = []
for i, line in enumerate(text.splitlines()):
if line.startswith("```"):
if code:
formatter = HtmlFormatter()
try:
lexer = get_lexer_by_name(code[0])
except Exception:
lexer = get_lexer_by_name("text")
lines.append(
pygments.highlight("\n".join(code[1:]), lexer, formatter)
)
code = []
else:
code.append(line[3:].strip()) # language
elif code:
code.append(line)
else:
lines.append(line)
return "\n".join(lines).strip()
def _split(self):
""" Split the markdown into parts based on sections.
Each part is either text or a tuple representing a section.
"""
text = self.md
self.parts = parts = []
self.headers = headers = []
lines = []
# Split in parts
for line in text.splitlines():
if line.startswith(("# ", "## ", "### ", "#### ", "##### ")):
# Finish pending lines
parts.append("\n".join(lines))
lines = []
# Process header
level = len(line.split(" ")[0])
title = line.split(" ", 1)[1]
title_short = title.split("(")[0].split("<")[0].strip().replace("`", "")
headers.append((level, title_short))
parts.append((level, title_short, title))
else:
lines.append(line)
parts.append("\n".join(lines))
# Now convert all text to html
for i in range(len(parts)):
if not isinstance(parts[i], tuple):
parts[i] = markdown.markdown(parts[i], extensions=[]) + "\n\n"
def to_html(self):
htmlparts = []
for part in self.parts:
if isinstance(part, tuple):
level, title_short, title = part
title_html = (
title.replace("``", "`")
.replace("`", "<code>", 1)
.replace("`", "</code>", 1)
)
ts = title_short.lower()
if part[0] == 2 and title_short:
htmlparts.append(
"<a class='anch' name='{}' href='#{}'>".format(ts, ts)
)
htmlparts.append("<h%i>%s</h%i>" % (level, title_html, level))
htmlparts.append("</a>")
else:
htmlparts.append("<h%i>%s</h%i>" % (level, title_html, level))
else:
htmlparts.append(part)
return "\n".join(htmlparts)
def copydir(src, dst):
""" Like shutil.copytree, but directories are ok to already exist.
"""
for item in os.listdir(src):
s, d = os.path.join(src, item), os.path.join(dst, item)
if os.path.isdir(s):
if not os.path.isdir(d):
os.mkdir(d)
copydir(s, d)
else:
shutil.copy(s, d)
if __name__ == "__main__":
main()
# webbrowser.open(os.path.join(OUT_DIR, "index.html"))