# -*- coding: utf-8 -*-
import io, re, shutil, os
P = r"D:\work buddy\2026-11-05-14-15-52\index_main.html"
BAK = P + ".bak_r368b"
if not os.path.exists(BAK):
    shutil.copy2(P, BAK)
html = io.open(P, encoding="utf-8").read()
seg = html[html.index("const PROGRAMS=["): html.index("const CATD=[")]
head = html[: html.index("const PROGRAMS=[")]
tail = html[html.index("const CATD=["):]

UPD = {
    "brown-msds": {
        "ddl": "Fall: intl Feb 1 / final Feb 1 / Fifth-Year May 1; Spring: intl Oct 1 / final Nov 1 / Fifth-Year Nov 1",
        "lang": "TOEFL iBT 105 (exam to 2026-01-20) / 5.5 (exam from 2026-01-21); IELTS 7.5",
        "tui": "2026-27 $10,196/course x 9 courses = $91,764 (standard 2+2+2+2+1 incl summer)",
    },
    "brown-scm-cs": {
        "ddl": "Spring: intl Oct 1 / final Nov 1 / Fifth-Year Nov 1; Fall: intl Jan 15 / final Jan 15 / Fifth-Year May 1",
        "lang": "TOEFL iBT 105 (exam to 2026-01-20) / 5.0 (exam from 2026-01-21, 5.5+ strongly recommended); IELTS 8.0",
        "test": "GRE not required (score submission encouraged); 8 courses incl 2 research or 2 grad CS courses",
        "tui": "2026-27 $10,977/course x 8 courses = $87,816; no TA/GA appointments for master's students",
    },
    "brown-scm-biostat": {
        "ddl": "Fall only: priority Dec 1 / final Jan 15 (overview card lists Nov 1) / Fifth-Year Apr 15",
        "lang": "TOEFL iBT 100 (exam to 2026-01-20) / 5.0 (exam from 2026-01-21); IELTS 7.0",
        "tui": "2026-27 $8,746/course: A.M. 8 courses = $69,968; Sc.M. 10 courses = $87,460",
    },
}
parts = seg.split('{id:"')
out = [parts[0]]
done = []
for b in parts[1:]:
    rid = b.split('"', 1)[0]
    if rid in UPD:
        for k, v in UPD[rid].items():
            if k == "test":
                pat = re.compile(r'(?<![A-Za-z])test:\{cls:"(?:[^"\\]|\\.)*"\}')
                rep = 'test:{cls:"' + v.replace('"', '') + '"}'
            else:
                pat = re.compile(r'(?<![A-Za-z])' + k + r':"(?:[^"\\]|\\.)*"')
                rep = k + ':"' + v.replace('\\', '').replace('"', '') + '"'
            n = len(pat.findall(b))
            assert n == 1, "%s.%s 匹配 %d 次" % (rid, k, n)
            b = pat.sub(rep, b, count=1)
        done.append(rid)
    out.append(b)
new_html = head + '{id:"'.join(out) + tail
io.open(P, "w", encoding="utf-8", newline="").write(new_html)
print("回填:", done, "字节", len(html), "->", len(new_html))
