forked from ProzorroUKR/standards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_index.py
executable file
·102 lines (78 loc) · 2.3 KB
/
build_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
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
#! /usr/bin/python
from collections import defaultdict
import os
LAYOUT = """---
layout: default
---
<body>{}</body>"""
LANG_CODES = ("en", "uk", "ru", "ro")
IGNORE_EXT = ("py", "txt", "html")
IGNORE_FILES = ("CNAME", "_config.yml")
def find_versions(file_names):
versions = defaultdict(list)
for name, full_name in file_names:
options = []
if name in IGNORE_FILES:
continue
name_parts = name.split(".")
if name_parts[-1] in IGNORE_EXT:
continue
name = name_parts[0]
options.extend(name_parts[1:])
if name.endswith("_pretty"):
name = name[:-7]
options.append("pretty")
if name.endswith("_annotated"):
name = name[:-10]
options.append("annotated")
if name[-2:] in LANG_CODES:
options.append(name[-2:])
name = name[:-2]
versions[name].append(
(".".join(reversed(options)), full_name)
)
return versions
def convert_to_list(strings):
if strings:
return "<ul><li>{}</li></ul>".format("</li><li>".join(strings))
def versions_to_html(args):
response = " ".join(
'<a href="{}" target="_blank">{}</a>'.format(full_name, v_name)
for v_name, full_name in args
)
return response
def get_files(path):
files, dirs = [], []
for f_name in os.listdir(path):
if f_name.startswith(path):
continue
full_name = os.path.join(path, f_name)
if os.path.isdir(full_name):
line = get_files(full_name)
if line:
dirs.append("{} {}".format(f_name, line))
else:
files.append(
(f_name, full_name)
)
file_versions = find_versions(files)
items = []
for f_name, versions in sorted(file_versions.items()):
items.append(
"{} [ {} ]".format(
f_name,
versions_to_html(versions)
)
)
items.extend(dirs)
response = ""
if items:
if len(items) > 1:
response = convert_to_list(items)
else:
response = items[0]
return response
if __name__ == "__main__":
data = get_files(".")
with open("index.html", "w") as f:
f.write(LAYOUT.format(data))