|
15 | 15 | ) |
16 | 16 | CANVAS_COURSE_PATTERN = re.compile(r"canvas\.uchicago\.edu/courses/\d+") |
17 | 17 |
|
| 18 | +EXAMPLES_EXCLUDE_DIRS = {"__pycache__", "images"} |
| 19 | +EXAMPLES_EXCLUDE_SUFFIXES = {".pyc"} |
| 20 | + |
| 21 | + |
| 22 | +def build_examples_indexes(examples_root: Path, env, base_url: str): |
| 23 | + """Generate index.html pages for the examples tree. |
| 24 | +
|
| 25 | + Walks examples/ recursively. For any directory without an existing |
| 26 | + index.html, renders one that lists its subdirectories and files. |
| 27 | + The top-level page lists each week; deeper pages list their contents. |
| 28 | + """ |
| 29 | + template = env.get_template("examples_index.html") |
| 30 | + |
| 31 | + def is_hidden(p: Path) -> bool: |
| 32 | + return any(part.startswith(".") for part in p.relative_to(examples_root).parts) |
| 33 | + |
| 34 | + def is_excluded(p: Path) -> bool: |
| 35 | + return any(part in EXAMPLES_EXCLUDE_DIRS for part in p.relative_to(examples_root).parts) |
| 36 | + |
| 37 | + all_dirs = [examples_root, *sorted(p for p in examples_root.rglob("*") if p.is_dir())] |
| 38 | + for dirpath in all_dirs: |
| 39 | + if is_hidden(dirpath) or is_excluded(dirpath): |
| 40 | + continue |
| 41 | + if (dirpath / "index.html").exists(): |
| 42 | + continue |
| 43 | + |
| 44 | + rel = dirpath.relative_to(examples_root) |
| 45 | + url_prefix = f"{base_url}/examples" |
| 46 | + if rel != Path("."): |
| 47 | + url_prefix += "/" + "/".join(rel.parts) |
| 48 | + |
| 49 | + entries = sorted(dirpath.iterdir(), key=lambda p: (not p.is_dir(), p.name.lower())) |
| 50 | + dirs = [ |
| 51 | + {"name": p.name, "url": f"{url_prefix}/{p.name}/"} |
| 52 | + for p in entries |
| 53 | + if p.is_dir() |
| 54 | + and p.name not in EXAMPLES_EXCLUDE_DIRS |
| 55 | + and not p.name.startswith(".") |
| 56 | + ] |
| 57 | + files = [ |
| 58 | + {"name": p.name, "url": f"{url_prefix}/{p.name}"} |
| 59 | + for p in entries |
| 60 | + if p.is_file() |
| 61 | + and p.name != "index.html" |
| 62 | + and p.suffix not in EXAMPLES_EXCLUDE_SUFFIXES |
| 63 | + and not p.name.startswith(".") |
| 64 | + ] |
| 65 | + |
| 66 | + if rel == Path("."): |
| 67 | + title = "Examples" |
| 68 | + crumbs = [] |
| 69 | + parent_url = "" |
| 70 | + else: |
| 71 | + title = "Examples — " + " / ".join(part.replace("_", " ") for part in rel.parts) |
| 72 | + crumbs = [{"name": "Examples", "url": f"{base_url}/examples/"}] |
| 73 | + accum = f"{base_url}/examples" |
| 74 | + for part in rel.parts[:-1]: |
| 75 | + accum += f"/{part}" |
| 76 | + crumbs.append({"name": part, "url": f"{accum}/"}) |
| 77 | + crumbs.append({"name": rel.parts[-1], "url": ""}) |
| 78 | + parent_parts = rel.parts[:-1] |
| 79 | + parent_url = f"{base_url}/examples/" + ( |
| 80 | + "/".join(parent_parts) + "/" if parent_parts else "" |
| 81 | + ) |
| 82 | + |
| 83 | + html = template.render( |
| 84 | + title=title, crumbs=crumbs, parent_url=parent_url, dirs=dirs, files=files |
| 85 | + ) |
| 86 | + (dirpath / "index.html").write_text(html) |
| 87 | + |
18 | 88 |
|
19 | 89 | def apply_config(text: str) -> str: |
20 | 90 | """Replace quarter and Canvas course ID references with current config values.""" |
@@ -140,6 +210,9 @@ def build_site(output_dir: str = "_site", base_url: str = ""): |
140 | 210 | homepage_html = env.get_template("index.html").render() |
141 | 211 | (out / "index.html").write_text(homepage_html) |
142 | 212 |
|
| 213 | + # Examples index pages (top-level + per-week + per-subdir) |
| 214 | + build_examples_indexes(out / "examples", env, base_url) |
| 215 | + |
143 | 216 |
|
144 | 217 | if __name__ == "__main__": |
145 | 218 | parser = argparse.ArgumentParser(description="Build the static site.") |
|
0 commit comments