Migrating Posts From Wordpress to Hugo
By jjm
After a rather long editing effort and also needing to clean up Flickr (to get into the new free tier limits), last month I completed the migration of content from Wordpress (minus the odd or deleted Flickr photo post’s) to my now not so new Hugo static site.
While part of this was scripted, by writing out the markdown files for each post from the XML export via this not so pretty, but functional script:
import xmltodict
from jinja2 import Template
with open('geekyandblonde-wordpress.xml') as fd:
old_blog = xmltodict.parse(fd.read())
template = Template("""---
title: "{{ title }}"
date: {{ date }}
draft: {{ draft }}
aliases:
- {{ link }}
---
{{ content }}
""")
for post in old_blog["rss"]["channel"]["item"]:
hugo_post_file = f"content/posts/migrated/{post['wp:post_name']}.md"
hugo_post = template.render(
title=post["title"],
date=post['pubDate'],
link=post['link'].replace('http://URL', ''),
draft=True,
content=post["content:encoded"],
)
print(hugo_post_file)
with open(hugo_post_file, "w") as new_post:
new_post.write(hugo_post)
Then followed rather a lot of editing and changing HTML markup to Markdown. In the coming year (2021), I do hope to post to the blog more than before (i.e. at least couple of times a year). So far this is only the 3rd non-migrated post!