# -*- coding: utf-8 -*-
"""Round 247: batch add 5 Materials Science and Engineering PhD (fifth batch: Canada completion + U.S.补漏)
- UBC MSE PhD (Feb 1; GRE not required; Sept 2027 intake opens Oct 15, 2026; transcript/referee Feb 15)
- McGill MSE PhD (Jan 15; GRE not required; supervisor must secure; GPA 3.2/4.0 masters; MEDA $24k domestic / $37k international)
- Waterloo MME PhD - Materials Engineering and Processing field (Feb 1 for Sept; thesis-based MASc 80%+; 48 months; Nanotechnology also available)
- UCLA MSE PhD (Dec 9, 2026 11:59pm PST; GRE optional but highly encouraged; GPA 3.25/4.0; 3 LOR; Fall only)
- JHU MSE PhD (Dec 15, 2026; GRE not required; 2 LOR; TOEFL C559; INBT/HEMI cross-disciplinary centers; VTSI fellowship)
"""
import os, re

BASE = os.path.dirname(os.path.abspath(__file__))
DET = os.path.join(BASE, "details")
os.makedirs(DET, exist_ok=True)

PROGRAMS = [
    {
        "id": "ubc-mse-phd",
        "s": "ubc",
        "n": "材料工程博士",
        "en": "PhD in Materials Engineering (University of British Columbia, Faculty of Applied Science, Department of Materials Engineering; part of the 5 Department of Materials Engineering alongside Mining and Materials Engineering; research across minerals processing, advanced materials, nanomaterials, biomaterials, recycling; Sept/Jan/May intakes; requires prior research master's and supervisor agreement per Canadian graduate model)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "Typically 4-5 years; coursework + candidacy exam + thesis; on-campus residential; supervisor must secure funding before offer",
        "ddl": "Fall 2027 (September): application opens October 15, 2026. **Application deadline February 1, 2027** (Canadian and International applicants). Transcript and referee deadline February 15, 2027. January 2028 intake: deadline August 1, 2027. May 2028 intake: opens August 1, 2027.",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo required for non-native English speakers (UBC Graduate School minimums); waivers for English-instructed degree in native-English country",
        "test": {"cls": "GRE NOT REQUIRED (UBC Graduate School degree listing: 'The GRE is not required.' for PhD in Materials Engineering. Canadian graduate model: applicants must identify and secure a supervisor prior to application; no offer without supervisor support. 2-3 academic references; SOP; CV; transcripts.)"},
        "tui": "PhD candidates typically receive funding through supervisor research grants, scholarships (NSERC, FRQNT, UBC Fellowships), and Teaching Assistantships. Minimum support package per UBC Graduate Studies for Engineering PhD.",
        "url": "details/ubc-mse-phd.html",
    },
    {
        "id": "mcg-mse-phd",
        "s": "mcg",
        "n": "材料工程博士",
        "en": "PhD in Materials Engineering (McGill University, Faculty of Engineering, Department of Mining and Materials Engineering; research across mineral processing and extraction, advanced materials including nano- and bio-materials, materials recycling; Fall/Winter/Summer intakes; supervisor must be found by applicant; 2 lecture courses + safety training + research seminar + preliminary exam within first year)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "Typically 4-5 years; 2 lecture courses assigned by department; safety training course; Research Seminar participation; preliminary exam within first year; thesis; oral defense; on-campus residential (Montreal, Quebec)",
        "ddl": "Fall 2027 (September): applications open September 15, 2026. **International deadline January 15, 2027** (Canadian domestic also January 15). Winter 2028: International August 15 / Domestic September 1, 2027. Summer 2027: January 15, 2027. Note: deadlines subject to change; verify in application portal.",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo required for non-native English speakers (McGill Graduate and Postdoctoral Studies minimums); waivers for English-instructed degree in English-speaking country",
        "test": {"cls": "GRE NOT A REQUIREMENT (McGill Mining and Materials Engineering: 'The GRE is NOT a requirement for your application.' Minimum GPA 3.2/4.0 in Masters program and 3.0/4.0 in undergraduate program; applicants should hold a Masters degree in relevant field except highly-qualified B.Sc./B.Eng. holders. 2-3 references; SOP; CV; transcripts; applicant must find supervisor before application.)"},
        "tui": "Over 70% of graduate students funded through research stipends/scholarships. PhD stipend ~$18,000 per annum (NSERC approved rates). MEDA (McGill Engineering Doctoral Award): $24,000/year over 3 years for domestic students, $37,000/year over 3 years for international students (auto-consideration by Feb 1 for May/September admission).",
        "url": "details/mcg-mse-phd.html",
    },
    {
        "id": "uw-mme-phd-mse",
        "s": "uw",
        "n": "机械与机电工程博士-材料工程与加工方向",
        "en": "PhD in Mechanical and Mechatronics Engineering - Materials Engineering and Processing research field (University of Waterloo, Faculty of Engineering, Department of Mechanical and Mechatronics Engineering, MME; one of five research fields alongside Automation and Controls, Fluid Mechanics, Solid-Body Mechanics and Mechanical Design, Thermal Engineering; also a separate Nanotechnology PhD program; 48 months full-time thesis-based; requires thesis-based Master's with minimum 80% standing)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "48 months (4 years) full-time; thesis-based; on-campus (Waterloo, Ontario); also part-time registration option; comprehensive exam + thesis; Nanotechnology PhD option with NANO 601-606 core courses",
        "ddl": "Fall (September) admission: **application deadline February 1, 2027**. Winter (January) admission: June 1, 2027. Spring (May) admission: October 1, 2027. Completed applications with all supporting documentation must be submitted on or before the posted deadline.",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo required for non-native English speakers (University of Waterloo graduate minimums); waivers for English-instructed degree",
        "test": {"cls": "GRE not listed as requirement (University of Waterloo MME PhD program listing does not require GRE; admission based on thesis-based Master's degree with minimum 80% standing and demonstrated research capabilities. Approved supervisor must agree in writing before offer. SOP; CV; transcripts; 3 references.)"},
        "tui": "PhD students typically receive funding through supervisor research grants, OGS, NSERC, and Teaching Assistantships. Waterloo Engineering minimum funding package for qualified doctoral students.",
        "url": "details/uw-mme-phd-mse.html",
    },
    {
        "id": "ucla-mse-phd",
        "s": "ucla",
        "n": "材料科学与工程博士",
        "en": "PhD in Materials Science and Engineering (University of California Los Angeles, Henry Samueli School of Engineering and Applied Science, Department of Materials Science and Engineering; research across ceramics and ceramic processing, computational materials science, electronic and optical materials, soft materials, structural materials; Fall admission only; MS normally required for PhD but exceptional BS direct admission possible)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "Typically 5-6 years; coursework + candidacy exam + dissertation; on-campus residential (Los Angeles, CA); Fall admission only",
        "ddl": "Fall 2027 admission: application opens mid-September 2026. **PhD deadline December 9, 2026, 11:59 pm Pacific Time (PST)**. Admission limited to Fall term only. Decisions typically by February-March; response by April 15.",
        "lang": "TOEFL iBT minimum total 87 (UCLA Graduate Division; MSE allows one subscore below minimum if total at least 87); IELTS Academic minimum; ETS UCLA 4837; waivers per UCLA international policy",
        "test": {"cls": "GRE OPTIONAL (UCLA MSE: 'Students who wish to apply for admission to our graduate program are not required to take the GRE or submit a GRE score report as part of their application package, however, it is highly encouraged for PhD applicants. The applications with GRE scores will not be given greater weight than those that do not include scores.' ETS institution 4837 / department 1403. 3 LOR; SOP; CV; transcripts; bachelor's GPA 3.25/4.0 for PhD.)"},
        "tui": "PhD students typically receive full funding through research assistantships, teaching assistantships, and fellowships (UCLA Engineering minimum package). Out-of-state tuition waived for supported PhD students.",
        "url": "details/ucla-mse-phd.html",
    },
    {
        "id": "jhu-mse-phd",
        "s": "jhu",
        "n": "材料科学与工程博士",
        "en": "PhD in Materials Science and Engineering (Johns Hopkins University, Whiting School of Engineering, Department of Materials Science and Engineering; cross-disciplinary research through Institute for NanoBioTechnology (INBT), Hopkins Extreme Materials Institute (HEMI), and School of Medicine; biomaterials and nanotechnology focus; 4 required courses + comprehensive oral exam + dissertation proposal + public defense; VTSI fellowship for STEM PhD)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "Typically 5-6 years; 4 required MSE courses; Whiting School coursework; comprehensive oral exam; dissertation proposal; original research documented in dissertation defended publicly; on-campus residential (Baltimore, MD)",
        "ddl": "Fall 2027 admission: application period opens around August 15, 2026. **PhD deadline December 15, 2026** (11:59 pm Eastern Time). Spring 2028 admission: October 15, 2027. MSE (master's) deadline March 15, 2027. BS/MSE combined deadline February 15, 2027.",
        "lang": "TOEFL minimum C559 paper-based (Whiting School); TOEFL iBT / IELTS Academic / Duolingo also accepted; required for international students unless native English speaker or degree from English-speaking country; all documents in English",
        "test": {"cls": "GRE NOT REQUIRED (Whiting School of Engineering: 'The Whiting School of Engineering does not require GRE General Test scores for applications to our graduate programs when applying to spring, summer, or fall 2026 start terms.' Institution code 4655 if self-reported. 2 LOR; SOP; unofficial transcripts; official upon admission. No application fee for master's applicants.)"},
        "tui": "PhD students funded by department as TA or RA typically receive full tuition, stipend, and health insurance. Vivien Thomas Scholars Initiative (VTSI) provides full tuition, stipend, and benefits for PhD STEM students from eligible institutions (HBCUs, community colleges, regional Maryland/Virginia/DC institutions).",
        "url": "details/jhu-mse-phd.html",
    },
]

