garden/scripts/parse_daily.py

44 lines
1.2 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
import yaml
import json
from datetime import date
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, date):
return obj.isoformat()
return super().default(obj)
def parse_daily_note(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
parts = content.split('---')
if len(parts) < 3:
print("{}", file=sys.stderr)
return
frontmatter = yaml.safe_load(parts[1])
body = parts[2].strip()
# Find the "Work:" section
work_section = ""
in_work_section = False
for line in body.splitlines():
if line.strip().lower().startswith('**work:**'):
in_work_section = True
continue
if in_work_section:
if line.strip() == "" and work_section:
# Break on the first blank line after finding some work items
break
if line.strip().startswith('- '):
work_section += line.strip()[2:] + '\n'
frontmatter['work'] = work_section.strip()
print(json.dumps(frontmatter, cls=DateEncoder))
if __name__ == "__main__":
if len(sys.argv) > 1:
parse_daily_note(sys.argv[1])