"""Normalize middle-screen templates: centered layout, no redundant button wrappers.""" import re from pathlib import Path ROOT = Path(__file__).resolve().parents[1] MIDDLE_GLOBS = [ "Risk/templates/Risk/Middle_Screen.html", "Ambiguity_New/templates/Ambiguity_New/Middle_Screen1.html", "Ambiguity_New/templates/Ambiguity_New/Middle_Screen2.html", "Ambiguity_New/templates/Ambiguity_New/Quiz_Middle_Screen.html", "Time_Adaptive_CE/templates/Time_Adaptive_CE/Middle_Screen.html", "Time_Adaptive_CE/templates/Time_Adaptive_CE/Middle_Screen_8w.html", "Time_Adaptive_CE_84/templates/Time_Adaptive_CE_84/Middle_Screen.html", "Time_Adaptive_CE_84/templates/Time_Adaptive_CE_84/Middle_Screen_8w.html", "Time_Adaptive_Lot_1/templates/Time_Adaptive_Lot_1/Middle_Screen.html", "Time_Adaptive_Lot_1/templates/Time_Adaptive_Lot_1/Middle_Screen_8w.html", "Time_Adaptive_Lot_1_84/templates/Time_Adaptive_Lot_1_84/Middle_Screen.html", "Time_Adaptive_Lot_1_84/templates/Time_Adaptive_Lot_1_84/Middle_Screen_8w.html", "Time_Adaptive_Lot_2/templates/Time_Adaptive_Lot_2/Middle_Screen.html", "Time_Adaptive_Lot_2/templates/Time_Adaptive_Lot_2/Middle_Screen_8w.html", "Time_Adaptive_Lot_2_84/templates/Time_Adaptive_Lot_2_84/Middle_Screen.html", "Time_Adaptive_Lot_2_84/templates/Time_Adaptive_Lot_2_84/Middle_Screen_8w.html", ] TRANSITION_CSS = """/* Transition / middle / end screens */ .transition-screen { font-family: Arial, Helvetica, sans-serif; font-size: 18px; line-height: 1.45; text-align: center; padding: 4rem 1.5rem; min-height: 50vh; width: 100%; max-width: 100%; margin: 0 auto; display: flex; flex-direction: column; justify-content: center; align-items: center; } .transition-screen p { margin-bottom: 1rem; text-align: center; width: 100%; max-width: 42em; } .transition-screen button, .transition-screen .btn { margin-top: 1rem; } """ RE_BTN_WRAP = re.compile( r"\s*(]*>.*?)\s*", re.DOTALL | re.IGNORECASE, ) RE_CUSTOMERS_STYLE = re.compile( r"\s*", re.DOTALL, ) RE_TRANSITION_BLOCK = re.compile( r"/\* Transition / middle / end screens \*/\s*\.transition-screen\s*\{.*?\}\s*" r"\.transition-screen p\s*\{.*?\}\s*" r"(?:\.transition-screen button\s*\{.*?\}\s*|\.transition-screen \.btn\s*\{.*?\}\s*)?", re.DOTALL, ) def normalize_html(path: Path) -> bool: text = path.read_text(encoding="utf-8") orig = text text = RE_BTN_WRAP.sub(r"\1", text) text = RE_CUSTOMERS_STYLE.sub("", text) if path.read_text(encoding="utf-8") != text and "transition-screen" not in text: pass if orig != text: path.write_text(text, encoding="utf-8") return True return False def update_custom_css(path: Path) -> bool: text = path.read_text(encoding="utf-8") if ".transition-screen" not in text: return False new_text, n = RE_TRANSITION_BLOCK.subn(TRANSITION_CSS + "\n", text, count=1) if n: path.write_text(new_text, encoding="utf-8") return True return False def main(): html_n = 0 for rel in MIDDLE_GLOBS: p = ROOT / rel if p.exists() and normalize_html(p): print("html", rel) html_n += 1 css_n = 0 for css in ROOT.glob("**/static/**/css/custom.css"): if "transition-screen" in css.read_text(encoding="utf-8") and update_custom_css(css): print("css", css.relative_to(ROOT)) css_n += 1 print(f"updated {html_n} templates, {css_n} css files") if __name__ == "__main__": main()