# -*- coding: utf-8 -*-
"""Round 302: Finish Stanford Biosciences 4 remaining + UChicago CMB PhD."""
import re, os

ROOT = r"D:\work buddy\2026-11-05-14-15-52"
IDX = os.path.join(ROOT, "index_main.html")

PAGES = [
    {
        "id": "stanford-biomed-info-phd",
        "s": "stanford",
        "n": "生物医学信息学博士",
        "en": "Biomedical Informatics PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求也不审核（即使提交也不显示给审核委员会）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $45,600/年，2025-26）+ 医保",
        "url": "details/stanford-biomed-info-phd.html",
        "school": "Stanford University",
        "dept": "Department of Biomedical Data Science / Biomedical Informatics Program",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://biomedinformatics.stanford.edu/",
        "overview": "斯坦福生物医学信息学博士项目（Biomedical Informatics PhD）是 14 个 Stanford Biosciences 博士项目之一，强调计算、数据科学与医学交叉，与 Biomedical Data Science PhD 同属 Department of Biomedical Data Science 但两个独立项目。",
        "requirements": "要求计算机、统计、数学或生物医学本科背景；编程、算法、统计、线性代数先修优先；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：机器学习与医学 AI；电子病历（EHR）数据挖掘；临床自然语言处理；基因组数据整合；精准医学；医学影像分析；健康数据科学；因果推断；可解释 AI；生物医学知识图谱。",
    },
    {
        "id": "stanford-molpharm-phd",
        "s": "stanford",
        "n": "分子药理学博士",
        "en": "Molecular Pharmacology PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求也不审核（即使提交也不显示给审核委员会）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $45,600/年，2025-26）+ 医保",
        "url": "details/stanford-molpharm-phd.html",
        "school": "Stanford University",
        "dept": "Department of Molecular Pharmacology",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://med.stanford.edu/molpharm.html",
        "overview": "斯坦福分子药理学系（Department of Molecular Pharmacology）研究药物作用机制、药物发现、化学生物学与结构药理学。博士项目是 14 个 Stanford Biosciences 博士项目之一，强调化学、生物与物理交叉。",
        "requirements": "要求化学、生物化学、药理学或生物科学本科背景；有机化学、生物化学、物理、微积分先修；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：药物发现与开发；化学生物学；GPCR 与离子通道药理学；结构药理学；药物代谢与药代动力学；癌症药理学；神经药理学；药物递送；CRISPR 药物筛选；计算化学与药物设计。",
    },
    {
        "id": "stanford-neuroscience-phd",
        "s": "stanford",
        "n": "神经科学博士",
        "en": "Neuroscience PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求也不审核（即使提交也不显示给审核委员会）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $45,600/年，2025-26）+ 医保",
        "url": "details/stanford-neuroscience-phd.html",
        "school": "Stanford University",
        "dept": " Neurosciences Program",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://neuroscience.stanford.edu/",
        "overview": "斯坦福神经科学博士项目（Neuroscience PhD）是 14 个 Stanford Biosciences 博士项目之一，跨系招生，与 Wu Tsai Neurosciences Institute 紧密合作，研究从分子、细胞、环路到系统与认知神经科学全层级。",
        "requirements": "要求生物、心理、神经科学、工程或物理本科背景；生物化学、细胞生物学、生理学、物理、微积分先修；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：分子与细胞神经科学；突触传递；神经环路；系统与计算神经科学；认知神经科学；神经发育；神经退行性疾病（阿尔茨海默、帕金森）；神经修复；光遗传学；神经影像学；脑机接口。",
    },
    {
        "id": "stanford-scb-phd",
        "s": "stanford",
        "n": "干细胞生物学与再生医学博士",
        "en": "Stem Cell Biology and Regenerative Medicine PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求也不审核（即使提交也不显示给审核委员会）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $45,600/年，2025-26）+ 医保",
        "url": "details/stanford-scb-phd.html",
        "school": "Stanford University",
        "dept": "Institute for Stem Cell Biology and Regenerative Medicine (ISCBRM)",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://iscb.stanford.edu/",
        "overview": "斯坦福干细胞生物学与再生医学博士项目（Stem Cell Biology and Regenerative Medicine PhD）是 14 个 Stanford Biosciences 博士项目之一，与 Stanford Institute for Stem Cell Biology and Regenerative Medicine 合作，研究干细胞生物学、组织工程与再生疗法。",
        "requirements": "要求生物、化学、工程或医学本科背景；生物化学、分子生物学、细胞生物学先修；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：胚胎与成体干细胞；iPSC 技术；细胞重编程；组织工程与器官芯片；再生医学；基因编辑；干细胞与癌症；发育生物学；疾病模型；细胞治疗；3D 生物打印。",
    },
    {
        "id": "uchicago-cmb-phd",
        "s": "uchicago",
        "n": "细胞与分子生物学博士",
        "en": "Cell and Molecular Biology PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 90+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（BSD 统一政策，5 年内有效分数可选提交）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $40,300/年，2025-26）+ 医保",
        "url": "details/uchicago-cmb-phd.html",
        "school": "University of Chicago",
        "dept": "Department of Molecular Biosciences",
        "college": "Division of the Biological Sciences (BSD)",
        "website": "https://molbiosci.uchicago.edu/academics/phd-programs/cell-and-molecular-biology",
        "overview": "芝加哥大学细胞与分子生物学博士项目（Cell and Molecular Biology PhD）属于 Molecular Biosciences cluster，是 BSD 统一招生博士项目之一。研究细胞结构、信号转导、基因表达与分子机制。",
        "requirements": "要求生物、化学或相关本科背景；生物化学、细胞生物学、遗传学、物理、微积分先修；3 封推荐信；SOP；CV；Research Experience。通过 UChicago BSD 统一申请。",
        "research": "研究方向包括：细胞周期与分裂；细胞信号转导；基因表达调控；染色质生物学；细胞骨架；膜运输；蛋白质折叠与降解；细胞死亡；微生物组；结构生物学；单细胞生物学。",
    },
]

