-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
62 lines (49 loc) · 2.13 KB
/
utils.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
import os
from htmlmin import minify
def directory_tree(path):
string_build = ""
space = " "
for dirpath, dirnames, filenames in os.walk(path):
directory_level = dirpath.replace(path, "")
directory_level = directory_level.count(os.sep)
if directory_level == 0:
root_display = 'style="display: inline!important"'
alter_display = 'style="display: none!important"'
dir_name = f"{dirpath.split(os.sep)[-1]}"
dir_sanitized = dirpath.replace(path, "")
dir_id = dir_sanitized.replace(os.sep, "_")
else:
root_display = ""
alter_display = ''
dir_name = os.path.basename(dirpath)
dir_sanitized = dirpath.replace(path, "")
dir_id = dir_sanitized.replace(os.sep, "_")
indent = space * (4 * directory_level)
indent_2 = space * (4 * directory_level + 1)
string_build += f'''
<div data-id="{dir_id}" class="directory-wrapper">
{indent}
<span class="directory-close" {root_display}>📁 </span>
<span class="directory-open" {alter_display}>📂 </span>
<strong>{dir_name}</strong>/
<div id="{dir_id}" class="directory-child" {root_display}>
'''
for f in filenames:
if f.endswith(".md"):
if directory_level == 0 and f == "README.md":
menu = " menu-selected"
select_id = ''
else:
menu = ""
select_id = f'{dir_id}__sep__{f.replace(".md", "")}'
string_build += f'''
<div>
{indent_2}
<a class="section-link{menu}"
id="{select_id}"
href="?dir={dir_sanitized}&file={f}#{select_id}">
<span>🗎 </span>{f}
</a>
</div>'''
string_build += "</div></div>"
return minify(f"<div>{string_build}</div>")