Skip to content

Instantly share code, notes, and snippets.

@ryokbys
Last active December 22, 2017 08:47
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/d4f343cdbb2f5d33c305 to your computer and use it in GitHub Desktop.
Save ryokbys/d4f343cdbb2f5d33c305 to your computer and use it in GitHub Desktop.
Blog post generation and make for Pelican static blog generator.
#!/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.[md|rst]`` .
This should be run at the directory that includes ``content`` directory.
Usage:
myblog.py post [options] TITLE
myblog.py notebook [options] NOTEBOOK
myblog.py make [options]
myblog.py upload [options]
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]
--rst Set text format ReST. If false (default), use Markdonw. [default: False]
'''
import os
import sys
from docopt import docopt
from datetime import datetime as dt
_content = 'content'
def make_post(basedir, author, slug, title, rst):
"""
Create a blog post
"""
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()
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_rst = ''':title: {0:s}
:date: {1:s}
:category: misc
:slug: {2:s}
:authors: {3:s}
:tags:
'''.format(title, date, slug, author)
header_template_md = '''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)
if rst:
fname = basedir+dname+'/{0:s}_{1:s}.rst'.format(day, slug)
else:
fname = basedir+dname+'/{0:s}_{1:s}.md'.format(day, slug)
if os.path.exists(fname):
print 'Error: '+fname+' already exists !'
sys.exit()
with open(fname, 'w') as f:
if rst:
f.write(header_template_rst+'\n')
else:
f.write(header_template_md+'\n')
f.write('\n')
f.write('Write a content here!\n\n')
print fname+' is written.'
os.system('open '+fname)
# os.chdir(cwd)
def notebook_to_post(notepath, basedir, author, slug):
"""
Make a pelican blog post from a given jupyter notebook.
"""
fullnotepath = os.path.abspath(notepath)
cwd = os.getcwd()
os.chdir(basedir)
title = slug
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()
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_notebook = '''Title: {0:s}
Slug: {1:s}
Date: {2:s}
Authors: {3:s}
Category: misc
Tags: python, notebook
'''.format(title, slug, date, author)
dname = _content+'/{0:s}/{1:s}'.format(year, month)
os.system('mkdir -p '+dname)
fname = basedir+'/'+dname+'/{0:s}_{1:s}.ipynb'.format(day, slug)
if os.path.exists(fname):
print 'Error: '+fname+' already exists !'
sys.exit()
#...Copy ipynb file to the post directory
os.system('cp {0:s} {1:s}'.format(fullnotepath,fname))
#...Create ipynb-meta file
metafname = fname+'-meta'
with open(metafname, 'w') as f:
f.write(header_template_notebook+'\n')
print 'Created {0:s} and {1:s}'.format(os.path.basename(fname),os.path.basename(metafname)) \
+' at {0:s}.'.format(basedir)
return None
if __name__ == '__main__':
args = docopt(__doc__)
basedir = os.path.expanduser(args['--basedir'])
author = args['--author']
slug = args['--slug']
rst = args['--rst']
if args['post']:
title = args['TITLE']
if slug == 'None':
slug = title.lower().replace(' ', '-')
slug = slug.replace('/', '-')
make_post(basedir, author, slug, title, rst)
elif args['notebook']:
notepath = args['NOTEBOOK']
notename = os.path.basename(notepath)
if notename == '':
raise ValueError('NOTEBOOK, {0:s}, is not a file.'.format(notepath))
if slug == 'None':
slug = notename.lower().replace(' ','-')
slug = slug.replace('_','-')
slug = slug.replace('.ipynb','')
print notepath
notebook_to_post(notepath, basedir, author, slug)
elif args['make']:
cwd = os.getcwd()
os.chdir(basedir)
os.system('make html')
os.chdir(cwd)
elif args['upload']:
cwd = os.getcwd()
os.chdir(basedir)
os.system('make rsync_upload')
os.chdir(cwd)
@ryokbys
Copy link
Author

ryokbys commented Feb 28, 2016

If you put following lines in ~/.bashrc,

alias blogpost='myblog.py post --basedir="~/Dropbox/Public/pelican_blog/" -a "AUTHOR NAME"'
alias blogmake='myblog.py make --basedir="~/Dropbox/Public/pelican_blog/"'

you can create a blog with a command

$ blogpost 'some title here'

then, new a blog post for Pelican system is created and automatically open a editor (in this case, Emacs.app).

And you can perform make html by typing

$ blogmake

regardless of the current working directory.

@ryokbys
Copy link
Author

ryokbys commented Dec 22, 2017

One can create a post from a jupyter notebook by

$ myblog.py notebook --basedir="~/Dropbox/Public/pelican_blog" -a "AUTHOR NAME" some_jupyter_notebook.ipynb

This will create ipynb-meta file as well as copying the jupyter notebook to the pelican blog contents directory.

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