From Wordpress to Orgmode and Jekyll

Quick description of the move from Wordpress to Orgmode with Org-jekyll, Jekyll and Disqus.

Backup your blog

Full description. If you have an account in your blog's server and a mysql setup in your computer you can do something like this:

ssh -l user bloghost.com \
       "mysqldump -u wpuser -p\"wppassword\" --opt wpdatabase > yourblog.sql"
rsync -azv -e 'ssh -l user' bloghost.com:yourblog.sql ./
mysql -u wpuser -p"wppassword" wpdatabase < yourblog.sql
mkdir uploads
rsync -azv -e 'ssh -l user' bloghost.com:path-to-blog/wp-content-uploads/ \
       uploads/

(Replacing bloghost, wpuser, wppassword and wpdatabase with your correct values).

Convert your posts to Jekyll

Follow the instructions here. You will end up with a directory full of files with names like

2009-12-02-on-org-mode-and-team-management.markdown

that require some cleanup (see below).

Set yourself up at Disqus

If you already have a Disqus account you'll probably need to add a site.

Rescue your comments from Wordpress

Go to the plugins page at your blog's admin pages, select "Add New" under the Plugins menu at your left, and install "Disqus comment system". If your setup is anythin like mine this will not work. Instead download from here and follow the instructions here.

From the plugins control panel activate the Disqus plugin, and follow the instructions to configure it. Then go to Advanced Options and Import comments into Disqus. In a short while your comments will be in Disqus.

Cleanup your posts and convert to orgmode

There are several things that you might want to do if you want to store your posts in org-mode format: convert your links, possibly your images, and store a permalink property with the exact permalink that the entry used to have in Wordpress. You will need this to ensure that your Jekyll posts end up with the same permalink (which in turn you need to ensure that links to your posts don't die, and that Disqus does not lose your comments). This hack might be of help:

import sys, re
from datetime import *

for fi in sys.argv[1:]:
    fo = fi.replace('.markdown', '.html')
    fin = open(fi, 'r')
    fout = open(fo, 'w')
    props = {}
    front_matter = True
    for line in fin.readlines()[1:]:
        line = line.replace('\r', '')
        if front_matter:
            line = line.strip()
            if line == '---':
                front_matter = False
                fout.write('* ' + props['title'] + "\n")
                fout.write("  :PROPERTIES:\n")

                date = fi.split('-')
                date = datetime(int(date[0]), int(date[1]), int(date[2]))
                day = ['Mon', 'Tue', 'Wed',
                       'Thu', 'Fri', 'Sat', 'Sun'][date.weekday()]
                fout.write("  :on: <%d-%d-%d %s>\n" % (date.year, date.month,
                                                       date.day, day))
                for (k, v) in props.items():
                    if k != 'title':
                        fout.write("  :%s: %s\n" % (k, v))
                fout.write("  :END:\n")
            else:
                try:
                    propline = line.split(': ')
                    props[propline[0]] = ': '.join(propline[1:])
                except:
                    print fi
                    print line
                    raise
        else:
            line = line.replace("<blockquote>", "#+begin_quote\n")
            line = line.replace("</blockquote>", "\n#+end_quote")
            line = re.sub(r'<a href=\'(.+?)\'>(.+?)</a>', r'[[\1][\2]]', line)
            line = re.sub(r'<a href="(.+?)">(.+?)</a>', r'[[\1][\2]]', line)
            line = re.sub(r'<li>(.+)</li>', r'  - \1', line)
            line = line.replace('<b>', '*')
            line = line.replace('</b>', '*')
            line = line.replace('<i>', '/')
            line = line.replace('</i>', '/')
            line = re.sub(r'<img src="http://juntoalcamino.com/wp-content/uploads/(.+?)" (.+?)/>', r'  #+attr_html: \2\n  [[file:/img/\1]]\n', line)
            fout.write(line)
    fin.close()
    fout.close()
Juan Reyero Barcelona, 2008-04-06
 

blog comments powered by Disqus