#!/usr/bin/env python3
from datetime import datetime
import argparse
import os.path
from slugify import slugify
import subprocess
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create a new post")
parser.add_argument("title", help="Title of the post")
parser.add_argument("--no-ide", help="Do not open a IDE", action="store_false", dest="ide")
parser.add_argument("--no-git", help="Do not add the file to git", action="store_false", dest="git")
args = parser.parse_args()
now = datetime.now()
slug = slugify(args.title)
filename = os.path.join("content", "post", f"{now.year}", f"{now.month}",
f"{slug}.md")
print(f"Creating new post: {filename}")
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "w", encoding="utf-8") as f:
f.write("---\n")
f.write(f"title: \"{args.title}\"\n")
f.write(f"date: {now.strftime('%Y-%m-%dT%H:%M:%S')}\n")
f.write(f"slug: \"{slug}\"\n")
f.write(f"author: Uli Köhler\n")
f.write(f"katex: false\n\n")
f.write(f"draft: true\n\n")
f.write(f"categories:\n - \n")
f.write("---\n\n")
if args.git:
subprocess.run(["git", "add", filename])
if args.ide:
subprocess.run(["code", filename])
Usage example
./new-post.py "Hugo script to create a new post and automatically open it in the editor"