Last active
February 28, 2016 22:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias post='create_post.py --basedir="/path/to/pelican/" -a "Author name"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
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
Putting the above script somewhere PATH environment sees, just run this script as
The script creates a file and open it with some editor. After editing the post, you can update your blog with
make html
.