'New blog' or bye bye Wordpress.

It has been two years and a half using Wordpress in my personal blog. To be honest it works pretty well, you can install new themes easily, the comment moderation is quite easy and even it updates itself automatically. But from time to time a change is required and now is the turn for the static webpage generator Pelican. Its a quite interesting project if you ask me. You basically write down your posts in a Markup language and it generates a static webpage with a theme of your election.

Advantatges? It can be hosted in AWS S3 as a static site. Or can be server by a Nginx or Apache with the minumum memory footprint. The site loads blazing fast. You don't have security bugs. You don't have to update your code because there is none. To update your blog you can write down your posts, see them locally and then use rsync to upload it to production.

Disadvantages. Basically you loose the comments. To be honest with your my blog was not very commented and its a drowback I can deal with. If anybody has a question can always send me an email.

The migration has been kinda painful. Not just because when you export your blog in Wordpress the funny characters such as '*' '#', '_' are scaped like that: * # _ . To solve the problem I created a sed expression that worked for me:

sed -i -re 's/\\([#$_~^<>*`])/\1/g' *.md

Using this expression you remove the scape character.

Another problem has been the code blocks defined in my posts. I used a plugin and I sourraunded the code blocks with this tags:

[bash]
$ echo "this is an example of codeblock"
[/bash]

The problem being that Pelican does not recognise this format and need something similar to:

    :::bash
    $ echo "this is an example of codeblock"

To fix that I've done a simple script in Python to modify all the posts files to the new codeblock version. It basically find codeblocks with the tokens [bash], [python] or [code] and replaces it accordingly. Here is the script:

#!/usr/bin/env python

import re
import sys
import os.path


TOKENS = ('bash', 'python', 'code')


def find_end_codeblock(line):
    return find_codeblock(line, "[/%s]")

def find_codeblock(line, pattern="[%s]"):
    for token in TOKENS:
        if pattern % token in line:
            return token

def indent_codeblocks(filename):
    try:
        f = open(filename, 'r')
    except:
        print "Error while processing %s. Skipping." % filename
        return
    in_codeblock, final_output = False, ""
    for line in f.readlines():
        if in_codeblock:
           line = "\t%s" % line
           if find_end_codeblock(line):
               in_codeblock = False
               line = ""

        else:
            token = find_codeblock(line)
            if token:
                in_codeblock = True
                line = "\t:::%s\n" % token

        final_output += line
    f.close()

    if in_codeblock:
        print "Error! The file %s has an unclosed codeblock. Ignoring this file." % filename
        return

    with open(filename, 'w') as f:
        f.write(final_output)



for filename in sys.argv[1:]:
    print "Processing %s..." % filename
    indent_codeblocks(filename)

print "Done"

Not happy with that I've checked out each post manually to fix some minor language typos, and done minor style fixes. After some days, I'm proudly announcing that I'm running under Pelican.