-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_index.py
executable file
·60 lines (49 loc) · 1.42 KB
/
create_index.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
#!/usr/bin/python3
import os
import glob
exclude_dirs = ['.git']
# glob patterns of paths you want to exclude
exclude_path = ['.git*', '*.DS_Store*']
all_dirs = {} # Key is path, value is list of directories
all_files = {} # Key is path, value is list of directories
# Iterate over all directories and sub-directories
for path, dirs, files in os.walk("."):
# path begins with ./
if set(path[2:].split("/")).intersection(exclude_dirs):
continue
all_dirs[path] = dirs
all_files[path] = files
def create_html(header, list_of_paths):
li_elements = ""
for path in list_of_paths:
li_elements += f'<li><a href="{path}">{path}</li>\n'
html = f"""
<body>
<html>
<h2>{header}</h2>
<p>
{li_elements}
</p>
</body>
</html>
"""
return html
cwd = os.getcwd()
for _path in all_dirs.keys():
_dirs = all_dirs[_path]
_files = all_files[_path]
list_of_paths = []
path = _path.replace('.', '')
for link in [*_dirs, *_files]:
link_is_included = True
for p in exclude_path:
if glob.fnmatch.fnmatch(link, p):
link_is_included = False
continue
if link_is_included:
list_of_paths.append(link)
html = create_html(path, list_of_paths)
index_path = cwd + path + "/index.html"
with open(index_path, "w") as index:
index.write(html)
print("Written ", index_path)