Skip to content

Instantly share code, notes, and snippets.

@tomalrussell
Created February 12, 2015 10:09
Show Gist options
  • Save tomalrussell/0828db8491b99db032fe to your computer and use it in GitHub Desktop.
Save tomalrussell/0828db8491b99db032fe to your computer and use it in GitHub Desktop.
Jekyll New Post
#! /usr/bin/env python3
import sys
import datetime
import os
# set title to command-line argument, or default
if (len(sys.argv) > 1):
title = sys.argv[1]
else:
title = "New Post"
# use the rest of the arguments for categories
categories = " ".join(sys.argv[2:])
# standard filename format: date and title
filename = datetime.datetime.now().strftime('%Y-%m-%d-') + title.lower().replace(" ","-") + '.md'
# standard front matter: title, full date, categories
front_matter = '''\
---
layout: post
title: %s
date: %s
categories: %s
---
''' % (title, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), categories)
# if we're in a jekyll root, pop it in ./_posts
if(os.path.exists(os.getcwd() + '/_posts')):
filepath = os.getcwd() + '/_posts/' + filename
else:
filepath = os.getcwd() + '/' + filename
# check if this post exists already, otherwise create and write!
if(os.path.exists(filepath)):
print ("Looks like this post already exists: " + filepath)
else:
with open(filepath,'w') as f:
print( front_matter, file=f)
print ("Post created! " + filename)
@tomalrussell
Copy link
Author

Use to create a new jekyll blog post with today's date, like:

./jekyll-new-post.py 'A Cool New Post about Chunky Bacon' chunky-bacon _why-rules

If you stumble across this, please point out any obvious blunders - python and I are just getting to know one another.

@rijieli
Copy link

rijieli commented Sep 26, 2020

Thank you, Tom,

I've encountered a time zone problem. Jekyll told me one of the new post "Skipping: _posts/2020-09-26-test-time-zone.md has a future date"

There's two way to solve this problem

Add time zone to line 27:

''' % (title, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') ++0900’, categories)

Or just add below to _config.yml, Jekyll will reading future posts.

future: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment