Moving from WordPress to Hugo

I’ve recently moved my Wordpress blog to Hugo, a static website generator.

Migration

I initially used the Wordpress Jekyll Exporter to export all of my content from Wordpress into something which Hugo can then import. This command worked rather well hugo import jekyll wordpress_export new_hugo_blog.

I then also wrote a script to move the files to the right directory layout:

cd content
for f in `find . -name "20*.md"`; do
    name=`echo "$f" | cut -c19-`
    year=`echo "$f" | cut -c3-6`
    month=`echo "$f" | cut -c13-14`
    day=`echo "$f" | cut -c16-17`
    dir="$year/$month/$day"
    mkdir -p "$dir"
    mv -v "$f" "$dir/$name"
done

This took the exported markdown files and moved them to a folder structure which matches my permalinks i.e. yyyy/mm/dd/post-name

Tags

I wanted to see all the tags that I’d used, so I put together another little script:

grep --no-filename -r -A8 "tags:" . | sed -e '/^--/d' -e '/^[^-]/d' -e '/^$/d' | sort | uniq

This finds the “tags:” line (in the YAML front matter at the start of each post) then prints the following 8 lines (I don’t think I have any posts with more than about 5 tags) then sorts and uniq’s the list. Who needs a Database!

Or you could do:

hugo && ls -l public/tags/

Theme

I’ve based my theme on one of the many excellent Hugo themes. After an initial steep learning curve the themes are now far easier than WordPress themes to edit, especially as I’ve tried to keep my theme as simple as possible.

Writing

To ease the editing of files, I’ve written a script to find and edit files:

find content -name "*$1*.md" -exec vim {} +

This is much faster than the admin interface in Wordpress! I can simply type ./find-post.sh hugo and it will open all the posts which have hugo in their name.

Publishing

To make publishing even easier I’ve created a simple Makefile:

all: clean deploy

deploy: site
    sudo rsync --delete-before -av public/ /var/www/blog/
    sudo chown www-data:www-data /var/www/blog -R

site:
    hugo

clean:
    rm -r public/*

I then just type make all and it creates a fresh copy of the site and publishes it!

Conclusion

So far so good, the website is many times faster and no longer requires any php! It is also far nicer to write in markdown in a nice text editor than use the WordPress editor. I also store everything in git so that I also have history and it’s immediately backed up.