# -*- coding: utf-8 -*-
"""Extract EN.xxx course blocks (code + title) from a JHU course-descriptions page."""
import io, re, sys, os
BASE = r"D:\work buddy\2026-11-05-14-15-52"
f = sys.argv[1]
if not os.path.isabs(f):
    f = os.path.join(BASE, "cache", f)
t = io.open(f, encoding="utf-8", errors="ignore").read()
blocks = re.findall(r'detail-code[^>]*><strong>(.*?)</strong></span>.*?detail-title[^>]*><strong>(.*?)</strong></span>', t, flags=re.S | re.I)
print("blocks:", len(blocks))
rows = []
for code, tb in blocks:
    code = re.sub(r'<[^>]+>', '', code).strip(" .")
    title = re.sub(r'<[^>]+>', '', tb).strip(" .")
    title = re.sub(r'&nbsp;', ' ', title).replace("&amp;", "&")
    title = re.sub(r'[ \t\xa0]+', ' ', title).strip()
    rows.append((code, title))
print("rows:", len(rows))
for r in rows[:40]:
    print(r)
