Skip to content

Instantly share code, notes, and snippets.

@andylolz
Last active December 19, 2015 00:58
Show Gist options
  • Select an option

  • Save andylolz/5872384 to your computer and use it in GitHub Desktop.

Select an option

Save andylolz/5872384 to your computer and use it in GitHub Desktop.
convert a directory of jekyll posts to wordpress posts
# -*- coding: utf-8 -*-
import os
import markdown
import yaml
from pywordpress import Wordpress
import xmlrpclib
class jekyll_to_wp():
def __init__(self):
blogs = self.parse_blogs()
for blog in blogs:
# TODO!
pass
# parse all blogs in a given directory
def parse_blogs(self, directory="_posts"):
return [self.parse_blog(directory, blog_filename) for blog_filename in os.listdir(directory)]
# parse the specified blog file
def parse_blog(self, directory, blog_filename):
# fetch the file into memory
with open('%s/%s' % (directory, blog_filename)) as blogfile:
lines = blogfile.readlines()
# find the bounds of the front matter
front_matter_bounds = [ind for ind in range(len(lines)) if lines[ind].startswith('---')]
if len(front_matter_bounds) < 2:
# sanity check - front matter isn't properly formatted
return False
front_matter_raw = ''.join(lines[front_matter_bounds[0]+1:front_matter_bounds[1]])
markdown_raw = ''.join(lines[front_matter_bounds[1]+1:]).decode('utf8')
# parse front matter yaml
front_matter = yaml.load(front_matter_raw)
front_matter['date'] = blog_filename[:10]
# parse blog markdown
m = markdown.Markdown()
html = m.convert(markdown_raw)
return {"front_matter": front_matter, "html": html}
jekyll_to_wp()
@rufuspollock
Copy link

This looks great. As a tip, based on experience i suggest running this against e.g. staging.okfn.org before running on your target site ;-)

@andylolz
Copy link
Author

andylolz commented Jul 4, 2013

sorry – only just spotted this comment. Good tip, thanks!

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