TEMPLATE = """<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>{n} - {en} | {school}</title>
<style>
body{{font-family:-apple-system,"Segoe UI",sans-serif;max-width:980px;margin:24px auto;padding:0 20px;color:#222;line-height:1.65}}
h1{{color:#8C1515;border-bottom:3px solid #8C1515;padding-bottom:8px}}
h2{{color:#2e0e0e;margin-top:28px;border-left:4px solid #8C1515;padding-left:10px}}
.cards{{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:14px;margin:18px 0}}
.card{{background:#faf7f3;border:1px solid #e6d9c8;border-radius:8px;padding:14px 16px}}
.card h3{{margin:0 0 8px;color:#8C1515;font-size:15px}}
.card .v{{font-size:14px}}
a{{color:#0066cc}}
</style>
</head>
<body>
<h1>{school} · {n}</h1>
<p><b>英文名：</b>{en}　|　<b>学位：</b>{deg}　|　<b>学院：</b>{college}　|　<b>系所：</b>{dept}</p>
<p><b>官网：</b><a href="{website}" target="_blank">{website}</a></p>

<div class="cards">
<div class="card"><h3>申请截止</h3><div class="v">{ddl}（2026-2027 入学季）</div></div>
<div class="card"><h3>语言要求</h3><div class="v">{lang}</div></div>
<div class="card"><h3>标化政策</h3><div class="v">{test_cls}</div></div>
<div class="card"><h3>学费与资助</h3><div class="v">{tui}</div></div>
<div class="card"><h3>学制</h3><div class="v">{dur}</div></div>
<div class="card"><h3>STEM</h3><div class="v">{stem_label}</div></div>
</div>

<h2>项目概览</h2>
<p>{overview}</p>

<h2>申请要求</h2>
<p>{requirements}</p>

<h2>研究方向</h2>
<p>{research}</p>

<p><i>数据来源：{school} 官方项目页 / Graduate Admissions；2026-12-01 核验。录取分数分布仅作参考，非最低门槛。</i></p>
</body>
</html>
"""

def render_page(p):
    stem_label = "STEM-designated，符合 36 个月 OPT 延期资格" if p["stem"] == 1 else "非 STEM"
    return TEMPLATE.format(
        n=p["n"], en=p["en"], school=p["school"], deg=p["deg"],
        college=p["college"], dept=p["dept"].strip(), website=p["website"],
        ddl=p["ddl"], lang=p["lang"], test_cls=p["test_cls"],
        tui=p["tui"], dur=p["dur"], stem_label=stem_label,
        overview=p["overview"], requirements=p["requirements"], research=p["research"],
    )

def main():
    with open(IDX, "r", encoding="utf-8") as f:
        html = f.read()
    before = len(html.encode("utf-8"))

    new_prog = []
    for p in PAGES:
        out = os.path.join(ROOT, p["url"].replace("/", os.sep))
        os.makedirs(os.path.dirname(out), exist_ok=True)
        with open(out, "w", encoding="utf-8") as f:
            f.write(render_page(p))
        print(f"Written: {p['url']}")
        new_prog.append(
            '{{id:"{id}",s:"{s}",n:"{n}",en:"{en}",deg:"{deg}",lvl:"{lvl}",stem:{stem},dur:"{dur}",ddl:"{ddl}",lang:"{lang}",test:{{cls:"{test_cls}"}},tui:"{tui}",url:"{url}"}}'.format(
                id=p["id"], s=p["s"], n=p["n"], en=p["en"], deg=p["deg"],
                lvl=p["lvl"], stem=p["stem"], dur=p["dur"], ddl=p["ddl"],
                lang=p["lang"], test_cls=p["test_cls"], tui=p["tui"], url=p["url"],
            )
        )

    m = re.search(r"const PROGRAMS\s*=\s*\[", html)
    if not m:
        raise RuntimeError("PROGRAMS not found")
    depth = 1
    i = m.end()
    while i < len(html) and depth > 0:
        c = html[i]
        if c == "[": depth += 1
        elif c == "]": depth -= 1
        i += 1
    prog_end = i - 1
    insert_block = "," + ",".join(new_prog) + "\n"
    html = html[:prog_end] + insert_block + html[prog_end:]

    m2 = re.search(r"const VERIFIED_DETAILS\s*=\s*new\s*Set\(\[", html)
    if not m2:
        raise RuntimeError("VD not found")
    depth = 1
    j = m2.end()
    while j < len(html) and depth > 0:
        c = html[j]
        if c == "[": depth += 1
        elif c == "]": depth -= 1
        j += 1
    vd_close_bracket = j - 1
    vd_new_urls = ",".join(f'"{p["url"]}"' for p in PAGES)
    html = html[:vd_close_bracket] + "," + vd_new_urls + html[vd_close_bracket:]

    after = len(html.encode("utf-8"))
    with open(IDX, "w", encoding="utf-8") as f:
        f.write(html)
    print(f"Before size: {before} bytes")
    print(f"After size: {after} bytes")
    print(f"Delta: +{after-before} bytes")
    print(f"PROGRAMS appended: {len(PAGES)} entries")
    print(f"VD appended: {len(PAGES)} urls")
    print("Done!")

if __name__ == "__main__":
    main()
