-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_html.py
More file actions
73 lines (58 loc) · 3.28 KB
/
update_html.py
File metadata and controls
73 lines (58 loc) · 3.28 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
72
73
import os
import re
# Define the directories to be scanned
directories = {
"Coding Projects": "Coding Projects",
"Research": "Research"
}
def generate_file_list_html(folder, path):
"""Generate HTML list items for files and folders in the specified path."""
items = sorted(os.listdir(path))
file_links = []
for item in items:
item_path = os.path.join(path, item)
if os.path.isfile(item_path):
file_links.append(f' <li class="file"><span></span><i class="fas fa-file"></i> <a href="{folder}/{item}" target="_blank">{item}</a></li>')
elif os.path.isdir(item_path):
folder_id = f'{folder.replace(" ", "-").lower()}-{item.replace(" ", "-").lower()}'
file_links.append(f''' <li class="folder">
<span></span><i class="fas fa-folder" onclick="toggleFiles('{folder_id}')"></i> {item}
<ul class="files" id="{folder_id}" style="display: none;">
<!-- {folder_id} Start -->
{generate_file_list_html(f"{folder}/{item}", item_path)}
<!-- {folder_id} End -->
</ul>
</li>''')
return '\n'.join(file_links)
def remove_existing_placeholders(content):
"""Remove existing placeholders and their content from the HTML content."""
# Remove main directories placeholders
content = re.sub(r'<!-- Coding Projects Start -->.*<!-- Coding Projects End -->', '<!-- Coding Projects Start --><!-- Coding Projects End -->', content, flags=re.DOTALL)
content = re.sub(r'<!-- Research Start -->.*<!-- Research End -->', '<!-- Research Start --><!-- Research End -->', content, flags=re.DOTALL)
# Remove dynamic folder placeholders
content = re.sub(r'<!-- ([\w-]+) Start -->.*<!-- \1 End -->', r'<!-- \1 Start --><!-- \1 End -->', content, flags=re.DOTALL)
return content
def update_index_html():
"""Update the index.html file with the list of files and folders from the specified directories."""
try:
# Read the current index.html
with open('index.html', 'r') as file:
content = file.read()
# Remove existing placeholders and their content
content = remove_existing_placeholders(content)
# Generate the new HTML content for each directory
for directory_name, directory_path in directories.items():
if os.path.exists(directory_path):
html_content = generate_file_list_html(directory_path, directory_path)
start_placeholder = f'<!-- {directory_name} Start -->'
end_placeholder = f'<!-- {directory_name} End -->'
content = re.sub(f'({re.escape(start_placeholder)}).*({re.escape(end_placeholder)})', f'\\1\n{html_content}\n \\2', content, flags=re.DOTALL)
# Write the updated HTML back to the file
with open('index.html', 'w') as file:
file.write(content)
print("index.html has been updated successfully.")
except Exception as e:
print(f"Error: {e}")
print("Please ensure that the index.html file exists and has the correct structure.")
if __name__ == "__main__":
update_index_html()