Back in 2015, my best friend was going through a rough patch, a divorce, alone 600 miles away from home. We were talking quite a lot back in the day and I decided that I should brighten her day for a while, by sending her a “good mood” song each morning. For this, I’ve set up a blog on wordpress.com, because it was the simplest and fastest way of doing that, and it would also allow me to keep track of songs, prepare a few in advance when I was too busy to do my daily research.
Her situation improved gradually over time, and at some point I’ve considered that my plan was a success and I stopped posting there, and after a few years, I’ve decided to make said blog private, out of privacy concerns. Today, I needed to check something into my wordpress.com account (which I don’t actually use at all), and I remembered about this blog, and I thought that it would be nice not to revive it, but at least take that content and bring it to my website, rather than to have them lost forever.
Therefore I decided to write a simple script to convert the standard Wordpress export file to simple MD files to be used in Hugo, so if you want to use the script below, it might need some tweaking to match your setup. I coded this using PHP, because I know it best: wp2hugo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<?php
$xml_file = file_get_contents('./import/goodmoods.xml');
// we're using the next line to convert tags like <content:encoded> into <content_encoded>, so we can access them in PHP easier. There's a small issue where the timestamps are messed up, but we'll fix that with a simple str_replace around line 200-ish.
$xml_file = preg_replace('~(</?|\s)([a-z0-9_]+):~is', '$1$2_', $xml_file);
$xml = simplexml_load_string($xml_file) or die("Error: Cannot create object");
foreach($xml->channel->item as $item) {
if($item->{'wp_status'} == 'publish') {
$content = trim($item->{'content_encoded'});
// this part is a bit ugly, where we're converting youtube URLs to {{ < youtube VIDEOID > }} embed shortcodes
$content = str_replace('https://www.youtube.com/watch?v=', '{{ < youtube ', $content);
$content = str_replace('http://www.youtube.com/watch?v=', '{{ < youtube ', $content);
$content = str_replace('https://youtu.be/', '{{ < youtube ', $content);
$content = str_replace('http://youtu.be/', '{{ < youtube ', $content);
$content = str_replace('http://youtu.be/', '{{ < youtube ', $content);
$content .= ' >}}';
$slug = $item->wp_post_name;
echo 'content/quickies/'.$slug.'.md' . "\n";
$title = explode(' - ', $item->title); // we break the title using the general ARTIST - TRACKNAME format
$output = '---
title: "' . $item->title . '"
date: ' . date('c', strtotime(str_replace("_", ":", $item->pubDate))) . '
draft: false
section: "music"
hidetitle: false
tags: [
"'. trim($title[0]) .'",
"music",
]
---
' . $content .'
';
// uncomment this line if you want to preview the .md file before it's generated
// echo $output;
// Checking if the file exists. If so, we don't overwrite it, but make a new one.
if(file_exists('content/quickies/'.$slug.'.md')) {
$slug = $slug . '-' . time();
}
// uncomment the block below to really write the files to the disk
/*
if(file_put_contents('content/quickies/'.$slug.'.md', $output)) {
echo "Event File 'content/quickies/".$slug.".md' created successfully\n";
}
*/
}
}
?>
|
The code is fairly simple, as I didn’t use tags, categories or other features, but you’re free to expand this as much as you want.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
---
title: "Röyksopp - Happy Up Here"
date: 2015-03-24T22:07:55+00:00
draft: false
section: "music"
hidetitle: false
tags: [
"Röyksopp",
"music",
"good moods",
"goodmoods",
]
---
{{ < youtube 51Bpx63wkbA > }}
|
In the end, if you want to see all the posted songs, just check the tag page for #good-moods!