Skip to content

Instantly share code, notes, and snippets.

@mseri
Created May 16, 2015 23:12
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 mseri/8fb565a13f3b6a14fe38 to your computer and use it in GitHub Desktop.
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.
"""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