|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Post-process JB2-built HTML to strip error output divs. |
| 4 | +
|
| 5 | +JB1 equivalent: nmaci/scripts/parse_html_for_errors.py |
| 6 | +JB2 difference: MyST uses different CSS classes for cell output containers. |
| 7 | +
|
| 8 | +JB1 class: "cell_output docutils container" |
| 9 | +JB2 classes tried (in order): |
| 10 | + - "cell_output" (MyST book-theme) |
| 11 | + - "output" (fallback) |
| 12 | + - any <div> containing the error text (last resort) |
| 13 | +
|
| 14 | +Run as: python parse_html_for_errors_v2.py student |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import yaml |
| 20 | +from bs4 import BeautifulSoup |
| 21 | + |
| 22 | +ARG = sys.argv[1] # "student" or "instructor" |
| 23 | + |
| 24 | +ERROR_STRINGS = ["NotImplementedError", "NameError"] |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + with open("tutorials/materials.yml") as fh: |
| 29 | + materials = yaml.load(fh, Loader=yaml.FullLoader) |
| 30 | + |
| 31 | + html_directory = "book/_build/html/" |
| 32 | + total_removed = 0 |
| 33 | + |
| 34 | + for m in materials: |
| 35 | + name = f"{m['day']}_{''.join(m['name'].split())}" |
| 36 | + |
| 37 | + notebook_paths = [] |
| 38 | + if os.path.exists(f"tutorials/{name}/{m['day']}_Intro.ipynb"): |
| 39 | + notebook_paths.append( |
| 40 | + f"{html_directory}/tutorials/{name}/{ARG}/{m['day']}_Intro.html" |
| 41 | + ) |
| 42 | + notebook_paths += [ |
| 43 | + f"{html_directory}/tutorials/{name}/{ARG}/{m['day']}_Tutorial{i + 1}.html" |
| 44 | + for i in range(m["tutorials"]) |
| 45 | + ] |
| 46 | + if os.path.exists(f"tutorials/{name}/{m['day']}_Outro.ipynb"): |
| 47 | + notebook_paths.append( |
| 48 | + f"{html_directory}/tutorials/{name}/{ARG}/{m['day']}_Outro.html" |
| 49 | + ) |
| 50 | + |
| 51 | + for html_path in notebook_paths: |
| 52 | + if not os.path.exists(html_path): |
| 53 | + print(f" Warning: {html_path} not found, skipping") |
| 54 | + continue |
| 55 | + |
| 56 | + with open(html_path, encoding="utf-8") as f: |
| 57 | + contents = f.read() |
| 58 | + |
| 59 | + parsed_html = BeautifulSoup(contents, features="html.parser") |
| 60 | + removed = strip_error_divs(parsed_html) |
| 61 | + total_removed += removed |
| 62 | + |
| 63 | + # Put solution figures in center (matches JB1 behaviour) |
| 64 | + for img in parsed_html.find_all("img", alt=True): |
| 65 | + if img["alt"] == "Solution hint": |
| 66 | + img["align"] = "center" |
| 67 | + img["class"] = "align-center" |
| 68 | + |
| 69 | + with open(html_path, "w", encoding="utf-8") as f: |
| 70 | + f.write(str(parsed_html)) |
| 71 | + |
| 72 | + if removed: |
| 73 | + print( |
| 74 | + f" Stripped {removed} error div(s) from {os.path.basename(html_path)}" |
| 75 | + ) |
| 76 | + |
| 77 | + print(f"Done. Removed {total_removed} error output div(s) total.") |
| 78 | + |
| 79 | + |
| 80 | +def strip_error_divs(parsed_html): |
| 81 | + """Remove output divs that contain NotImplementedError or NameError text. |
| 82 | +
|
| 83 | + Tries JB1's class first, then JB2/MyST class names, then a broad sweep. |
| 84 | + Returns the number of divs removed. |
| 85 | + """ |
| 86 | + removed = 0 |
| 87 | + |
| 88 | + # JB1 class (sphinx/docutils) |
| 89 | + candidates = parsed_html.find_all( |
| 90 | + "div", {"class": "cell_output docutils container"} |
| 91 | + ) |
| 92 | + |
| 93 | + # JB2/MyST book-theme output wrapper |
| 94 | + if not candidates: |
| 95 | + candidates = parsed_html.find_all("div", {"class": "cell_output"}) |
| 96 | + |
| 97 | + # Broader fallback: any <div> that directly wraps an error traceback |
| 98 | + if not candidates: |
| 99 | + candidates = parsed_html.find_all("div", class_=lambda c: c and "output" in c) |
| 100 | + |
| 101 | + for div in candidates: |
| 102 | + if any(err in str(div) for err in ERROR_STRINGS): |
| 103 | + div.decompose() |
| 104 | + removed += 1 |
| 105 | + |
| 106 | + return removed |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments