#!/usr/bin/env python3 """Regenerate references.bib from paper-note frontmatter. The paper notes are the single source of truth; the .bib file is a generated view (like the syntheses). Run from anywhere after adding or editing a paper: python3 _scripts/make_bib.py Entry type is derived from `status`: journal -> @article, accepted/workshop -> @inproceedings, preprint -> @misc (arXiv eprint). Venue annotations in parentheses are stripped for the booktitle/journal field. """ import glob import os import re VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def field(fm: str, name: str) -> str: m = re.search(rf"^{name}: (.*)$", fm, flags=re.M) return m.group(1).strip().strip('"') if m else "" def main() -> None: entries = [] for path in sorted(glob.glob(os.path.join(VAULT, "papers", "*.md")) + glob.glob(os.path.join(VAULT, "influences", "*.md"))): text = open(path, encoding="utf-8").read() parts = text.split("---") if len(parts) < 3: print(f"WARN no frontmatter: {path}") continue fm = parts[1] stem = os.path.splitext(os.path.basename(path))[0] citekey = (field(fm, "citekey") or stem).split("#")[0].strip() or stem title = field(fm, "title") year = field(fm, "year") status = field(fm, "status") arxiv = field(fm, "arxiv") # keep the primary venue only: drop provenance notes after ";" and # trailing parentheticals (poster/oral/decision annotations) venue = field(fm, "venue").split(";")[0].strip() venue = re.sub(r"\s*\([^)]*\)$", "", venue).strip() raw_authors = field(fm, "authors") names = re.findall(r"\[\[(.+?)\]\]", raw_authors) if not names: names = [n.strip() for n in raw_authors.strip("[]").split(",") if n.strip()] authors = " and ".join(names) lines = [f" title = {{{title}}}", f" author = {{{authors}}}", f" year = {{{year}}}"] if status == "journal": kind = "article" lines.append(f" journal = {{{venue}}}") elif status in ("accepted", "workshop"): kind = "inproceedings" lines.append(f" booktitle = {{{venue}}}") else: kind = "misc" if arxiv: lines += [f" eprint = {{{arxiv}}}", " archivePrefix = {arXiv}"] entries.append(f"@{kind}{{{citekey},\n" + ",\n".join(lines) + "\n}") out = os.path.join(VAULT, "references.bib") with open(out, "w", encoding="utf-8") as f: f.write("% Generated by _scripts/make_bib.py — do not edit by hand.\n") f.write("% Source of truth: papers/*.md frontmatter.\n\n") f.write("\n\n".join(entries) + "\n") print(f"wrote {len(entries)} entries to {out}") if __name__ == "__main__": main()