-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_cdoc_index.py
More file actions
71 lines (66 loc) · 1.68 KB
/
make_cdoc_index.py
File metadata and controls
71 lines (66 loc) · 1.68 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
import argparse
import glob
import os
from typing import List
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('docdir')
args = parser.parse_args()
run(**vars(args))
def run(docdir:str) -> None:
all_htmls = [] # type: List[str]
for p in sorted(os.listdir(docdir)):
if not os.path.isdir(os.path.join(docdir, p)): continue
htmls = glob.glob(os.path.join(docdir, p, '*'))
if not htmls: continue
htmls = [x for x in htmls if 'cdoc' not in x]
htmls.sort()
write_index(htmls, os.path.join(docdir, p), True)
htmls.insert(0, os.path.join(docdir, p, 'cdocindex.html'))
all_htmls.extend(htmls)
write_index(all_htmls, docdir)
HEAD='''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style>
* {
box-sizing: border-box;
}
html {
background-color: #272822;
color: #D2D39A;
height: 100%;
width: 100%;
}
a {
color: #D2D39A;
font-family: ui-monospace, "Cascadia Mono", Consolas, mono;
}
#directory {
margin: auto;
width: max-content;
}
</style>
</head>
<body>
<ul id="directory">
'''
TAIL = '''
</ul>
</body>
</html>
'''
def write_index(htmls:List[str], dir:str, up=False) -> None:
index = os.path.join(dir, 'cdocindex.html')
with open(index, 'w') as fp:
print(HEAD, file=fp)
if up:
print(f'<li><a href="../cdocindex.html">Up One Level</a></li>', file=fp)
for h in htmls:
p = os.path.relpath(h, dir)
print(f'<li><a href="{p}">{p[:-5]}</a></li>', file=fp)
print(TAIL, file=fp)
if __name__ == '__main__':
main()