# -*- coding: utf-8 -*-
import sys, io, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = r"D:\work buddy\2026-11-05-14-15-52"
for name in ["r347-emory-ms-req", "r347-rice-mcs", "r347-rice-mds", "r347-nd-mscse"]:
    t = open(BASE + "\\cache\\" + name + ".html", encoding="utf-8", errors="replace").read()
    t = re.sub(r"<script.*?</script>|<style.*?</style>|<!--.*?-->", " ", t, flags=re.S)
    txt = re.sub(r"<br[^>]*>|</p>|</li>|</h\d>|</div>|</td>|</tr>", "\n", t)
    txt = re.sub(r"<[^>]+>", " ", txt)
    txt = txt.replace("&nbsp;", " ").replace("&#160;", " ").replace("&amp;", "&").replace("&#8217;", "'").replace("&#8220;", '"').replace("&#8221;", '"').replace("&#8211;", "-")
    txt = re.sub(r"[ \t]{2,}", " ", txt)
    lines = [l.strip() for l in txt.split("\n") if len(l.strip()) > 1]
    out = "\n".join(lines)
    open(BASE + "\\_r347-new-" + name + ".txt", "w", encoding="utf-8").write(out)
    print(name, len(out))