DETAIL_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{school} - {en}</title>
<style>
body{{font-family:-apple-system,'Segoe UI',Roboto,sans-serif;max-width:960px;margin:24px auto;padding:0 20px;color:#222;line-height:1.6}}
h1{{font-size:24px;border-bottom:2px solid #1a73e8;padding-bottom:8px}}
h2{{font-size:18px;color:#1a73e8;margin-top:24px}}
.card{{background:#f8f9fa;border-left:4px solid #1a73e8;padding:12px 16px;margin:12px 0;border-radius:4px}}
.meta{{display:grid;grid-template-columns:120px 1fr;gap:6px 12px;font-size:14px}}
.meta b{{color:#555}}
ul{{margin:6px 0;padding-left:20px}}
.note{{background:#fff3cd;border-left-color:#ffc107;padding:10px 14px;margin-top:16px;font-size:13px}}
a{{color:#1a73e8}}
</style>
</head>
<body>
<h1>{school} - {en}</h1>
<div class="card">
<div class="meta">
<b>Degree</b><span>{deg}</span>
<b>Level</b><span>Research Doctorate (PhD)</span>
<b>STEM</b><span>{stem_label}</span>
<b>Duration</b><span>{dur}</span>
<b>Deadline</b><span>{ddl}</span>
<b>Language</b><span>{lang}</span>
<b>Standardized</b><span>{test_cls}</span>
<b>Funding</b><span>{tui}</span>
</div>
</div>

<h2>Program Overview</h2>
<div class="card">
{overview}
</div>

<h2>Admissions Requirements</h2>
<div class="card">
{requirements}
</div>

<h2>Research Areas</h2>
<div class="card">
{research}
</div>

<h2>Official Source</h2>
<div class="card">
{source}
</div>

<div class="note">
<strong>Note:</strong> Fifth batch of Materials Science and Engineering PhD sweep (UBC, McGill, Waterloo MME-MSE, UCLA, JHU). Completes Canadian 4-school MSE coverage (UofT round 246 + UBC/McGill/Waterloo this round). U.S. schools added: UCLA and JHU. MSE PhD sweep now covers 25 programs (21 U.S. + 4 Canadian). GRE policy: all 5 programs do not require GRE. Deadlines: Dec 9 (UCLA PST), Dec 15 (JHU ET), Jan 15 (McGill), Feb 1 (UBC and Waterloo). Key Canadian-model differences: UBC/McGill/Waterloo require applicant to secure supervisor before offer; UBC and McGill require prior research master's; Waterloo requires thesis-based MASc with 80%+ standing. Note: Waterloo does not have an independent Materials Engineering department; MSE is one of five research fields within MME, with a separate Nanotechnology PhD program also available. Georgetown has no standalone MSE PhD (only Physics department has materials faculty) - skipped. Dartmouth offers Engineering Sciences PhD with MSE concentration (not standalone MSE degree) - will revisit in a later sweep.
</div>
</body>
</html>
"""

PAGES = {
    "ubc-mse-phd": {
        "school": "University of British Columbia, Faculty of Applied Science, Department of Materials Engineering",
        "en": "PhD in Materials Engineering",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "STEM-designated (Engineering / CIP 14.1801)",
        "dur": "Typically 4-5 years; coursework + candidacy exam + thesis; on-campus residential; supervisor must secure funding before offer",
        "ddl": "Fall 2027 (September): opens October 15, 2026. **Deadline February 1, 2027** (Canadian and International). Transcript/referee February 15, 2027. January 2028: August 1, 2027. May 2028: opens August 1, 2027.",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo required for non-native English speakers (UBC Graduate School minimums); waivers for English-instructed degree",
        "test_cls": "GRE NOT REQUIRED (UBC Graduate School: 'The GRE is not required.' for PhD in Materials Engineering. Canadian model: secure supervisor before application.)",
        "tui": "PhD funding through supervisor research grants, scholarships (NSERC, UBC Fellowships), Teaching Assistantships. UBC Graduate Studies minimum funding package for Engineering PhD.",
        "overview": "The University of British Columbia's Department of Materials Engineering (Faculty of Applied Science) offers MASc and PhD degrees in Materials Engineering. The PhD program is research-intensive, typically completed in 4-5 years. The application window for September 2027 intake opens October 15, 2026. The application deadline is February 1, 2027 for both Canadian and international applicants; transcripts and referee submissions follow by February 15, 2027. January 2028 intake deadline is August 1, 2027. GRE is not required. Consistent with the Canadian graduate model, applicants are expected to identify and secure a supervisor prior to submitting their application; no offer is extended without supervisor support and funding. Research areas span minerals processing and extraction, advanced materials including nanomaterials and biomaterials, and materials recycling and sustainability.",
        "requirements": "<ul><li>Bachelor's or Master's degree in materials engineering, metallurgy, mining, chemistry, or related engineering/science field</li><li>Research master's preferred (Canadian graduate model)</li><li>GRE NOT REQUIRED (UBC Graduate School degree listing)</li><li>TOEFL iBT / IELTS / Duolingo for non-native English speakers</li><li>UBC Graduate and Postdoctoral Studies online application (opens Oct 15, 2026)</li><li>2-3 academic references</li><li>Statement of purpose</li><li>CV/resume</li><li>Transcripts</li><li>**September 2027 deadline February 1, 2027**</li><li>Transcript/referee deadline February 15, 2027</li><li>January 2028 intake: August 1, 2027</li><li>May 2028 intake: opens August 1, 2027</li><li>Applicants must secure supervisor before offer</li><li>On-campus residential (Vancouver, BC, Canada)</li></ul>",
        "research": "<ul><li>Minerals processing and extraction</li><li>Advanced materials (nanomaterials, biomaterials)</li><li>Materials recycling and sustainability</li><li>Metallurgical engineering</li><li>Materials for energy</li><li>Interdisciplinary collaboration with Mining Engineering, Chemistry, Physics</li></ul>",
        "source": '<a href="https://www.grad.ubc.ca/prospective-students/graduate-degree-programs/phd-materials-engineering" target="_blank">UBC Graduate School: PhD in Materials Engineering (GRE not required; Sept 2027 opens Oct 15 / deadline Feb 1 / transcript/referee Feb 15; Jan 2028 deadline Aug 1; May 2028 opens Aug 1)</a> | <a href="https://www.grad.ubc.ca/prospective-students/application-admission" target="_blank">UBC Graduate School Application and Admission (minimum academic requirements; English language proficiency; program-specific additional tests; deadlines program-specific from November to April)</a>',
    },
    "mcg-mse-phd": {
        "school": "McGill University, Faculty of Engineering, Department of Mining and Materials Engineering",
        "en": "PhD in Materials Engineering",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "STEM-designated (Engineering / CIP 14.1801)",
        "dur": "Typically 4-5 years; 2 lecture courses; safety training; Research Seminar; preliminary exam within first year; thesis; oral defense; on-campus (Montreal, Quebec)",
        "ddl": "Fall 2027: opens September 15, 2026. **International deadline January 15, 2027** (Domestic also January 15). Winter 2028: International August 15 / Domestic September 1, 2027. Summer 2027: January 15, 2027.",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo for non-native English speakers; waivers for English-instructed degree in English-speaking country",
        "test_cls": "GRE NOT A REQUIREMENT (McGill Mining and Materials Engineering: 'The GRE is NOT a requirement for your application.' Minimum GPA 3.2/4.0 Masters / 3.0/4.0 undergrad; Masters degree normally required except highly-qualified B.Sc./B.Eng.)",
        "tui": "Over 70% of graduate students funded. PhD stipend ~$18,000/year (NSERC rates). MEDA: $24,000/yr domestic / $37,000/yr international over 3 years (auto-consideration by Feb 1).",
        "overview": "The PhD in Materials Engineering at McGill University is offered by the Department of Mining and Materials Engineering in the Faculty of Engineering. The program is research-intensive, emphasizing original thinking, research design, and intellectual independence for careers in academia or industry. Candidates must complete a minimum of two lecture courses assigned by the department based on prior training and research interests, pass a safety training course, participate in a Research Seminar, and take a preliminary examination within their first year. The thesis must constitute original scholarship and be defended orally. Minimum GPA is 3.2/4.0 in a Masters program and 3.0/4.0 in the undergraduate program; applicants should normally hold a Masters degree in a relevant field, except in special cases for highly-qualified B.Sc. or B.Eng. holders. GRE is not a requirement. Regardless of program, each applicant must find a supervisor themselves; no student is accepted without a supervisor. Fall applications open September 15; international deadline is January 15. Funding: over 70% of graduate students receive research stipends; PhD students receive ~$18,000/year NSERC rates; MEDA (McGill Engineering Doctoral Award) provides $24,000/year domestic or $37,000/year international over 3 years for applicants by February 1.",
        "requirements": "<ul><li>Bachelor's degree in engineering, materials science, metallurgy, mining, chemistry, or related field (minimum GPA 3.0/4.0 undergrad)</li><li>Masters degree in relevant field preferred (minimum GPA 3.2/4.0); highly-qualified B.Sc./B.Eng. may apply directly in special cases</li><li>GRE NOT A REQUIREMENT</li><li>TOEFL iBT / IELTS / Duolingo for non-native English speakers</li><li>McGill Future Graduate Students online application</li><li>2-3 references (academic)</li><li>Statement of purpose</li><li>CV/resume</li><li>Transcripts</li><li>**Applicant must find and secure supervisor before application** (no offer without supervisor)</li><li>**Fall 2027 international deadline January 15, 2027**</li><li>Winter 2028: International August 15 / Domestic September 1, 2027</li><li>Summer 2027: January 15, 2027</li><li>2 lecture courses + safety training + Research Seminar + preliminary exam within first year</li><li>On-campus residential (Montreal, Quebec, Canada)</li></ul>",
        "research": "<ul><li>Mineral processing and extraction (mining engineering nexus)</li><li>Advanced materials including nano- and bio-materials</li><li>Materials recycling</li><li>Metallurgy</li><li>Materials characterization</li><li>Industrial collaborations with faculty</li></ul>",
        "source": '<a href="https://www.mcgill.ca/gradapplicants/program/materials-engineering-phd" target="_blank">McGill Future Graduate Students: Materials Engineering PhD (Fall opens Sep 15 / Intl deadline Jan 15 / Domestic Jan 15; Winter Intl Aug 15 / Domestic Sep 1; Summer Jan 15; Bachelor\'s minimum 3.0/4.0; English proficiency required)</a> | <a href="https://www.mcgill.ca/materials/current/admissionsprocess" target="_blank">McGill Materials Engineering Admissions Process (must find supervisor; no student accepted without supervisor; PhD minimum GPA 3.2/4.0 Masters / 3.0/4.0 undergrad; GRE NOT a requirement; 2 lecture courses + safety training + research seminar + preliminary exam within first year)</a> | <a href="https://www.mcgill.ca/materials/current/financial" target="_blank">McGill Materials Engineering Financial Information (over 70% funded; PhD $18,000/year NSERC rates; MEDA $24,000/yr domestic / $37,000/yr international over 3 years; auto-consideration by Feb 1 for May/Sept admission)</a>',
    },
    "uw-mme-phd-mse": {
        "school": "University of Waterloo, Faculty of Engineering, Department of Mechanical and Mechatronics Engineering (MME)",
        "en": "PhD in Mechanical and Mechatronics Engineering - Materials Engineering and Processing field",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "STEM-designated (Engineering / CIP 14.1801)",
        "dur": "48 months (4 years) full-time; thesis-based; on-campus; also part-time option; comprehensive exam + thesis; separate Nanotechnology PhD option with NANO 601-606 core courses",
        "ddl": "Fall (September) admission: **February 1, 2027**. Winter (January): June 1, 2027. Spring (May): October 1, 2027. Completed applications with all supporting documentation due on or before deadline.",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo for non-native English speakers (University of Waterloo graduate minimums); waivers for English-instructed degree",
        "test_cls": "GRE not listed as requirement (Waterloo MME PhD program listing). Requires thesis-based Master's with minimum 80% standing and demonstrated research capabilities. Approved supervisor must agree in writing before offer.",
        "tui": "PhD funding through supervisor research grants, OGS, NSERC, and Teaching Assistantships. Waterloo Engineering minimum funding package for qualified doctoral students.",
        "overview": "The University of Waterloo's Department of Mechanical and Mechatronics Engineering (MME) offers a PhD program with five research fields: Automation and Controls, Fluid Mechanics, Materials Engineering and Processing, Solid-Body Mechanics and Mechanical Design, and Thermal Engineering. The Materials Engineering and Processing research group focuses on electronic packaging, functional nanomaterials for sustainability and health, materials interface foundry, NEMS/MEMS, welding and joining, and water treatment. The PhD program is 48 months (4 years) full-time, thesis-based, on-campus, with Fall/Winter/Spring admission terms. A thesis-based Master's degree from a recognized university with minimum 80% standing and demonstrated research capabilities is required. Application deadlines: February 1 for September admission, June 1 for January, October 1 for May. Consistent with Canadian model, an approved supervisor must agree in writing before an offer is extended. Since 2009, MME also offers a separate Nanotechnology graduate program (MASc and PhD) with NANO 601-606 core courses in nanomaterials characterization, nanocomposites, nanomechanics, and MEMS/NEMS design.",
        "requirements": "<ul><li>Thesis-based Master's degree from a recognized university with minimum 80% standing</li><li>Demonstrated research capabilities</li><li>GRE not listed as requirement</li><li>TOEFL iBT / IELTS / Duolingo for non-native English speakers</li><li>Waterloo Graduate Studies and Postdoctoral Affairs online application</li><li>3 references</li><li>Statement of purpose</li><li>CV/resume</li><li>Transcripts</li><li>**Approved supervisor must agree in writing before offer**</li><li>**Fall (September) deadline February 1, 2027**</li><li>Winter (January) deadline June 1, 2027</li><li>Spring (May) deadline October 1, 2027</li><li>48 months (4 years) full-time thesis-based</li><li>On-campus (Waterloo, Ontario, Canada)</li><li>Separate Nanotechnology PhD option available (NANO 601-606 core)</li></ul>",
        "research": "<ul><li>Materials Engineering and Processing (one of 5 MME research fields)</li><li>Electronic packaging</li><li>Functional nanomaterials for sustainability and health</li><li>Materials interface foundry</li><li>NEMS/MEMS and nanojoining</li><li>Welding and joining</li><li>Water treatment materials</li><li>Energy materials, batteries, solar fuels</li><li>Thin films, nanowires, nanotubes</li><li>High-temperature superconducting materials</li></ul>",
        "source": '<a href="https://uwaterloo.ca/future-graduate-students/programs/by-faculty/engineering/mechanical-and-mechatronics-engineering-doctor-philosophy" target="_blank">Waterloo Future Graduate Students: MME PhD (5 research fields incl. Materials Engineering and Processing; 48 months full-time; Fall/Winter/Spring admission; on-campus; thesis-based; full-time/part-time options)</a> | <a href="https://uwaterloo.ca/graduate-studies-postdoctoral-affairs/future-students/programs/mechanical-and-mechatronics-engineering-phd-waterloo" target="_blank">Waterloo GSPA: MME PhD (deadlines Feb 1/Sept, Jun 1/Jan, Oct 1/May; requires thesis-based Master\'s minimum 80% standing with demonstrated research capabilities; admission to PhD candidacy requires demonstrated research)</a> | <a href="https://uwaterloo.ca/mechanical-mechatronics-engineering/research/materials-engineering-and-processing" target="_blank">Waterloo MME: Materials Engineering and Processing Research Group (electronic packaging; functional nanomaterials; materials interface foundry; NEMS/MEMS; welding; water treatment; faculty Musselman, Wu, Yavuz)</a>',
    },
    "ucla-mse-phd": {
        "school": "University of California Los Angeles, Henry Samueli School of Engineering and Applied Science, Department of Materials Science and Engineering",
        "en": "PhD in Materials Science and Engineering",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "STEM-designated (Engineering / CIP 14.1801)",
        "dur": "Typically 5-6 years; coursework + candidacy exam + dissertation; on-campus residential (Los Angeles, CA); Fall admission only",
        "ddl": "Fall 2027: application opens mid-September 2026. **PhD deadline December 9, 2026, 11:59 pm Pacific Time (PST)**. Fall admission only. Decisions by February-March; response by April 15.",
        "lang": "TOEFL iBT minimum total 87 (MSE allows one subscore below minimum if total at least 87); IELTS Academic minimum; ETS UCLA 4837; waivers per UCLA international policy",
        "test_cls": "GRE OPTIONAL (UCLA MSE: 'not required to take the GRE or submit a GRE score report as part of their application package, however, it is highly encouraged for PhD applicants. The applications with GRE scores will not be given greater weight than those that do not include scores.' ETS institution 4837 / dept 1403. 3 LOR; GPA 3.25/4.0 for PhD.)",
        "tui": "PhD students typically receive full funding through RA, TA, and fellowships (UCLA Engineering minimum package). Out-of-state tuition waived for supported PhD students.",
        "overview": "The Department of Materials Science and Engineering at UCLA's Henry Samueli School of Engineering offers M.S. and Ph.D. degrees. The PhD program admits only in the Fall term. The application opens mid-September 2026, with a deadline of December 9, 2026 at 11:59 pm Pacific Time. The PhD admission requirement is a B.S. in Materials Science and Engineering or a comparable major with minimum undergraduate GPA of 3.25/4.0. Normally the M.S. degree is required for admission to the PhD program, but exceptional students may be admitted directly to the PhD upon receiving their B.S. degree; in most cases, students obtain the M.S. along the way. GRE is optional: applicants are not required to take the GRE or submit scores, though submitting is highly encouraged for PhD applicants; applications with GRE scores are not given greater weight than those without. ETS institution code is 4837, department code is 1403. Three letters of recommendation are required. TOEFL minimum total 87 (one subscore may be below minimum if total is at least 87). Research areas include ceramics and ceramic processing, computational materials science, electronic and optical materials, soft materials, and structural materials.",
        "requirements": "<ul><li>B.S. in Materials Science and Engineering or comparable major</li><li>PhD minimum GPA 3.25/4.0 undergraduate</li><li>MS normally required for PhD admission; exceptional BS direct admission possible</li><li>GRE OPTIONAL (highly encouraged for PhD; no minimum score; not given greater weight)</li><li>TOEFL iBT minimum total 87 (one subscore below minimum allowed if total 87+); IELTS accepted</li><li>ETS UCLA institution 4837 / MSE department 1403</li><li>UCLA Graduate Division online application (opens mid-September)</li><li>3 letters of recommendation</li><li>Statement of purpose</li><li>Departmental supplement</li><li>CV/resume</li><li>Transcripts</li><li>**PhD deadline December 9, 2026, 11:59 pm PST**</li><li>Fall admission only (no Spring/Summer entry)</li><li>On-campus residential (Los Angeles, CA)</li></ul>",
        "research": "<ul><li>Ceramics and ceramic processing</li><li>Computational materials science</li><li>Electronic and optical materials</li><li>Soft materials (polymers, biomaterials)</li><li>Structural materials</li><li>Thin films, nanomaterials</li><li>Energy materials</li></ul>",
        "source": '<a href="https://www.mse.ucla.edu/graduate-admissions/" target="_blank">UCLA MSE Graduate Admissions (2026-2027 application opens mid-September; deadline Dec 9 11:59pm PST; Fall only; PhD minimum GPA 3.25/4.0; MS minimum 3.0/4.0; GRE not required but highly encouraged for PhD; applications with GRE not given greater weight; ETS 4837 / 1403)</a> | <a href="https://grad.ucla.edu/programs/school-of-engineering-and-applied-science/materials-science-engineering/" target="_blank">UCLA Graduate Programs: Materials Science and Engineering (Dec 9, 2026 deadline; Fall admission only; GRE optional; 3 LOR; UCLA-wide minimum requirements)</a> | <a href="https://www.mse.ucla.edu/applicant-faqs/" target="_blank">UCLA MSE Applicant FAQ (ETS 4837; dept 0402; no minimum GRE; no subject test; TOEFL allows one subscore below minimum if total 87+; GRE valid 5 years)</a>',
    },
    "jhu-mse-phd": {
        "school": "Johns Hopkins University, Whiting School of Engineering, Department of Materials Science and Engineering",
        "en": "PhD in Materials Science and Engineering",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "STEM-designated (Engineering / CIP 14.1801)",
        "dur": "Typically 5-6 years; 4 required MSE courses; Whiting School coursework; comprehensive oral exam; dissertation proposal; original research defended publicly; on-campus (Baltimore, MD)",
        "ddl": "Fall 2027: application period opens around August 15, 2026. **PhD deadline December 15, 2026** (11:59 pm ET). Spring 2028: October 15, 2027. MSE (master's) deadline March 15, 2027. BS/MSE combined deadline February 15, 2027.",
        "lang": "TOEFL minimum C559 paper-based (Whiting School); TOEFL iBT / IELTS / Duolingo also accepted; required unless native English speaker or degree from English-speaking country; all documents in English",
        "test_cls": "GRE NOT REQUIRED (Whiting School of Engineering: 'does not require GRE General Test scores for applications to our graduate programs when applying to spring, summer, or fall 2026 start terms.' Institution code 4655 if self-reported. 2 LOR; SOP; unofficial transcripts; official upon admission.)",
        "tui": "PhD students funded by department as TA or RA receive full tuition, stipend, health insurance. VTSI (Vivien Thomas Scholars Initiative) provides full tuition, stipend, benefits for PhD STEM students from eligible institutions (HBCUs, community colleges, regional MD/VA/DC institutions).",
        "overview": "The PhD in Materials Science and Engineering at Johns Hopkins University's Whiting School of Engineering leverages cross-disciplinary research opportunities through the Institute for NanoBioTechnology (INBT), the Hopkins Extreme Materials Institute (HEMI), and the renowned School of Medicine. Students work in small, collaborative groups with faculty and graduate students, with the option to focus on biomaterials and nanotechnology. The PhD requires successful completion of four required courses in materials science and engineering, coursework required by Whiting School policy, a comprehensive oral examination, a research project proposal to form the basis of the dissertation, and completion of an original research project documented in a dissertation defended in a public presentation. The application period opens around August 15 each year. The PhD deadline for Fall 2027 is December 15, 2026; Spring 2028 admission is October 15, 2027. GRE is not required by the Whiting School of Engineering. Two letters of recommendation, statement of purpose, and unofficial transcripts are required (official upon admission). TOEFL minimum is C559 paper-based. The Vivien Thomas Scholars Initiative (VTSI) provides full tuition, stipend, and benefits for PhD STEM students who attended eligible institutions including HBCUs, community colleges, and regional Maryland/Virginia/District of Columbia institutions.",
        "requirements": "<ul><li>Bachelor's degree in engineering, physics, chemistry, materials science, or related field</li><li>GRE NOT REQUIRED (Whiting School of Engineering)</li><li>TOEFL minimum C559 paper-based; TOEFL iBT / IELTS / Duolingo accepted</li><li>Required unless native English speaker or degree from English-speaking country</li><li>Whiting School of Engineering online application (opens ~August 15)</li><li>2 letters of recommendation</li><li>Statement of purpose</li><li>Unofficial transcripts (official upon admission)</li><li>All application documents in English</li><li>No application fee for master's applicants</li><li>**PhD Fall 2027 deadline December 15, 2026** (11:59 pm ET)</li><li>Spring 2028 admission: October 15, 2027</li><li>MSE (master's) deadline March 15, 2027</li><li>BS/MSE combined deadline February 15, 2027</li><li>4 required MSE courses + Whiting School coursework</li><li>Comprehensive oral exam + dissertation proposal + public defense</li><li>On-campus residential (Baltimore, MD)</li></ul>",
        "research": "<ul><li>Biomaterials and medical materials (cross with School of Medicine)</li><li>Nanotechnology (Institute for NanoBioTechnology, INBT)</li><li>Extreme materials (Hopkins Extreme Materials Institute, HEMI)</li><li>Electronic and photonic materials</li><li>Computational materials science</li><li>Cross-disciplinary collaboration with JHU Applied Physics Lab, School of Medicine, Krieger School</li></ul>",
        "source": '<a href="https://engineering.jhu.edu/materials/academics/graduate-programs/graduate-admissions-information/" target="_blank">JHU MSE Graduate Admissions (PhD Dec 15, 2026 / Spring Oct 15, 2027; MSE master\'s Mar 15, 2027; BS/MSE Feb 15, 2027; No GRE required; 2 LOR; SOP; unofficial transcripts; TOEFL C559 paper; no fee for master\'s)</a> | <a href="https://engineering.jhu.edu/admissions/graduate-admissions/full-time-programs/how-to-apply/general-application-requirements/" target="_blank">JHU Whiting School General Application Requirements (application opens ~August 15; GRE not required for spring/summer/fall 2026; institution code 4655 if self-reported; GRE older than 5 years not accepted; TOEFL/IELTS required)</a> | <a href="https://e-catalogue.jhu.edu/engineering/full-time-residential-programs/degree-programs/materials-science-engineering/materials-science-engineering-phd/" target="_blank">JHU Academic Catalogue: MSE PhD (4 required MSE courses; Whiting School coursework; comprehensive oral exam; dissertation proposal; original research defended publicly; closed exam by majority vote; dissertation approved by advisor and one committee member)</a> | <a href="https://apply.jhu.edu/academics/majors-minors-programs/materials-science-and-engineering/" target="_blank">JHU Admissions: MSE (cross-disciplinary research through INBT, HEMI, School of Medicine; biomaterials and nanotechnology focus; small collaborative groups)</a>',
    },
}

# Write detail pages
for p in PROGRAMS:
    key = p["id"]
    d = PAGES[key]
    html = DETAIL_TEMPLATE.format(
        school=d["school"], en=d["en"], deg=d["deg"], stem_label=d["stem_label"],
        dur=d["dur"], ddl=d["ddl"], lang=d["lang"], test_cls=d["test_cls"], tui=d["tui"],
        overview=d["overview"], requirements=d["requirements"],
        research=d["research"], source=d["source"],
    )
    path = os.path.join(DET, key + ".html")
    with open(path, "w", encoding="utf-8") as f:
        f.write(html)
    print(f"Wrote: {key}.html")

# Append PROGRAMS
idx_path = os.path.join(BASE, "index_main.html")
with open(idx_path, "r", encoding="utf-8") as f:
    html = f.read()

def build_entry(p):
    return ('{{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"].replace('"', "'"),
        tui=p["tui"].replace('"', "'"), url=p["url"],
    )

new_entries = ",\n".join(build_entry(p) for p in PROGRAMS)

m = re.search(r'const\s+PROGRAMS\s*=\s*\[', html)
start = m.end()
depth = 1
i = start
while i < len(html) and depth > 0:
    c = html[i]
    if c == '[': depth += 1
    elif c == ']': depth -= 1
    i += 1
insert_pos = i - 1
prefix = html[:insert_pos].rstrip()
suffix = html[insert_pos:]
if prefix.endswith('{'):
    new_block = prefix + "\n" + new_entries + "\n" + suffix
else:
    new_block = prefix + ",\n" + new_entries + "\n" + suffix

# Append VD on new_block (re-locate to avoid offset)
vd_entries = ",\n".join(f'"{p["url"]}"' for p in PROGRAMS)
m2 = re.search(r'VERIFIED_DETAILS\s*=\s*new\s+Set\s*\(\s*\[', new_block)
vstart = m2.end()
vdepth = 1
j = vstart
while j < len(new_block) and vdepth > 0:
    c = new_block[j]
    if c == '[': vdepth += 1
    elif c == ']': vdepth -= 1
    j += 1
vinsert_pos = j - 1
vprefix = new_block[:vinsert_pos].rstrip()
vsuffix = new_block[vinsert_pos:]
if vprefix.endswith(','):
    new_block = vprefix + "\n" + vd_entries + "\n" + vsuffix
elif vprefix.endswith('['):
    new_block = vprefix + "\n" + vd_entries + "\n" + vsuffix
else:
    new_block = vprefix + ",\n" + vd_entries + "\n" + vsuffix

with open(idx_path, "w", encoding="utf-8") as f:
    f.write(new_block)

print(f"Appended {len(PROGRAMS)} PROGRAMS entries and {len(PROGRAMS)} VD entries")
print(f"Done. File size: {os.path.getsize(idx_path)}")
