# -*- coding: utf-8 -*-
import sys, io, re, pathlib
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52"); CACHE = BASE / "cache"

def clean(t):
    t = re.sub(r"<script.*?</script>", " ", t, flags=re.S)
    t = re.sub(r"<style.*?</style>", " ", t, flags=re.S)
    t = t.replace("&nbsp;", " ").replace("&amp;", "&").replace("&#39;", "'").replace("&quot;", '"')
    t = t.replace("&lt;", "<").replace("&gt;", ">")
    t = re.sub(r"<[^>]+>", "\n", t)
    lines = [re.sub(r"\s+", " ", x).strip() for x in t.split("\n")]
    return [x for x in lines if x]

for name in ["ece","qcf","sce","ie","or","cse","mse","urban"]:
    L = clean((CACHE / f"gt2-{name}.html").read_text(encoding="utf-8", errors="ignore"))
    try:
        s = L.index("Program Information")
    except ValueError:
        s = 0
    e = next((i for i in range(s, len(L)) if L[i].startswith("Georgia Tech Resources")), len(L))
    body = L[s:e]
    (BASE / f"_r344_txt_{name}.txt").write_text("\n".join(body), encoding="utf-8")
    print(name, "lines", len(body), "chars", len("\n".join(body)))
