Skip to content

Instantly share code, notes, and snippets.

@Sigafoos
Last active September 21, 2017 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sigafoos/3adf25a94718d4edb0be4ffbe63cf639 to your computer and use it in GitHub Desktop.
Save Sigafoos/3adf25a94718d4edb0be4ffbe63cf639 to your computer and use it in GitHub Desktop.
Python script to create the file and YAML frontmatter for a Jekyll post
#!/usr/bin/python
from slugify import slugify # this will have to be installed via pip
import subprocess
import time
blog_dir = "/home/you" # CHANGE THIS
blog_name = "blog" # CHANGE THIS
posts_dir = "/_posts"
# here you could override via command line but I'm lazy
title = raw_input("post title: ")
categories = raw_input("categories (space separated): ")
filename = "%s/%s%s/%s-%s.md" % (blog_dir, blog_name, posts_dir, time.strftime("%Y-%m-%d"), slugify(title))
with open(filename, "a") as fp:
template = """---
layout: post
title: "{title}"
date: {date}
categories: {categories}
---"""
context = {
"title": title,
"date": time.strftime("%Y-%m-%d %H:%m:%S %z"),
"categories": categories
}
fp.write(template.format(**context))
subprocess.call(["vim", filename])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment