+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/main.py b/app/main.py
new file mode 100644
index 0000000..ff719e4
--- /dev/null
+++ b/app/main.py
@@ -0,0 +1,426 @@
+import os
+import sys
+import re
+import mimetypes
+import urllib.parse
+import posixpath
+from http.server import HTTPServer, BaseHTTPRequestHandler
+import markdown
+
+# Configurable paths
+DOCS_ROOT = os.path.realpath(os.environ.get("DOCS_ROOT", "/docs"))
+PORT = int(os.environ.get("PORT", "8000"))
+
+# Read layout template
+TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), 'layout.html')
+try:
+ with open(TEMPLATE_PATH, 'r', encoding='utf-8') as f:
+ TEMPLATE = f.read()
+except OSError:
+ print(f"Error: Could not read template from {TEMPLATE_PATH}", file=sys.stderr)
+ TEMPLATE = "{{ content }}"
+
+
+def is_safe_path(path):
+ """Ensure the path is within the DOCS_ROOT directory to prevent traversal."""
+ real_path = os.path.realpath(path)
+ return real_path == DOCS_ROOT or real_path.startswith(DOCS_ROOT + os.sep)
+
+
+def get_breadcrumbs(url_path):
+ """Generate beautiful navigation breadcrumbs."""
+ parts = [p for p in url_path.split('/') if p]
+ breadcrumbs_html = 'Home'
+
+ current_acc = ""
+ for i, part in enumerate(parts):
+ current_acc += f"/{part}"
+ breadcrumbs_html += ' / '
+ if i == len(parts) - 1:
+ breadcrumbs_html += f'{part}'
+ else:
+ breadcrumbs_html += f'{part}'
+
+ return breadcrumbs_html
+
+
+def build_tree(path):
+ """Recursively build a tree of folders and markdown files."""
+ items = []
+ try:
+ names = os.listdir(path)
+ except OSError:
+ return []
+
+ for name in names:
+ if name.startswith('.'):
+ continue
+ if name in ('node_modules', 'venv', '.git', '__pycache__', 'data'):
+ continue
+
+ full_path = os.path.join(path, name)
+ rel_path = os.path.relpath(full_path, DOCS_ROOT)
+
+ if os.path.isdir(full_path):
+ children = build_tree(full_path)
+ # Only list directory if it contains markdown files or subdirs with markdown files
+ if children:
+ items.append({
+ 'name': name,
+ 'type': 'dir',
+ 'rel_path': rel_path.replace(os.sep, '/'),
+ 'children': children
+ })
+ elif name.endswith('.md'):
+ url_name = name[:-3]
+ # Handle root readme differently or filter it
+ if url_name.lower() in ('readme', 'index') and rel_path == name:
+ # We skip showing root readme directly in sidebar tree to prevent clutter,
+ # as it is already mapped to the Home button.
+ continue
+
+ # Form clean url
+ url_path = os.path.splitext(rel_path)[0]
+ url = '/' + url_path.replace(os.sep, '/')
+
+ # If it's a subdirectory README, we want to link it to the folder URL
+ if url_name.lower() in ('readme', 'index'):
+ parent_dir = os.path.dirname(rel_path)
+ url = '/' + parent_dir.replace(os.sep, '/') if parent_dir else '/'
+ display_name = os.path.basename(parent_dir) if parent_dir else 'Home'
+ else:
+ display_name = url_name
+
+ items.append({
+ 'name': display_name,
+ 'type': 'file',
+ 'url': url,
+ 'rel_path': rel_path.replace(os.sep, '/')
+ })
+
+ # Sort folders first, then files
+ items.sort(key=lambda x: (0 if x['type'] == 'dir' else 1, x['name'].lower()))
+ return items
+
+
+def render_tree(tree, active_url):
+ """Render the built tree into HTML nested ul/details list."""
+ if not tree:
+ return ""
+
+ html = '
'
+ for item in tree:
+ if item['type'] == 'dir':
+ # Check if active URL is nested inside this directory
+ active_clean = active_url.lstrip('/')
+ is_open = active_clean.startswith(item['rel_path'] + '/') or active_clean == item['rel_path']
+ open_attr = ' open' if is_open else ''
+
+ html += f'
đ {item["name"]}'
+ html += render_tree(item['children'], active_url)
+ html += '
'
+ else:
+ is_active = ' active' if active_url == item['url'] else ''
+ html += f'
"
+
+ for name in names:
+ if name.startswith('.') or name in ('node_modules', 'venv', '.git', '__pycache__', 'data'):
+ continue
+
+ full_p = os.path.join(folder_path, name)
+ is_dir = os.path.isdir(full_p)
+
+ if is_dir:
+ url = posixpath.join(rel_url_path, name)
+ html += f"