# -*- coding: utf-8 -*-
import pathlib, re, sys, io, html as H
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 text(p):
    raw = (CACHE / p).read_text(encoding="utf-8", errors="replace")
    raw = re.sub(r"(?is)<(script|style|nav|footer|header)[^>]*>.*?</\1>", " ", raw)
    raw = re.sub(r"(?is)<br\s*/?>", "\n", raw)
    raw = re.sub(r"(?is)</(p|div|li|h[1-6]|tr|td|th)>", "\n", raw)
    raw = re.sub(r"(?is)<[^>]+>", " ", raw)
    raw = H.unescape(raw)
    raw = re.sub(r"[ \t\xa0]+", " ", raw)
    raw = re.sub(r"\n\s*\n+", "\n", raw)
    return raw.strip()
for k in ["nyu-ece", "duke-ece", "nu-cs", "cornell-cs", "mcg-mma", "nyu-appstat", "cornell-mae"]:
    t = text(f"r345-{k}.html")
    (BASE / f"_r345_txt_{k}.txt").write_text(t, encoding="utf-8")
    print(k, len(t))
