Fix broken figure lightbox clicks and stale slide URL (#1795, #1424)

Add fix_lightbox_paths.py post-render script to rewrite lightbox hrefs
for mediabag SVGs — Quarto generates the wrong path, causing 404 on
click-to-enlarge. Fixes 210 links across 30 chapter files in both
volumes. Also replace stale scholar.harvard.edu URL in slides README.
This commit is contained in:
Vijay Janapa Reddi
2026-05-25 10:26:26 -04:00
parent 2a0acde76d
commit 070d2ac31d
4 changed files with 79 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ project:
post-render:
- scripts/clean_svgs.py
- scripts/resolve_cross_references.py
- scripts/fix_lightbox_paths.py
# Render-truth backstop: fail the build if any unresolved `?@xxx-yyy`
# literal survives the resolve-cross-references pass. See script header.
- scripts/verify_rendered_xrefs.py

View File

@@ -22,6 +22,7 @@ project:
post-render:
- scripts/clean_svgs.py
- scripts/resolve_cross_references.py
- scripts/fix_lightbox_paths.py
# Render-truth backstop: fail the build if any unresolved `?@xxx-yyy`
# literal survives the resolve-cross-references pass. See script header.
- scripts/verify_rendered_xrefs.py

View File

@@ -0,0 +1,76 @@
#!/usr/bin/env python3
"""
Post-render step: fix lightbox hrefs for mediabag SVGs.
Quarto's lightbox feature generates <a href="HASH.svg"> for images stored in
the mediabag directory, but the actual file lives at
chapter_files/mediabag/HASH.svg. The <img src> inside the anchor is correct;
only the lightbox href is wrong. This causes a 404 when clicking a figure
to enlarge it.
This script rewrites each lightbox <a href="HASH.svg"> to use the path from
the child <img src>, making the enlarged-view link resolve correctly.
Fixes: https://github.com/harvard-edge/cs249r_book/issues/1795
"""
import re
import sys
from pathlib import Path
LIGHTBOX_PATTERN = re.compile(
r'<a\s+href="([0-9a-f]{20,}\.svg)"(\s+class="lightbox"[^>]*>)'
r'(<img\s+src="([^"]+)"[^>]*>)</a>'
)
def fix_lightbox_hrefs(html_path: Path) -> int:
text = html_path.read_text(encoding="utf-8")
count = 0
def replacer(m: re.Match) -> str:
nonlocal count
old_href = m.group(1)
attrs = m.group(2)
img_tag = m.group(3)
img_src = m.group(4)
if old_href != img_src:
count += 1
return f'<a href="{img_src}"{attrs}{img_tag}</a>'
return m.group(0)
new_text = LIGHTBOX_PATTERN.sub(replacer, text)
if count > 0:
html_path.write_text(new_text, encoding="utf-8")
return count
def main() -> None:
if len(sys.argv) > 1:
build_dir = Path(sys.argv[1])
else:
script_dir = Path(__file__).resolve().parent
project_dir = script_dir.parent
build_dir = project_dir / "_build"
if not build_dir.exists():
build_dir = project_dir
html_files = sorted(build_dir.rglob("*.html"))
total_fixes = 0
files_fixed = 0
for html_file in html_files:
fixes = fix_lightbox_hrefs(html_file)
if fixes > 0:
total_fixes += fixes
files_fixed += 1
if total_fixes > 0:
print(f"[fix-lightbox] Fixed {total_fixes} lightbox href(s) in {files_fixed} file(s).")
else:
print("[fix-lightbox] No lightbox mediabag mismatches found.")
if __name__ == "__main__":
main()

View File

@@ -17,7 +17,7 @@ TinyML on edX is designed in four chapters across 3 courses as described below.
## The TinyMLx Team:
### Course Instructors
+ [Vijay Janapa Reddi](https://scholar.harvard.edu/vijay-janapa-reddi)
+ [Vijay Janapa Reddi](https://vijay.seas.harvard.edu)
+ [Laurence Maroney](http://www.laurencemoroney.com/)
+ [Pete Warden](https://petewarden.com/)
+ [Lara Suzuki](https://larissasuzuki.com/)