"""Point Instructions pages and modals at the 12 segment-specific body files.""" import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PAGE_INCLUDES = { "Time_Adaptive_CE/templates/Time_Adaptive_CE/Instructions.html": "time_ce_48_4w.html", "Time_Adaptive_CE/templates/Time_Adaptive_CE/Instructions_8w.html": "time_ce_48_8w.html", "Time_Adaptive_CE_84/templates/Time_Adaptive_CE_84/Instructions.html": "time_ce_84_4w.html", "Time_Adaptive_CE_84/templates/Time_Adaptive_CE_84/Instructions_8w.html": "time_ce_84_8w.html", "Time_Adaptive_Lot_1/templates/Time_Adaptive_Lot_1/Instructions.html": "time_Lot_1_48_4w.html", "Time_Adaptive_Lot_1/templates/Time_Adaptive_Lot_1/Instructions_8w.html": "time_Lot_1_48_8w.html", "Time_Adaptive_Lot_1_84/templates/Time_Adaptive_Lot_1_84/Instructions.html": "time_Lot_1_84_4w.html", "Time_Adaptive_Lot_1_84/templates/Time_Adaptive_Lot_1_84/Instructions_8w.html": "time_Lot_1_84_8w.html", "Time_Adaptive_Lot_2/templates/Time_Adaptive_Lot_2/Instructions.html": "time_Lot_2_48_4w.html", "Time_Adaptive_Lot_2/templates/Time_Adaptive_Lot_2/Instructions_8w.html": "time_Lot_2_48_8w.html", "Time_Adaptive_Lot_2_84/templates/Time_Adaptive_Lot_2_84/Instructions.html": "time_Lot_2_84_4w.html", "Time_Adaptive_Lot_2_84/templates/Time_Adaptive_Lot_2_84/Instructions_8w.html": "time_Lot_2_84_8w.html", } MODAL_BODY = { "Time_Adaptive_CE": { "4w": "time_ce_48_4w.html", "8w": "time_ce_48_8w.html", }, "Time_Adaptive_CE_84": { "4w": "time_ce_84_4w.html", "8w": "time_ce_84_8w.html", }, "Time_Adaptive_Lot_1": { "4w": "time_Lot_1_48_4w.html", "8w": "time_Lot_1_48_8w.html", }, "Time_Adaptive_Lot_1_84": { "4w": "time_Lot_1_84_4w.html", "8w": "time_Lot_1_84_8w.html", }, "Time_Adaptive_Lot_2": { "4w": "time_Lot_2_48_4w.html", "8w": "time_Lot_2_48_8w.html", }, "Time_Adaptive_Lot_2_84": { "4w": "time_Lot_2_84_4w.html", "8w": "time_Lot_2_84_8w.html", }, } INCLUDE_LINE = re.compile( r"\{%\s*include\s+'instructions/[^']+'(?:\s+with\s+[^%]+)?\s*%\}" ) MODAL_LINE = re.compile( r"\{%\s*include\s+'instructions/time_instructions_modal\.html'\s+with\s+[^%]+\s*%\}" ) def update_instruction_pages() -> None: for rel, body in PAGE_INCLUDES.items(): path = ROOT / rel.replace("/", "\\") if "\\" not in rel else ROOT / rel path = ROOT / Path(rel) text = path.read_text(encoding="utf-8") new_line = f"{{% include 'instructions/{body}' %}}" text2, n = INCLUDE_LINE.subn(new_line, text, count=1) if n != 1: raise ValueError(f"{rel}: expected 1 include replace, got {n}") path.write_text(text2, encoding="utf-8") print(f" page {rel} -> {body}") def segment_from_filename(name: str) -> str: return "8w" if "_8w" in name else "4w" def update_modals() -> None: apps = list(MODAL_BODY.keys()) for app in apps: tpl = ROOT / app / "templates" / app for html in tpl.glob("*.html"): if html.name.startswith("Instructions"): continue text = html.read_text(encoding="utf-8") if "time_instructions_modal" not in text: continue seg = segment_from_filename(html.name) body = MODAL_BODY[app][seg] new_line = ( f"{{% include 'instructions/time_instructions_modal.html' " f"with body_template='instructions/{body}' %}}" ) text2, n = MODAL_LINE.subn(new_line, text) if n == 0: continue html.write_text(text2, encoding="utf-8") print(f" modal {app}/{html.name} -> {body} ({n}x)") def main() -> None: print("Instruction pages:") update_instruction_pages() print("Choice/quiz modals:") update_modals() print("Done.") if __name__ == "__main__": main()