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("")
html_lines.append("
Folder Index")
html_lines.append("")
html_lines.append("Bonsai Tutorials Table of Contents
")
# Table headers
headers = ["Folder"] + [ext.lower() for ext in priority_extensions] + ["Other Files / Subfolders"]
html_lines.append("")
html_lines.append("" + "".join([f"| {html.escape(h)} | " for h in headers]) + "
")
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"{html.escape(f)}/")
else:
ext = os.path.splitext(f)[1].lower()
if ext in columns and columns[ext] == "":
columns[ext] = f"{html.escape(f)}"
else:
others.append(f"{html.escape(f)}")
# Build table row
row = [f"{html.escape(folder)}"]
row += [columns[ext] for ext in priority_extensions]
row.append(" | ".join(others))
html_lines.append("" + "".join([f"| {cell} | " for cell in row]) + "
")
html_lines.append("
")
# 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()