forked from lxc/linuxcontainers.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate
More file actions
executable file
·344 lines (271 loc) · 12.2 KB
/
generate
File metadata and controls
executable file
·344 lines (271 loc) · 12.2 KB
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
#!/usr/bin/python3
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Variables
CONTENT_PATH = "content"
TARGET_PATH = "output"
# Required imports
import bs4
import jinja2
import json
import markdown
import markdown.extensions.codehilite
import pygments.formatters
import os
import shutil
import subprocess
def pretty_print(html):
"""
Returns a cleanly indented version of the provided html code.
"""
soup = bs4.BeautifulSoup(html)
return soup.prettify(formatter="html")
def content_path(filename):
"""
Returns the path to a file from the content directory
"""
return os.path.join(CONTENT_PATH, filename)
def load_json(filename):
"""
Load the provided JSON file from the content directory.
"""
with open(content_path(filename), "r") as fd:
return json.load(fd)
def gen_menu(structure, override, prefix):
"""
Generate a list representing the menu.
If there are sub-menus, the content of the entry will be a
nested list.
The function takes the structure list and an optional override
dict used to handle translations.
When all internal links must be prefixed, prefix should be set.
"""
menu = []
sub_menu = None
sub_menu_title = None
for entry in structure:
item = dict(entry)
# Apply the override (if any)
item.update(override.get(entry['path'], {}))
if prefix:
# Prepend the prefix to all internal links
item['path'] = "%s%s" % (prefix, item['path'])
if "generator" not in item:
# Menu splitter
item['path'] = ""
elif item['generator'] == "link":
# External link
item['path'] = item['meta']['url']
if not "menu" in item:
continue
if len(item['menu']) == 1:
if "generator" in item and item['generator'] == "manpages":
# Dynamic manpage menu
man_menu = []
man_path = item['meta']['dir'].lstrip("/")
for entry in sorted(os.listdir(man_path)):
if not os.path.isfile("%s/%s" % (man_path, entry)):
continue
entry_link = "/%s/%s" % (item['path'].lstrip("/"), entry)
man_menu.append((entry_link, entry))
menu.append((man_menu, item['menu'][0]))
else:
# Simple menu entry
menu.append((item['path'], item['menu'][0]))
else:
# Drop-down menu
if sub_menu_title != item['menu'][0]:
if sub_menu:
menu.append((sub_menu, sub_menu_title))
sub_menu = []
sub_menu_title = item['menu'][0]
if "generator" in item and item['generator'] == "manpages":
# Dynamic manpage menu
man_menu = []
man_path = item['meta']['dir'].lstrip("/")
for entry in sorted(os.listdir(man_path)):
if not os.path.isfile("%s/%s" % (man_path, entry)):
continue
entry_link = "/%s/man%s/%s.html" % (
item['path'].lstrip("/"), entry.split(".")[-1], entry)
man_menu.append((entry_link, entry))
sub_menu.append((man_menu, item['menu'][-1]))
else:
# Simple menu entry
sub_menu.append((item['path'], item['menu'][-1]))
# Process the last drop-down menu
if structure and sub_menu_title:
if sub_menu:
menu.append((sub_menu, sub_menu_title))
return menu
def gen_pages(structure, override, prefix, **variables):
for entry in structure:
item = dict(entry)
# Apply the override (if any)
item.update(override.get(entry['path'], {}))
# Store the original path (required for translations)
page_raw_path = item['path']
if prefix:
# Prepend the prefix to all internal links
item['path'] = "%s%s" % (prefix, item['path'])
if "generator" not in item or item['generator'] == "link":
# Skip the virtual entries (external links, splitters)
continue
# Generate the target path and create any missing directory
output_path = "%s%s/index.html" % (TARGET_PATH, item['path'])
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path))
# Generate the page content
content = ""
template = "page.tpl.html"
hilite_config = [('linenums', None),
('guess_lang', False),
('noclasses', False)]
codehilite = markdown.extensions.codehilite.makeExtension(
hilite_config)
if item['generator'] == "html":
with open(content_path(item['meta']['input']), "r") as fd:
content = fd.read()
elif item['generator'] == "markdown":
with open(content_path(item['meta']['input']), "r") as fd:
content = markdown.markdown(fd.read(), extensions=[codehilite])
elif item['generator'] == "downloads":
# Support a markdown description before the download table
if "input" in item['meta']:
with open(content_path(item['meta']['input']), "r") as fd:
content = markdown.markdown(fd.read(),
extensions=[codehilite])
downloads = []
for entry in sorted(os.listdir(item['meta']['dir'].lstrip("/")),
reverse=True):
if entry.endswith(".asc"):
continue
fs_path = "%s/%s" % (item['meta']['dir'].lstrip("/"), entry)
download = {}
download['filename'] = entry
download['path'] = "%s/%s" % (item['meta']['dir'], entry)
download['signame'] = None
download['sigpath'] = None
if os.path.exists("%s.asc" % fs_path):
download['signame'] = "%s.asc" % download['filename']
download['sigpath'] = "%s.asc" % download['path']
download['size'] = "%sK" % \
(round(os.stat(fs_path).st_size / 1024, 2))
downloads.append(download)
variables['downloads'] = downloads
template = "downloads.tpl.html"
elif item['generator'] == "manpages":
man_path = item['meta']['dir'].lstrip("/")
for entry in sorted(os.listdir(man_path)):
if not os.path.isfile("%s/%s" % (man_path, entry)):
continue
section = entry.split(".")[-1]
output_man_path = "%s%s/man%s/%s.html" % (TARGET_PATH,
item['path'],
section, entry)
if not os.path.exists(os.path.dirname(output_man_path)):
os.makedirs(os.path.dirname(output_man_path))
man2html = subprocess.Popen(["man2html", "-r",
"%s/%s" % (man_path, entry)],
stdout=subprocess.PIPE,
universal_newlines=True)
man2html.wait()
soup = bs4.BeautifulSoup(man2html.stdout.read())
# Remove all broken links
for link in soup.findAll("a"):
if not link.get("href", None):
continue
if link['href'] in ("../index.html",):
continue
if link['href'].startswith("mailto"):
continue
if link['href'].startswith("#"):
continue
filename = link['href'].split("/")[-1].split(".html")[0]
if filename in os.listdir(man_path):
continue
link.replace_with_children()
contents = soup.findAll("body")[0].contents
content = "".join([str(entry)
for entry in contents[1:]])
content = "<div class='manpage'>%s</div>" % content
entry_link = "/%s/man%s/%s.html" % (
item['path'].lstrip("/"), section, entry)
template = env.get_template(template)
with open(output_man_path, "w+") as fd:
fd.write(pretty_print(
template.render(page_path=entry_link,
page_raw_path="%s/man%s/%s.html" % (
page_raw_path, section, entry),
page_title="%s - %s" % (item['title'],
entry),
page_menu=item['menu'] + [entry],
content=content,
**variables)))
content = "<ul>"
for entry in sorted(os.listdir(man_path)):
if not os.path.isfile("%s/%s" % (man_path, entry)):
continue
section = entry.split(".")[-1]
entry_link = "/%s/man%s/%s.html" % (
item['path'].lstrip("/"), section, entry)
content += "<li><a href=\"%s\">%s</a></li>" % (entry_link,
entry)
content += "</ul>"
elif item['generator'] == "directory":
os.rmdir("%s/%s" % (TARGET_PATH, item['path']))
shutil.copytree(content_path(item['meta']['input']),
"%s/%s" % (TARGET_PATH, item['path']))
continue
# Pass all the variables to the template and generate the html
template = env.get_template(template)
with open(output_path, "w+") as fd:
fd.write(pretty_print(
template.render(page_path=item['path'],
page_raw_path=page_raw_path,
page_title=item['title'],
page_menu=item['menu'],
content=content,
**variables)))
# Load the configuration and structure
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates/'))
config = load_json("CONFIG.json")
structure = load_json("STRUCTURE.json")
# Wipe the output directory clean
if os.path.exists(TARGET_PATH):
shutil.rmtree(TARGET_PATH)
os.mkdir(TARGET_PATH)
shutil.copytree("static", "%s/static" % TARGET_PATH)
shutil.copytree("downloads", "%s/downloads" % TARGET_PATH)
os.symlink("static/img/favicon.ico", "%s/favicon.ico" % TARGET_PATH)
with open("%s/static/css/pygments.css" % TARGET_PATH, "w+") as fd:
fd.write(pygments.formatters.HtmlFormatter().get_style_defs())
# Start generating the website
for language in config['languages']:
# Load a translation override
override = {}
try:
override = {entry['path']: entry
for entry in load_json("STRUCTURE.%s.json" % language[0])}
except FileNotFoundError:
pass
# Figure out the translation prefix
lang_prefix = ""
if language[0]:
lang_prefix = "/%s" % language[0]
# Generate the menu
menu = gen_menu(structure, override, lang_prefix)
# Generate all the pages
gen_pages(structure, override, lang_prefix,
menu=menu, page_language=language,
languages=config['languages'])