From Pelican to Hugo

15 March 2016

The transition from Pelican to Hugo was pretty straightforward. The important labour saving device was a python script that I used to convert the posts from the Pelican format to the Hugo format. For those who are interested, here is the script. It’s not so smart, but it did the job,

import os

old_dir = 'C:/Websites-source/petersmith-blog/content/'
new_dir = 'C:/Websites-source/petersmith/content/blog/'

files = os.listdir(old_dir)

for f in files:
    if f.endswith('.md'):
    finished_header = False
    with open(old_dir+f, 'r') as in_f:
        with open(new_dir + f, 'w') as out_f:
        lines = in_f.readlines()
        for line in lines:
            if finished_header:
            elif line.startswith('Title: '):
            _, title = line.split(':', 1)
            title = title.strip()
            title = title.replace('"', "'")
            out_f.write('title = "'+title+'"\n')
            elif line.startswith('Date: '):
            _, date = line.split(':', 1)
            date = date.strip()
            date = date.replace(" ", "T")
            date = date[:16]
            out_f.write('date = "'+date+':00+12:00"\n')
            elif line.startswith('Author: '):
                elif line.startswith('Summary: '):
                elif line.startswith('Slug: '):
                    _, slug = line.split(':', 1)
                    slug = slug.strip()
                    out_f.write('slug = "'+slug+'"\n')
                elif line.startswith('Category: '):
                    _, tags = line.split(':', 1)
                    tags = tags.strip().split(',')
                    tags = ['"'+tag.strip()+'"' for tag in tags]
                    tags = ', '.join(tags)
                    out_f.write('categories = ['+tags+']\n')
                elif line.startswith('Tags: '):
                    out_f.write('type = "post"\n')
                elif line.strip() == "":
                    out_f.write('draft = "False"\n')
                    finished_header = True

This worked pretty well. I then manually moved the static files—and in particular the image files—to the static location for Hugo. In my case that was ~/petersmith/static/images. When I was using Pelican, I used the {attach} directive to locate image files in the right place. For Hugo, that’s not needed, so I used a quick search and replace in VIM to fix-up the files.

For completeness, here is the configuration file (config.toml) that I am using.

baseurl = ""
languageCode = "en-uk"
title = "The occassional ramblings of an academic"
theme = "academe"

canonifyURLs =  true
pygmentsuseclasses =  true

  category =  "categories"

    identifier = "publications"
    name       = "Publications"
    url        = "/publications/"
    weight     = 1

    identifier = "journal"
    name       = "journal"
    url        = "/journal/"
    weight     = 2

    identifier = "resources"
    name       = "resources"
    url        = "/resources/"
    weight     = 3

    identifier = "teaching"
    name       = "teaching"
    url        = "/teaching/"
    weight     = 4

    identifier = "blog"
    name       = "blog"
    url        = "/blog/"
    weight     = 5

  blog = "/blog/:year/:month/:day/:slug"
  journal = "/journal/:year/:month/:day/:slug"
  teaching = "/teaching/:year/:month/:slug"

That was pretty much it. It too some pratting around to get the dual blog aspect of the site working (blog posts and journal posts) and I have tried to keep the links the same so the search engines don’t get too upset. It took me a while to figure out how to get the archive pages working correctly, but there is a great resource at Parsia’s Den.

So, what is left to do. First, I need to set up the posts for all of my publications teaching. I don’t have that stored away anywhere (in a form that would be easy to convert), so that will take some time to do.

The other time consuming operation is to add in all my teaching. Again, I don’t have that in a form that is readily convertible/accessible. That too will take some time.

But, the site works and is pretty current (once I go-live with it).