Last active
December 19, 2015 00:58
-
-
Save andylolz/5872384 to your computer and use it in GitHub Desktop.
convert a directory of jekyll posts to wordpress posts
This file contains hidden or 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
| # -*- 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() |
Author
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
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 ;-)