Skip to content

Instantly share code, notes, and snippets.

@ryokbys
Last active February 28, 2016 22:13
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 ryokbys/e5dea8f2476a83ec2758 to your computer and use it in GitHub Desktop.
Save ryokbys/e5dea8f2476a83ec2758 to your computer and use it in GitHub Desktop.
Create a blog post for Pelican, which is a static blog system. Note other than Mac, comment out the line 78.
alias post='create_post.py --basedir="/path/to/pelican/" -a "Author name"'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
This creates blog post file for Pelican at the directory in the year
and month of the current date as ``content/yyyy/mm/dd_slug.rst`` .
This should be run at the directory that includes ``content`` directory.
Usage:
create_post.py [options] TITLE
Options:
-h, --help Show this help message and exit.
-a, --author=AUTHOR
Specifies author. [default: None]
-s, --slug=SLUG
Specifies slug string. [default: None]
--basedir=BASEDIR
Specifies base directory where the Pelican blog system is. [default: None]
'''
import os,sys
from docopt import docopt
from datetime import datetime as dt
_content= 'content'
if __name__ == '__main__':
args= docopt(__doc__)
basedir= os.path.expanduser(args['--basedir'])
cwd= os.getcwd()
os.chdir(basedir)
if not os.path.exists(_content):
print 'Error: {0:s} does not exist.\n'.format(_content)
print __doc__
os.chdir(cwd)
sys.exit()
if not os.path.isdir(_content):
print 'Error: {0:s} is not a directory.\n'.format(_content)
print __doc__
os.chdir(cwd)
sys.exit()
author= args['--author']
slug= args['--slug']
title= args['TITLE']
if slug == 'None':
slug= title.lower().replace(' ','-')
now= dt.now()
date= now.strftime('%Y-%m-%d %H:%M:%S')
year= now.strftime('%Y')
month= now.strftime('%m')
day= now.strftime('%d')
header_template='''
:title: {0:s}
:date: {1:s}
:category: misc
:slug: {2:s}
:authors: {3:s}
:tags:
'''.format(title,date,slug,author)
dname= _content+'/{0:s}/{1:s}'.format(year,month)
os.system('mkdir -p '+dname)
fname= basedir+dname+'/{0:s}_{1:s}.rst'.format(day,slug)
with open(fname,'w') as f:
f.write(header_template+'\n')
f.write('\n')
f.write('Write a content here!\n\n')
print fname+' is written.'
os.system('open '+fname) # Only for Mac
#os.chdir(cwd)
@ryokbys
Copy link
Author

ryokbys commented Jan 10, 2016

Putting the above script somewhere PATH environment sees, just run this script as

$ post "new post title"

The script creates a file and open it with some editor. After editing the post, you can update your blog with make html.

@ryokbys
Copy link
Author

ryokbys commented Feb 28, 2016

This one is out of date. I uploaded revised one as myblog.py today.

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