Bonsai_Tutorials/genList.py

74 lines
2.7 KiB
Python

import os
import re
import html
# Priority file extensions in desired order
priority_extensions = [".mp4", ".blend", ".ifc"]
# Regex for folders starting with 6 digits
folder_pattern = re.compile(r"^\d{6}")
# Output HTML file
output_file = "index.html"
def generate_html():
current_dir = os.getcwd()
# Filter folders that start with 6 digits
folders = [f for f in os.listdir(current_dir)
if os.path.isdir(os.path.join(current_dir, f)) and folder_pattern.match(f)]
html_lines = []
html_lines.append("<!DOCTYPE html>")
html_lines.append("<html><head><meta charset='UTF-8'><title>Folder Index</title>")
html_lines.append("<style>"
"table { border-collapse: collapse; width: 100%; }"
"th, td { border: 1px solid #ccc; padding: 6px; text-align: left; }"
"th { background-color: #f0f0f0; }"
"</style></head><body>")
html_lines.append("<h1>Bonsai Tutorials Table of Contents</h1>")
# Table headers
headers = ["Folder"] + [ext.lower() for ext in priority_extensions] + ["Other Files / Subfolders"]
html_lines.append("<table>")
html_lines.append("<tr>" + "".join([f"<th>{html.escape(h)}</th>" for h in headers]) + "</tr>")
for folder in sorted(folders):
folder_path = os.path.join(current_dir, folder)
files = sorted(os.listdir(folder_path))
# Initialize columns for priority files
columns = {ext: "" for ext in priority_extensions}
others = []
for f in files:
file_path = os.path.join(folder_path, f)
if os.path.isdir(file_path):
# Subfolder link
others.append(f"<a href='{html.escape(folder)}/{html.escape(f)}/'>{html.escape(f)}/</a>")
else:
ext = os.path.splitext(f)[1].lower()
if ext in columns and columns[ext] == "":
columns[ext] = f"<a href='{html.escape(folder)}/{html.escape(f)}'>{html.escape(f)}</a>"
else:
others.append(f"<a href='{html.escape(folder)}/{html.escape(f)}'>{html.escape(f)}</a>")
# Build table row
row = [f"<a href='{html.escape(folder)}/'>{html.escape(folder)}</a>"]
row += [columns[ext] for ext in priority_extensions]
row.append(" | ".join(others))
html_lines.append("<tr>" + "".join([f"<td>{cell}</td>" for cell in row]) + "</tr>")
html_lines.append("</table></body></html>")
# Write to file
with open(output_file, "w", encoding="utf-8") as f:
f.write("\n".join(html_lines))
print(f"HTML index generated: {output_file}")
if __name__ == "__main__":
generate_html()