This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-9p/tools/memory_remember

62 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
# description: Save a memory as a denote-formatted flat file
# Args: <title> <tags_csv> <body>
import sys
import os
import re
from datetime import datetime
if len(sys.argv) < 4:
print("usage: memory_remember <title> <tags_csv> <body>")
sys.exit(1)
title = sys.argv[1]
tags_csv = sys.argv[2]
body = sys.argv[3]
mount = os.environ["OLLIE"]
mem_dir = os.path.join(mount, "m")
if not os.path.isdir(mem_dir):
print(f"error: memory directory does not exist: {mem_dir}")
sys.exit(1)
now = datetime.now()
ident = now.strftime("%Y%m%dT%H%M%S")
date = now.strftime("%Y-%m-%d %a %H:%M")
tags = [t.strip() for t in tags_csv.split(",") if t.strip()]
def slugify(s):
s = s.lower()
s = re.sub(r"[^a-z0-9]+", "-", s)
return s.strip("-")
slug = slugify(title)
tag_slug = "-".join(slugify(t) for t in tags)
filename = f"{ident}__{slug}--{tag_slug}.md" if tag_slug else f"{ident}__{slug}.md"
path = os.path.join(mem_dir, filename)
tags_yaml = "[" + ", ".join(f'"{t}"' for t in tags) + "]"
content = f"""---
title: {title}
date: {date}
tags: {tags_yaml}
identifier: {ident}
signature:
---
{body}
"""
try:
with open(path, "w") as f:
f.write(content)
except OSError as e:
print(f"error: failed to write {path}: {e}")
sys.exit(1)
print(f"saved: {filename}")