#!/usr/bin/env python3 from __future__ import annotations import argparse from pathlib import Path def read_title(markdown_path: Path) -> str: for line in markdown_path.read_text(encoding='utf-8').splitlines(): if line.startswith('# '): return line[2:].strip() return markdown_path.stem def build_index(workshops_dir: Path) -> str: sections: list[str] = ['# Workshops Index'] for subdir in sorted(path for path in workshops_dir.iterdir() if path.is_dir()): sections.append(f'## {subdir.name}') markdown_files = sorted(subdir.glob('*.md')) if not markdown_files: sections.append('_No Markdown files found._') continue for markdown_file in markdown_files: title = read_title(markdown_file) relative_path = markdown_file.relative_to(workshops_dir) sections.append(f'- [{title}]({relative_path.as_posix()})') sections.append('') return '\n'.join(sections).rstrip() + '\n' def main() -> None: parser = argparse.ArgumentParser(description='Generate a Markdown index for workshop subdirectories.') parser.add_argument('directory', type=Path, help='Workshops directory containing topic subdirectories') args = parser.parse_args() index_path = args.directory / 'index.md' index_path.write_text(build_index(args.directory), encoding='utf-8') print(index_path) if __name__ == '__main__': main()