File size: 5,689 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #!/usr/bin/env python3
"""Regenerate _to-read.md: dangling paper links across the vault.
A slug like `benjamin2019-fdr` referenced by notes but having no
papers/benjamin2019-fdr.md is a paper the discourse points to that we haven't
analyzed — the to-read list. Reference count is the in-vault influence proxy.
The section below the MANUAL marker is preserved verbatim across runs — put
titles, priorities, and notes there.
python3 _scripts/make_toread.py
"""
import glob
import os
import re
from collections import defaultdict
VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SCAN_DIRS = ["papers", "influences", "ideas", "concepts", "syntheses", "methods"]
MARKER = "<!-- MANUAL — everything below survives regeneration -->"
def main() -> None:
existing = set()
for d in SCAN_DIRS + ["_templates", "authors"]:
existing |= {os.path.splitext(os.path.basename(p))[0]
for p in glob.glob(os.path.join(VAULT, d, "*.md"))}
existing |= {os.path.splitext(f)[0] for f in os.listdir(VAULT) if f.endswith(".md")}
refs = defaultdict(set)
idea_refs = defaultdict(int)
for d in SCAN_DIRS:
for path in glob.glob(os.path.join(VAULT, d, "*.md")):
stem = os.path.splitext(os.path.basename(path))[0]
for line in open(path, encoding="utf-8"):
if line.startswith("authors:") or line.strip().startswith("uses::"):
continue # author and trick nodes dangle by design
clean = re.sub(r"`[^`]*`", "", line) # code spans aren't links
for t in re.findall(r"\[\[(.+?)\]\]", clean):
t = t.split("|")[0].split("#")[0].strip()
if t and t not in existing and re.match(r"^[a-z][a-z-]*\d{4}-", t):
refs[t].add(stem)
if d == "ideas":
idea_refs[t] += 0 # counted below via source sets
# idea-refs = how many of a slug's referencing notes are idea notes
idea_stems = {os.path.splitext(os.path.basename(p))[0]
for p in glob.glob(os.path.join(VAULT, "ideas", "*.md"))}
for t in refs:
idea_refs[t] = sum(1 for s in refs[t] if s in idea_stems)
# External influences: registered out-of-scope slugs leave the to-read
# queue and get their own ranked table inside _influences.md.
inf_path = os.path.join(VAULT, "_influences.md")
external = set()
if os.path.exists(inf_path):
inf_text = open(inf_path, encoding="utf-8").read()
register = inf_text.split("## Register", 1)[1] if "## Register" in inf_text else ""
# In-scope rulings are not external — but "Wave-N additions" blocks
# appended after that heading are register entries; keep those lines.
if "## Adjudicated IN scope" in register:
register, adjudicated = register.split("## Adjudicated IN scope", 1)
in_wave = False
for line in adjudicated.splitlines():
if re.match(r"^Wave-\d+ additions", line):
in_wave = True
elif line.startswith("## "):
in_wave = False
if in_wave:
register += line + "\n"
external = {t.split("|")[0].split("#")[0].strip()
for t in re.findall(r"\[\[(.+?)\]\]", register)
if re.match(r"^[a-z][a-z-]*\d{4}-", t.split("|")[0].strip())}
ext_refs = {s: refs[s] for s in external if s in refs}
gen = ["", "| External root | Refs | Imported by |", "|---|---|---|"]
for slug in sorted(ext_refs, key=lambda s: (-len(ext_refs[s]), s)):
srcs = ", ".join(f"[[{s}]]" for s in sorted(ext_refs[slug]))
gen.append(f"| [[{slug}]] | {len(ext_refs[slug])} | {srcs} |")
gen.append("")
start, end = "<!-- GENERATED:START (by make_toread.py — ref counts + importing notes) -->", "<!-- GENERATED:END -->"
if start in inf_text and end in inf_text:
pre = inf_text.split(start, 1)[0]
post = inf_text.split(end, 1)[1]
open(inf_path, "w", encoding="utf-8").write(pre + start + "\n" + "\n".join(gen) + end + post)
print(f"wrote {len(ext_refs)} external influences to _influences.md")
refs = {k: v for k, v in refs.items() if k not in external}
out = os.path.join(VAULT, "_to-read.md")
manual = ""
if os.path.exists(out):
prev = open(out, encoding="utf-8").read()
if MARKER in prev:
manual = prev.split(MARKER, 1)[1]
lines = [
"---",
"tags: [meta]",
"---",
"",
"# To-read list",
"",
"Papers the vault's discourse points to but that have no note yet —",
"generated by `_scripts/make_toread.py` from dangling links; reference",
"count is the in-vault influence proxy. Feed a slug to `/analyze-paper`",
"and it disappears from this list on the next regeneration.",
"",
"| Wanted paper | Refs | Idea-refs | Referenced from |",
"|---|---|---|---|",
]
for slug in sorted(refs, key=lambda s: (-len(refs[s]), -idea_refs[s], s)):
srcs = ", ".join(f"[[{s}]]" for s in sorted(refs[slug]))
lines.append(f"| [[{slug}]] | {len(refs[slug])} | {idea_refs[slug]} | {srcs} |")
lines += ["", MARKER]
body = "\n".join(lines) + (manual if manual else "\n\n## Notes / titles / priorities\n\n(add freely — preserved across regenerations)\n")
open(out, "w", encoding="utf-8").write(body)
print(f"wrote {len(refs)} wanted papers to _to-read.md")
if __name__ == "__main__":
main()
|