File size: 3,913 Bytes
9542666 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #!/usr/bin/env python3
"""Create author notes from paper-note frontmatter and keep their paper
lists fresh.
One note per unique author name in papers/influences frontmatter. Each note
carries a GENERATED paper-links section between markers — real wikilinks, so
they register in Obsidian's graph and render outside Obsidian (export,
GitHub/HF views), unlike dataview blocks. The generated section is refreshed
on every run; anything the user writes outside the markers is preserved.
python3 _scripts/make_authors.py
Also reports author notes that no paper references anymore (possible
spelling drift) — it never deletes them.
"""
import glob
import os
import re
import unicodedata
VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AUTHORS_DIR = os.path.join(VAULT, "authors")
STUB = """---
tags: [author]
---
# {name}
<!-- Enrich freely above/below; the section between markers is regenerated. -->
{papers_section}"""
MARK_A = "<!-- GENERATED:papers -->"
MARK_B = "<!-- /GENERATED:papers -->"
def papers_section(entries):
lines = "\n".join(f"- [[{slug}]] ({year}, {venue})" if venue else f"- [[{slug}]] ({year})"
for slug, year, venue in entries) or "- (none currently)"
return f"{MARK_A}\n## Papers in this vault\n\n{lines}\n{MARK_B}\n"
def main() -> None:
os.makedirs(AUTHORS_DIR, exist_ok=True)
by_author = {}
for path in glob.glob(os.path.join(VAULT, "papers", "*.md")) + glob.glob(os.path.join(VAULT, "influences", "*.md")):
txt = open(path, encoding="utf-8").read()
fm = txt.split("---")[1]
m = re.search(r"^authors: \[(.*)\]$", fm, flags=re.M)
if not m:
continue
slug = os.path.splitext(os.path.basename(path))[0]
ym = re.search(r"^year:\s*(\d+)", fm, flags=re.M)
vm = re.search(r'^venue:\s*"?(.*?)"?\s*$', fm, flags=re.M)
year = ym.group(1) if ym else "?"
venue = (vm.group(1) if vm else "")[:40]
for name in re.findall(r"\[\[(.+?)\]\]", m.group(1)):
by_author.setdefault(name, []).append((slug, year, venue))
names = set(by_author)
created, refreshed = 0, 0
for name in sorted(names):
entries = sorted(by_author[name], key=lambda e: (e[1], e[0])) # year, then slug: stable across machines
section = papers_section(entries)
out = os.path.join(AUTHORS_DIR, f"{name}.md")
if not os.path.exists(out):
with open(out, "w", encoding="utf-8") as f:
f.write(STUB.format(name=name, papers_section=section))
created += 1
continue
txt = open(out, encoding="utf-8").read()
if MARK_A in txt and MARK_B in txt:
new = re.sub(re.escape(MARK_A) + r".*?" + re.escape(MARK_B) + r"\n?",
section, txt, flags=re.S)
else:
# legacy stub: replace the dataview papers section wholesale
new = re.sub(r"## Papers in this vault\n+```dataview.*?```\n?",
section, txt, flags=re.S)
if new == txt: # enriched/unknown layout: append the generated section
new = txt.rstrip() + "\n\n" + section
if new != txt:
open(out, "w", encoding="utf-8").write(new)
refreshed += 1
# macOS may list filenames decomposed (NFD) while notes spell names composed
# (NFC): compare in NFC so accented authors are not reported as stale.
nfc = lambda s: unicodedata.normalize("NFC", s)
existing = {nfc(os.path.splitext(f)[0]) for f in os.listdir(AUTHORS_DIR) if f.endswith(".md")}
stale = existing - {nfc(n) for n in names}
print(f"authors referenced: {len(names)}, notes created: {created}, link-sections refreshed: {refreshed}")
if stale:
print(f"notes no paper references (check spelling drift, not deleted): {sorted(stale)}")
if __name__ == "__main__":
main()
|