# -*- coding: utf-8 -*-
import io, re, shutil

html = io.open("index_main.html", encoding="utf-8", newline="").read()
shutil.copyfile("index_main.html", "index_main.html.bak_r358")

EDITS = {
    "vandy-msmkt": [
        ("lang", "TOEFL/IELTS/Duolingo/TOEFL Essentials；项目级最低分待补",
         "TOEFL/IELTS/Duolingo/TOEFL Essentials；官网未列最低分（可按英语官方语言国家学位/近两年工作/GMAT·GRE verbal 90 百分位豁免）"),
        ("tui", "2026-27 学费 $76,700；保险 $4,244(可豁免)+书本 $500+杂费",
         "2026-27 学费 $76,700 + 项目费 $4,500；保险 $4,244(可豁免)+健康 $1,035+服务 $812+成绩单 $100"),
    ],
    "ucsd-cse-ms": [
        ("ddl", "December (Fall 2027, exact date 待补)",
         "2026-12-16 23:59 PST（2026-09-02 开放，仅秋季入学）"),
        ("lang", "TOEFL iBT / IELTS (UCSD Grad Division standard)",
         "TOEFL iBT / IELTS / PTE Academic（最低分按 Graduate Division，项目页未列）"),
    ],
    "wustl-mems-aero": [
        ("dur", "30 units（论文 / 课程两选项）",
         "30 credit hours（论文 / 项目 / 课程三路径）"),
    ],
}

for pid, edits in EDITS.items():
    i = html.index('{id:"%s"' % pid)
    j = html.index('}', html.index('url:', i)) + 1
    blk = html[i:j]
    new = blk
    for key, old, rep in edits:
        pat = r'(?<![A-Za-z])' + key + r':"' + re.escape(old) + r'"'
        cnt = len(re.findall(pat, new))
        assert cnt == 1, (pid, key, cnt)
        new = re.sub(pat, lambda m: key + ':"' + rep + '"', new, count=1)
    html = html[:i] + new + html[j:]

# ucsd test.cls 长串 -> notreq
i = html.index('{id:"ucsd-cse-ms"')
j = html.index('}', html.index('url:', i)) + 1
blk = html[i:j]
m = re.search(r'test:\{cls:"([^"]*)"\}', blk)
assert m and m.group(1).startswith("GRE not required")
new = blk.replace(m.group(0), 'test:{cls:"notreq"}', 1)
html = html[:i] + new + html[j:]

io.open("index_main.html", "w", encoding="utf-8", newline="").write(html)
print("written", len(html))
