#!/usr/bin/env python3 # description: Save a memory as a denote-formatted flat file # Args: <tags_csv> <body> import sys import os import re from datetime import datetime if len(sys.argv) < 4: print("error: title, tags_csv, and body are required") sys.exit(1) title = sys.argv[1] tags_csv = sys.argv[2] body = sys.argv[3] mount = os.environ.get("OLLIE_9MOUNT") or os.path.expanduser("~/mnt/ollie") mem_dir = os.path.join(mount, "m") 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}")