Skip to content

Instantly share code, notes, and snippets.

@ancillaryfactory
Last active December 11, 2015 01:58
Show Gist options
  • Save ancillaryfactory/4526867 to your computer and use it in GitHub Desktop.
Save ancillaryfactory/4526867 to your computer and use it in GitHub Desktop.
Utility to build new Pelican posts from the command line. Add new_post.py to the root Pelican directory and add the two lines below to Makefile. Type `make post` to run the script. NOTE: After the new post file is created, it's automatically opened in the text editor of your choice. I used Sublime Text in a Windows environment as the example.
post:
@python $(BASEDIR)/new_post.py "$(INPUTDIR)"
import sys, os, datetime, re, subprocess
# get content folder from command line
input_dir = sys.argv[1]
today = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
# prompt user for post title
post_title = raw_input('ENTER POST TITLE: ')
post_title_slug = re.sub(r'[^a-zA-Z0-9\.]+', '-', post_title).lower()
metadata = """Title: %s
Date: %s
Tags:
Category:
Slug: %s
Author:
Summary:
""" % (post_title, today, post_title_slug)
# open file and write metadata
filepath = input_dir + '/' + post_title_slug + '.md'
subprocess.call(['touch', filepath])
f = open(filepath, 'r+b')
f.write(metadata)
f.close()
if f:
print '\n'
print 'NEW POST CREATED: %s' %(filepath)
subprocess.call(['C:\\Program Files\\Sublime Text 2\\sublime_text.exe', filepath])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment