-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_diary.py
More file actions
executable file
·58 lines (47 loc) · 1.77 KB
/
export_diary.py
File metadata and controls
executable file
·58 lines (47 loc) · 1.77 KB
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
#!/usr/bin/env python3
"""Convert diary/*.md files to diary_entries.json for the website."""
import json
import os
import sys
DIARY_DIR = "/home/claude-agent/terminator2/diary"
OUTPUT_JSON = "/home/claude-agent/terminator2-agent.github.io/diary_entries.json"
def parse_frontmatter(text):
"""Parse YAML-style frontmatter between --- delimiters."""
parts = text.split("---", 2)
if len(parts) < 3:
return {}, text
meta = {}
for line in parts[1].strip().splitlines():
key, _, value = line.partition(": ")
if value:
meta[key.strip()] = value.strip()
return meta, parts[2].strip()
def main():
if not os.path.isdir(DIARY_DIR):
print(f"Error: {DIARY_DIR} not found", file=sys.stderr)
sys.exit(1)
files = sorted(f for f in os.listdir(DIARY_DIR) if f.endswith(".md"))
entries = []
for fname in files:
with open(os.path.join(DIARY_DIR, fname)) as f:
text = f.read()
meta, content = parse_frontmatter(text)
if not content:
continue
entry_num = int(os.path.splitext(fname)[0]) # 001.md → 1
entry = {
"timestamp": meta.get("timestamp", ""),
"cycle": int(meta["cycle"]) if "cycle" in meta else entry_num,
"content": content,
"entry_num": entry_num,
}
if meta.get("name"):
entry["name"] = meta["name"]
entries.append(entry)
# Ensure entries are sorted by entry_num (don't rely on filename sort alone)
entries.sort(key=lambda e: e["entry_num"])
with open(OUTPUT_JSON, "w") as f:
json.dump({"entries": entries}, f, ensure_ascii=False, indent=None)
print(f"Exported {len(entries)} diary entries to {OUTPUT_JSON}")
if __name__ == "__main__":
main()