Files

160 lines
6.6 KiB
Python
Raw Permalink Normal View History

2026-09-22 07:50:20 -06:00
#!/usr/bin/env python3
r"""Aggregate UPDATES.md files below an explicitly selected directory into local HTML."""
from __future__ import annotations
import argparse
import html
import os
from datetime import datetime
from pathlib import Path
DEFAULT_OUTPUT = Path(__file__).resolve().parent.parent / "output/updates-aggregate.html"
IGNORED_DIRS = {".git", ".hg", ".svn", ".venv", "node_modules"}
def find_update_files(folder: Path) -> list[Path]:
"""Return the folder's UPDATES.md files in alphabetical path order."""
matches: list[Path] = []
for current_dir, dir_names, file_names in os.walk(folder):
dir_names[:] = sorted(
(name for name in dir_names if name not in IGNORED_DIRS),
key=str.casefold,
)
if "UPDATES.md" in file_names:
matches.append(Path(current_dir) / "UPDATES.md")
return sorted(matches, key=lambda path: str(path).casefold())
def build_html(projects_dir: Path) -> str:
folders = sorted(
(path for path in projects_dir.iterdir() if path.is_dir()),
key=lambda path: path.name.casefold(),
)
sections: list[str] = []
nav_items: list[str] = []
total_files = 0
for index, folder in enumerate(folders):
update_files = find_update_files(folder)
total_files += len(update_files)
anchor = f"folder-{index}"
background_class = " red-background" if index % 2 else ""
nav_items.append(
f'<li><a href="#{anchor}">{html.escape(folder.name)}</a>'
f" ({len(update_files)})</li>"
)
file_blocks: list[str] = []
for update_file in update_files:
relative_path = update_file.relative_to(projects_dir)
content = update_file.read_text(encoding="utf-8-sig", errors="replace")
file_blocks.append(
'<article class="update-file">'
f"<h2>{html.escape(str(relative_path))}</h2>"
f"<pre>{html.escape(content)}</pre>"
"</article>"
)
if not file_blocks:
file_blocks.append('<p class="empty">No UPDATES.md files found.</p>')
sections.append(
f'<section id="{anchor}" class="project-folder{background_class}">'
f"<h1>{html.escape(folder.name)}</h1>"
+ "".join(file_blocks)
+ "</section>"
)
generated = datetime.now().astimezone().strftime("%Y-%m-%d %H:%M %Z")
return f"""<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Project Updates</title>
<style>
:root {{
color-scheme: dark;
--dark: #444444;
--steel: #587b91;
--steel-deep: #456579;
--white: #ffffff;
--cream: #eeeecd;
--jade: #52999a;
--pink: #ff1a51;
--line: #84a2b2;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}}
* {{ box-sizing: border-box; }}
html {{ scroll-behavior: smooth; }}
body {{ margin: 0; color: var(--white); background: var(--steel); font: 15px/1.5 var(--font, Inter, ui-sans-serif, system-ui, sans-serif); }}
header, main {{ width: min(1400px, 90%); margin: 0 auto; }}
header {{ padding: 20px 0 24px; border-bottom: 1px solid #ffffff55; }}
header h1 {{ display: inline-block; margin: 0 0 18px; padding: 10px 14px; color: var(--white); background: var(--dark); border-left: 5px solid var(--pink); border-radius: 8px; box-shadow: 4px 4px 0 var(--pink); font-size: clamp(1.6rem, 3vw, 2.35rem); letter-spacing: .04em; }}
header p {{ margin: .25rem 0; color: #e4edf0; }}
nav {{ margin-top: 18px; }}
nav ul {{ display: flex; flex-wrap: wrap; gap: 8px; margin: 0; padding: 0; list-style: none; }}
nav li {{ margin: 0; }}
nav a {{ display: inline-block; padding: 7px 10px; color: #1649a5; background: var(--cream); border-radius: 9px; text-decoration: none; font-size: 13px; font-weight: 800; }}
nav a:hover, nav a:focus {{ color: var(--white); background: var(--pink); }}
.project-folder {{ margin: 14px 0; padding: 0; overflow: hidden; background: var(--dark); border: 1px solid #ffffff30; border-radius: 14px; box-shadow: 0 5px 18px #263e4c44; }}
.project-folder.red-background {{ background: var(--dark); }}
.project-folder > h1 {{ margin: 0; padding: 18px 20px; color: var(--white); border-left: 5px solid var(--jade); font-size: 1.35rem; letter-spacing: .04em; }}
.project-folder.red-background > h1 {{ border-left-color: var(--pink); }}
.update-file {{ margin: 0; padding: 16px 20px 20px; border-top: 1px solid #ffffff22; background: var(--steel-deep); }}
.update-file h2 {{ margin: 0 0 12px; color: var(--cream); font-size: .85rem; font-weight: 800; overflow-wrap: anywhere; }}
pre {{ margin: 0; padding: 14px; overflow-x: auto; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--dark); background: #f7fbfc; border-left: 3px solid var(--jade); border-radius: 8px; font: 13px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; }}
.red-background .update-file pre {{ border-left-color: var(--pink); }}
.empty {{ margin: 0; padding: 18px 20px 20px; color: #e4edf0; font-style: italic; }}
@media (max-width: 700px) {{
header, main {{ width: 92%; }}
header {{ padding-top: 14px; }}
.project-folder > h1, .update-file {{ padding-left: 14px; padding-right: 14px; }}
nav ul {{ gap: 6px; }}
nav a {{ padding: 6px 8px; font-size: 12px; }}
}}
</style>
</head>
<body>
<header>
<h1>Project Updates</h1>
<p>{total_files} UPDATES.md files across {len(folders)} top-level folders.</p>
<p>Generated {html.escape(generated)} from the configured projects directory.</p>
<nav><ul>{"".join(nav_items)}</ul></nav>
</header>
<main>{"".join(sections)}</main>
</body>
</html>
"""
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--projects-dir",
type=Path,
required=True,
help="Explicit directory to scan; its contents will appear in the local output.",
)
parser.add_argument(
"--output",
type=Path,
default=DEFAULT_OUTPUT,
help=f"HTML output file (default: {DEFAULT_OUTPUT})",
)
args = parser.parse_args()
if not args.projects_dir.is_dir():
parser.error(f"Projects directory does not exist: {args.projects_dir}")
output = args.output.resolve()
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(build_html(args.projects_dir.resolve()), encoding="utf-8")
print(f"Wrote {output}")
if __name__ == "__main__":
main()