# -*- coding: utf-8 -*-
"""Round 301: Stanford Biosciences 5 more PhD programs."""
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-devbio-phd",
        "s": "stanford",
        "n": "发育生物学博士",
        "en": "Developmental Biology 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-devbio-phd.html",
        "school": "Stanford University",
        "dept": "Department of Developmental Biology",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://med.stanford.edu/devbio.html",
        "overview": "斯坦福发育生物学系（Department of Developmental Biology）研究胚胎发育、器官形成、干细胞分化与再生的分子机制。博士项目是 14 个 Stanford Biosciences 博士项目之一，跨系招生，第一年课程 + rotation，年末定导师。",
        "requirements": "要求相关生物科学本科背景；生物化学、分子生物学、遗传学、细胞生物学先修课程；3 封推荐信；Statement of Purpose；CV；可选 Research Experience。申请通过 Stanford Biosciences 统一申请系统。",
        "research": "研究方向包括：模式动物（果蝇、斑马鱼、小鼠）发育遗传；器官发生与形态发生；干细胞生物学；再生医学；肿瘤发育；单细胞与空间转录组；发育神经生物学；计算发育生物学。",
    },
    {
        "id": "stanford-genetics-phd",
        "s": "stanford",
        "n": "遗传学博士",
        "en": "Genetics 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-genetics-phd.html",
        "school": "Stanford University",
        "dept": "Department of Genetics",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://med.stanford.edu/genetics.html",
        "overview": "斯坦福遗传学系（Department of Genetics）是人类遗传学、基因组学、基因组医学与计算生物学的顶尖研究机构。博士项目是 14 个 Stanford Biosciences 博士项目之一，强调结合湿实验与计算方法研究基因组结构与功能。",
        "requirements": "要求相关生物科学或计算/数学本科背景；生物化学、分子生物学、统计学、编程先修优先；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：人类遗传学与基因组医学；统计遗传学；单细胞基因组学；表观遗传学；基因组学与 AI；演化与群体遗传学；微生物组；结构变异；CRISPR 基因组编辑；遗传疾病机制。",
    },
    {
        "id": "stanford-immunology-phd",
        "s": "stanford",
        "n": "免疫学博士",
        "en": "Immunology 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-immunology-phd.html",
        "school": "Stanford University",
        "dept": "Department of Immunology",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://immunity.stanford.edu/",
        "overview": "斯坦福免疫学系（Department of Immunology）研究免疫系统发育、免疫识别、自身免疫、感染免疫与肿瘤免疫。博士项目是 14 个 Stanford Biosciences 博士项目之一，跨系招生，与 Institute for Immunity, Transplantation and Infection (ITI) 紧密合作。",
        "requirements": "要求相关生物科学或化学本科背景；生物化学、分子生物学、细胞生物学、微生物学先修；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：T/B 细胞发育与分化；先天免疫；自身免疫；肿瘤免疫与免疫治疗；移植免疫；感染免疫；黏膜免疫；疫苗开发；单细胞免疫组库；免疫代谢；CRISPR 免疫筛选。",
    },
    {
        "id": "stanford-microbiology-phd",
        "s": "stanford",
        "n": "微生物学与免疫学博士",
        "en": "Microbiology and Immunology 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-microbiology-phd.html",
        "school": "Stanford University",
        "dept": "Department of Microbiology and Immunology",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://med.stanford.edu/microimmun.html",
        "overview": "斯坦福微生物与免疫学系（Department of Microbiology and Immunology）研究病毒、细菌、真菌、寄生虫的致病机制与宿主反应。博士项目是 14 个 Stanford Biosciences 博士项目之一，与 Stanford HIV-1 研究所、感染控制网络紧密合作。",
        "requirements": "要求相关生物科学或化学本科背景；微生物学、免疫学、生物化学、分子生物学先修；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "research": "研究方向包括：病毒学（HIV、流感、冠状病毒、疱疹病毒）；细菌致病机制；先天免疫；宿主-病原体互作；微生物组；结构病毒学；抗病毒药物；疫苗开发；感染与癌症；跨物种传播。",
    },
    {
        "id": "stanford-mcp-phd",
        "s": "stanford",
        "n": "分子与细胞生理学博士",
        "en": "Molecular and Cellular Physiology 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-mcp-phd.html",
        "school": "Stanford University",
        "dept": "Department of Molecular and Cellular Physiology",
        "college": "School of Medicine / Stanford Biosciences",
        "website": "https://med.stanford.edu/mcp.html",
        "overview": "斯坦福分子与细胞生理学系（Department of Molecular and Cellular Physiology）研究细胞功能、信号转导、膜蛋白、神经与内分泌生理。博士项目是 14 个 Stanford Biosciences 博士项目之一，强调分子生物物理学、电生理与成像技术。",
        "requirements": "要求相关生物科学、化学、物理或工程本科背景；生物化学、细胞生物学、生理学、物理、微积分先修；3 封推荐信；SOP；CV；Research Experience。通过 Stanford Biosciences 统一申请。",
        "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-11-30 核验。录取分数分布仅作参考，非最低门槛。</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"], 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"],
            )
        )

    # Locate PROGRAMS array end
    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  # position of closing ]
    # Insert before closing ]
    insert_block = "," + ",".join(new_prog) + "\n"
    html = html[:prog_end] + insert_block + html[prog_end:]

    # Locate VERIFIED_DETAILS Set(...) end in new html
    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  # position of ]
    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()
