This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
generated from bharxhav/cdn-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdn_index_generator.py
72 lines (58 loc) · 1.99 KB
/
cdn_index_generator.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
import os
from urllib.parse import quote
INITIAL_HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My CDN</title>
<style>
body { font-family: monospace; }
pre { margin-left: 20px; }
a { text-decoration: none; color: #028391; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>My CDN</h1>
<div id="file-list"></div>
</body>
</html>
"""
def generate_structure(path, prefix='', top_level=True):
structure = []
try:
entries = sorted(os.scandir(path), key=lambda e: (
not e.is_dir(), e.name.lower()))
for entry in entries:
if entry.name.startswith('.'):
continue
if top_level and not entry.is_dir():
continue
rel_path = os.path.relpath(entry.path, '.')
if entry.is_dir():
structure.append(f"{prefix}{entry.name}/")
structure.extend(generate_structure(
entry.path, prefix + '│ ', False))
else:
encoded_path = quote(rel_path)
structure.append(
f"{prefix}├── <a href='{encoded_path}'>{entry.name}</a>")
if structure and not top_level:
structure[-1] = structure[-1].replace('├──', '└──')
except PermissionError:
structure.append(f"{prefix}[Permission Denied]")
return structure
def generate_html_content(folder_structure):
return "<pre>\n" + "\n".join(folder_structure) + "\n</pre>"
def main():
folder_structure = generate_structure('.')
html_content = generate_html_content(folder_structure)
updated_content = INITIAL_HTML.replace(
'<div id="file-list"></div>',
f'<div id="file-list">{html_content}</div>'
)
with open('index.html', 'w') as file:
file.write(updated_content)
if __name__ == "__main__":
main()