Skip to content

Instantly share code, notes, and snippets.

@breard-r
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save breard-r/59ee332bee550f3abcf5 to your computer and use it in GitHub Desktop.

Select an option

Save breard-r/59ee332bee550f3abcf5 to your computer and use it in GitHub Desktop.
Helper script to migrate blog posts from octopress to pelican.
#!/bin/env python
##
## Copyright (c) 2014 Rodolphe Breard
##
## Permission to use, copy, modify, and/or distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
##
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
##
INPUT_DIR = 'source/_posts/'
OUTPUT_DIR = 'content/'
ARTICLE_PATTERN = '*.markdown'
METADATA_SEPARATOR = '---\n'
DATETIME_FORMAT = '%Y-%m-%d %H:%M'
import distutils.core
import datetime
import shlex
import glob
import os
def strip_quotes(s):
return s.strip('"\'')
def date_convert(s):
return datetime.datetime.strptime(s, DATETIME_FORMAT)
def list_convert(s):
lst = [e.strip(',') for e in shlex.split(s.strip('[]'))]
return ', '.join(lst)
def status_conversion(s):
return 'published' if distutils.util.strtobool(s) else 'draft'
class Article:
_metadatas = {
# original_name: (new_name, content_conversion_function),
'title': ('Title', strip_quotes),
'date': ('Date', date_convert),
'categories': ('Tags', list_convert),
'published': ('Status', status_conversion),
}
def __init__(self, path):
self.file_name = os.path.basename(path)
self.metadatas = [('Slug', os.path.splitext(self.file_name[11:])[0])]
self.initial_path = path
self.new_path = os.path.join(OUTPUT_DIR, self.file_name)
self.load_data()
def load_data(self):
with open(self.initial_path, 'r') as f:
_, *content = f.readlines()
split_index = content.index(METADATA_SEPARATOR)
for line in [line.strip() for line in content[:split_index]]:
self._set_metadata(line)
self.content = ''.join(content[split_index + 1:])
def _set_metadata(self, line):
split_index = line.index(':')
name, content = line[:split_index].strip(), line[split_index + 1:].strip()
if name in self._metadatas:
new_name, func = self._metadatas[name]
self.metadatas.append((new_name, func(content)))
def save(self):
with open(self.new_path, 'w') as f:
for name, content in self.metadatas:
f.write('%s: %s\n' % (name, content))
f.write('\n%s' % self.content)
if __name__ == '__main__':
for article_path in glob.glob(os.path.join(INPUT_DIR, ARTICLE_PATTERN)):
article = Article(article_path)
article.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment