|
| 1 | +from pathlib import Path |
| 2 | +import re |
| 3 | +import yaml |
| 4 | + |
| 5 | +ROOT = Path(__file__).resolve().parents[1] |
| 6 | +data_path = ROOT / "data" / "problems.yaml" |
| 7 | +readme_path = ROOT / "README.md" |
| 8 | + |
| 9 | +START = "<!-- TABLE:START -->" |
| 10 | +END = "<!-- TABLE:END -->" |
| 11 | + |
| 12 | +def num_link(num): |
| 13 | + return f"[{num}](https://www.erdosproblems.com/{num})" |
| 14 | + |
| 15 | +def md_link(text, url): |
| 16 | + return f"[{text}]({url})" |
| 17 | + |
| 18 | +def oeis_link(code: str) -> str: |
| 19 | + # only link if it's a genuine OEIS identifier like "A123456" |
| 20 | + if re.fullmatch(r"A\d{6}", code): |
| 21 | + return f"[{code}](https://oeis.org/{code})" |
| 22 | + else: |
| 23 | + return code # e.g. "N/A" or any other placeholder |
| 24 | + |
| 25 | +def build_table(rows): |
| 26 | + header = "| ID | Prize | Status | OEIS | Tags |\n|---|---|---|---|---|" |
| 27 | + lines = [header] |
| 28 | + for r in rows: |
| 29 | + oeis = ", ".join(oeis_link(s) for s in r.get("oeis", [])) or "—" |
| 30 | + tags = ", ".join(s for s in r.get("tags", [])) or "—" |
| 31 | + status = r["status"]["state"] |
| 32 | + rid = num_link(r["number"]) |
| 33 | + prize = r.get("prize", "—") |
| 34 | + lines.append(f"| {rid} | {prize} | {status} | {oeis} | {tags} |") |
| 35 | + return "\n".join(lines) |
| 36 | + |
| 37 | +def insert_between_markers(content, payload): |
| 38 | + pattern = re.compile( |
| 39 | + rf"({re.escape(START)})(.*)({re.escape(END)})", |
| 40 | + flags=re.DOTALL |
| 41 | + ) |
| 42 | + repl = rf"\1\n{payload}\n\3" |
| 43 | + return re.sub(pattern, repl, content) |
| 44 | + |
| 45 | +rows = yaml.safe_load(data_path.read_text(encoding="utf-8")) |
| 46 | +rows = sorted(rows, key=lambda r: r["number"].lower()) |
| 47 | +table_md = build_table(rows) |
| 48 | + |
| 49 | +readme = readme_path.read_text(encoding="utf-8") |
| 50 | +new_readme = insert_between_markers(readme, table_md) |
| 51 | +if new_readme != readme: |
| 52 | + readme_path.write_text(new_readme, encoding="utf-8") |
| 53 | + print("README updated.") |
| 54 | +else: |
| 55 | + print("README already up-to-date.") |
0 commit comments