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
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///

# How I start a new blog post:
# bin/newpost.pyt "Who are your draft readers?"
# which creates ~blog/posts/who-are-your-draft-readers.md
# Aug 3, 2025

import os
import sys
from datetime import date
import subprocess


def slugify(text):
    return (
        text.lower()
        .strip()
        .encode("ascii", "ignore")
        .decode("utf-8")
        .replace("’", "")
        .replace("'", "")
        .replace('"', "")
        .replace("&", "and")
        .replace("@", "at")
        .replace(" ", "-")
        .replace("/", "-")
        .replace("?", "")
        .replace(",", "")
        .replace(".", "")
        .replace(":", "")
        .replace(";", "")
        .replace("--", "-")
    )


def main():
    if len(sys.argv) < 2:
        print('Usage: newpost.py "Post Title Here"')
        sys.exit(1)

    title = sys.argv[1].strip()
    slug = slugify(title)[:40]
    dt = date.today().isoformat()

    post_dir = "content/posts"
    os.makedirs(post_dir, exist_ok=True)
    filename = f"{slug}.md"
    filepath = os.path.join(post_dir, filename)
    if not os.path.exists(filepath):
        with open(filepath, "w") as f:
            f.write(
                f"""---
title: "{title}"
date: {dt}
draft: true
tags: []
---

"""
            )
    print(f"Created {filepath}")
    subprocess.call(("open", f"obsidian:///Users/pradeep/quartz/{filepath}"))


if __name__ == "__main__":
    main()