-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild_index
executable file
·81 lines (70 loc) · 2.17 KB
/
build_index
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
#!/usr/bin/env python
import os
import urllib
from glob import glob
MAGIC_HEADER = "<!-- generated -->"
def build_dir_index(paths):
index_items = []
for filepath in paths:
filename = os.path.basename(filepath).decode("utf-8")
href = urllib.quote(filename)
title = filename.encode("ascii", "xmlcharrefreplace")
if os.path.isdir(filepath):
title += "/"
index_items.append("""
<li><a href="{href}">{title}</a></li>
""".format(href=href, title=title))
return index_items
template = """
<html>
<head>
<title>PyCon 2013 Slides</title>
</head>
<body>
<h1>PyCon 2013 Slides</h1>
<p>
Most presentations can be found at: <a
href="https://speakerdeck.com/pyconslides/">https://speakerdeck.com/pyconslides/</a>.
</p>
<p>
This repository contains presentations which cannot be converted to
PDF, or the code from presentations which have some.
</p>
<ul>
{presentation_links}
</ul>
</body>
"""
base = os.path.join(os.path.dirname(__file__))
base_links = build_dir_index([
dirpath for dirpath in glob(base + "/*")
if os.path.isdir(dirpath)
])
with open(os.path.join(base, "index.html"), "w") as f:
f.write(template.format(
presentation_links="".join(base_links),
))
for dirpath, dirnames, filenames in os.walk(base):
if dirpath == base or "/." in dirpath:
continue
index_file = os.path.join(dirpath, "index.html")
if os.path.exists(index_file):
header = open(index_file).read(1024).strip()
if not header.startswith(MAGIC_HEADER):
continue
title = os.path.basename(dirpath).encode("ascii", "xmlcharrefreplace")
links = "".join(build_dir_index([
os.path.join(dirpath, x) for x in dirnames + filenames
]))
with open(index_file, "w") as f:
f.write("""
{MAGIC_HEADER}
<html>
<head><title>{title}</title></head>
<body>
<ul>
{links}
</ul>
</body>
</html>
""".format(MAGIC_HEADER=MAGIC_HEADER, title=title, links=links))