Created
May 16, 2015 23:12
-
-
Save mseri/8fb565a13f3b6a14fe38 to your computer and use it in GitHub Desktop.
Takes a Ghost (www.ghost.org) json datafile and fills the folder with markdown posts.
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
"""Takes a Ghost json datafile and fills the folder with markdown posts. | |
It does not import the tags (yet).""" | |
import json | |
AUTHOR = "My Name" | |
GHOST_DATAFILE = "ghostData.json" | |
ARTICLE_FOLDER = "" # An empty string corresponds to the script folder | |
def slugify(value): | |
""" | |
Normalizes string, converts to lowercase, removes non-alpha characters, | |
and converts spaces to hyphens. | |
""" | |
import unicodedata | |
import re | |
value = str(unicodedata.normalize('NFKD', value).encode('ascii', 'ignore'), "utf8") | |
value = re.sub('[^\w\s-]', '', value).strip().lower() | |
return re.sub('[-\s]+', '-', value) | |
def prepare_post(jpost): | |
"""Takes the post data dictionary and generates a markdown post for pelican""" | |
meta = """Id: {4}, | |
Title: {0} | |
Date: {1} | |
Modified: {2} | |
Tags: | |
Category: Blog | |
Slug: {3} | |
Authors: {4}""".format(jpost['title'], | |
jpost['published_at'], | |
jpost['updated_at'], | |
jpost['slug'], | |
jpost['id'], | |
AUTHOR) | |
meta = '\n'.join([meta, "Status: draft"]) if jpost['status'] != 'published' else meta | |
content = jpost['markdown'].replace('" target="_blank','') | |
return ("{0:02d}-{1}".format(jpost['id'], slugify(jpost['title'])), '\n\n'.join([meta, content])) | |
def save_post(ppost): | |
"""Saves the markdown post to a file""" | |
with open(ARTICLE_FOLDER + ppost[0] + ".md", "w") as f: | |
f.write(ppost[1]) | |
print(ppost[0], "written!") | |
if __name__ == "__main__": | |
with open(GHOST_DATAFILE) as f: | |
data = f.read() | |
json_data = json.loads(data) | |
posts = json_data['db'][0]['data']['posts'] | |
[save_post(prepare_post(post)) for post in posts] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment