cl-agentic-vault / _scripts /make_ideas_index.py
TLESORT's picture
Initial release of the Continual-Learning Ideas Vault (vault commit e3600181)
9542666 verified
Raw History Blame Contribute Delete
4.43 kB
#!/usr/bin/env python3
"""Regenerate _ideas-index.md as a two-level outline from idea frontmatter.
Hierarchy comes from each idea's `parent:` field (empty = root/main idea).
Roots are ranked by total paper engagement (own + descendants), so the most
contested conversations float to the top. Paper counts per idea in brackets.
python3 _scripts/make_ideas_index.py
"""
import glob
import os
import re
from collections import defaultdict
VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def fm_field(text: str, name: str) -> str:
m = re.search(rf'^{name}: (.*)$', text.split("---")[1], flags=re.M)
return m.group(1).strip().strip('"') if m else ""
def main() -> None:
ideas, parent, statement = set(), {}, {}
for path in glob.glob(os.path.join(VAULT, "ideas", "*.md")):
slug = os.path.splitext(os.path.basename(path))[0]
text = open(path, encoding="utf-8").read()
ideas.add(slug)
statement[slug] = fm_field(text, "statement")
p = re.sub(r"[\[\]]", "", fm_field(text, "parent"))
parent[slug] = p if p in (None, "") else p
# paper engagement counts
count = defaultdict(int)
for path in glob.glob(os.path.join(VAULT, "papers", "*.md")) + glob.glob(os.path.join(VAULT, "influences", "*.md")):
seen = set()
for m in re.finditer(r'^(first-mention|argues-for|argues-against|nuances|tests)::\s*\[\[(.+?)\]\]',
open(path, encoding="utf-8").read(), flags=re.M):
if m.group(2) in ideas:
seen.add(m.group(2))
for s in seen:
count[s] += 1
children = defaultdict(list)
roots = []
for s in sorted(ideas):
p = parent.get(s) or ""
if p and p in ideas:
children[p].append(s)
else:
roots.append(s)
if p and p not in ideas:
print(f"WARN: parent of {s} does not resolve: {p}")
def subtree_count(s):
return count[s] + sum(subtree_count(c) for c in children[s])
lines = ["---", "tags: [meta]", "---", "", "# Ideas index", "",
"Generated by `_scripts/make_ideas_index.py` from idea frontmatter —",
"set an idea's `parent:` field to place it; empty parent = main idea.",
"Roots ranked by total paper engagement (own + sub-ideas); `[n]` = papers",
"engaging that idea directly. Skills check this outline before creating",
"a new idea — a candidate that restates any line below links instead.", ""]
def emit(s, depth):
short = statement[s]
if len(short) > 1 and not short[1].isupper():
short = short[0].lower() + short[1:]
short = short.split(";")[0]
lines.append(f"{' ' * depth}- [[{s}]] `[{count[s]}]` — {short}")
for c in sorted(children[s], key=lambda x: (-subtree_count(x), x)):
emit(c, depth + 1)
for r in sorted(roots, key=lambda x: (-subtree_count(x), x)):
emit(r, 0)
open(os.path.join(VAULT, "_ideas-index.md"), "w", encoding="utf-8").write("\n".join(lines) + "\n")
print(f"wrote outline: {len(roots)} main ideas, {len(ideas) - len(roots)} sub-ideas")
# maintain a Sub-ideas section on parent notes (marker-delimited; added/removed as hierarchy changes)
START, END = "<!-- SUBIDEAS:START (generated by make_ideas_index.py) -->", "<!-- SUBIDEAS:END -->"
BLOCK = START + """
## Sub-ideas
```dataview
TABLE WITHOUT ID file.link AS "Sub-idea", status AS Status
FROM "ideas"
WHERE contains(string(parent), this.file.name)
SORT file.name ASC
```
""" + END
touched = 0
for s in ideas:
path = os.path.join(VAULT, "ideas", f"{s}.md")
text = open(path, encoding="utf-8").read()
has_block = START in text
if children[s] and not has_block:
if "\n## Notes" in text:
text = text.replace("\n## Notes", "\n" + BLOCK + "\n## Notes", 1)
else:
text = text.rstrip("\n") + "\n\n" + BLOCK + "\n"
open(path, "w", encoding="utf-8").write(text); touched += 1
elif not children[s] and has_block:
text = re.sub(re.escape(START) + r".*?" + re.escape(END) + r"\n?", "", text, flags=re.S)
open(path, "w", encoding="utf-8").write(text); touched += 1
print(f"sub-ideas sections maintained on {touched} notes")
if __name__ == "__main__":
main()