gbrain: sync converted org-mode brain files
This commit is contained in:
@@ -1,12 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Convert brain Org-mode files to markdown + YAML frontmatter and sync into gbrain."""
|
||||
import subprocess, re, os, sys
|
||||
import subprocess, re, os, sys, glob
|
||||
|
||||
BRAIN = "/root/brain"
|
||||
GBRAIN_SRC = "/mnt/hermes/brain"
|
||||
PANDOC = "/usr/bin/pandoc"
|
||||
BUN = os.path.expanduser("~/.bun/bin/gbrain")
|
||||
|
||||
def find_org_files():
|
||||
"""Scan ideas/ recursively for all .org files, return (slug, rel_path, abs_path)."""
|
||||
files = []
|
||||
base = f"{BRAIN}/ideas"
|
||||
for root, dirs, filenames in os.walk(base):
|
||||
for fn in filenames:
|
||||
if not fn.endswith('.org'):
|
||||
continue
|
||||
abs_path = os.path.join(root, fn)
|
||||
rel = os.path.relpath(abs_path, base)
|
||||
# rel is like "compliance/hipaa.org" or "triad-overview.org"
|
||||
name = fn[:-4] # remove .org
|
||||
files.append((name, rel, abs_path))
|
||||
return files
|
||||
|
||||
def gbrain_target(rel_path):
|
||||
"""Derive gbrain target path from org relative path.
|
||||
|
||||
ideas/compliance/hipaa.org → concepts/compliance/hipaa.md
|
||||
ideas/triad-overview.org → concepts/triad-overview.md (via routing dict)
|
||||
ideas/competitive-analysis...→ ideas/competitive-analysis.md
|
||||
"""
|
||||
parts = rel_path.split('/')
|
||||
|
||||
if len(parts) == 1:
|
||||
# Flat file in ideas/ root — use ROUTING dict
|
||||
slug = parts[0][:-4] if parts[0].endswith('.org') else parts[0][:-4]
|
||||
category = ROUTING.get(slug, "concepts")
|
||||
return f"{GBRAIN_SRC}/{category}/{slug}.md"
|
||||
else:
|
||||
# In a subdirectory: ideas/compliance/foo.org → concepts/compliance/foo.md
|
||||
subdir = parts[0]
|
||||
slug = parts[1][:-4] if parts[1].endswith('.org') else parts[1][:-4]
|
||||
return f"{GBRAIN_SRC}/concepts/{subdir}/{slug}.md"
|
||||
|
||||
def extract_org_properties(src_path):
|
||||
"""Extract :PROPERTIES: drawer and #+title/#+filetags from an org file."""
|
||||
props = {}
|
||||
@@ -135,20 +170,13 @@ ROUTING = {
|
||||
}
|
||||
|
||||
def main():
|
||||
# Ensure MECE directories exist
|
||||
for d in ["concepts", "ideas"]:
|
||||
os.makedirs(f"{GBRAIN_SRC}/{d}", exist_ok=True)
|
||||
|
||||
imported = []
|
||||
|
||||
for slug, category in ROUTING.items():
|
||||
src_path = f"{BRAIN}/ideas/{slug}.org"
|
||||
if not os.path.exists(src_path):
|
||||
print(f" SKIP {slug}: not found")
|
||||
continue
|
||||
for slug, rel_path, src_path in find_org_files():
|
||||
dst_path = gbrain_target(rel_path)
|
||||
|
||||
dst_dir = f"{GBRAIN_SRC}/{category}"
|
||||
dst_path = f"{dst_dir}/{slug}.md"
|
||||
# Create parent directories
|
||||
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
||||
|
||||
# Extract frontmatter from org properties
|
||||
props = extract_org_properties(src_path)
|
||||
@@ -168,8 +196,10 @@ def main():
|
||||
with open(dst_path, 'w') as f:
|
||||
f.write(full)
|
||||
|
||||
imported.append(f"{category}/{slug}.md")
|
||||
print(f" OK {category}/{slug}")
|
||||
# Show relative path for clarity
|
||||
rel_dst = os.path.relpath(dst_path, GBRAIN_SRC)
|
||||
imported.append(rel_dst)
|
||||
print(f" OK {rel_dst}")
|
||||
|
||||
print(f"\nConverted {len(imported)} files.")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user