Skip to content

Instantly share code, notes, and snippets.

@popthestack
Created April 29, 2012 18:48
Show Gist options
  • Save popthestack/2552613 to your computer and use it in GitHub Desktop.
Save popthestack/2552613 to your computer and use it in GitHub Desktop.
Hacked together Wordpress -> Nanoc script
<?php
$db = mysql_connect('localhost', 'db_user', 'db_password');
mysql_select_db('blog');
$sql = "SELECT * FROM wp_posts WHERE post_status IN ('publish', 'private') AND post_type = 'post'";
$r = mysql_query($sql);
get_posts($r);
// do drafts separately in order to keep them from messing with published posts' URLs
$sql = "SELECT * FROM wp_posts WHERE post_status = 'draft' AND post_type = 'post'";
$r = mysql_query($sql);
get_posts($r);
function get_posts($r) {
while ($post = mysql_fetch_assoc($r)) {
$sql = "SELECT t.name, tt.taxonomy
FROM wp_terms t
LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id
LEFT JOIN wp_term_relationships tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id = " . $post['ID'] . " AND t.term_id != 1 AND taxonomy != 'link_category'";
$r2 = mysql_query($sql);
$tags_replace = array(
'straw-man-argument' => 'straw man',
'i-hate-permanent-head-songs' => 'head-song',
'About Me' => 'about me',
#'In the News' => 'news',
);
$cats_to_tags = array(
"Ryan's Life" => 'about me',
'In the News' => 'In the News',
);
$tags = array();
while ($tag = mysql_fetch_assoc($r2)) {
if (empty($tags[$tag['taxonomy']])) $tags[$tag['taxonomy']] = array();
if (isset($tags_replace[$tag['name']])) {
$tag['name'] = $tags_replace[$tag['name']];
}
if ($tag['taxonomy'] == 'category' && isset($cats_to_tags[$tag['name']])) {
$tag['name'] = $cats_to_tags[$tag['name']];
$tag['taxonomy'] = 'post_tag';
}
if (!empty($tag['name']) && (empty($tags[$tag['taxonomy']]) || !in_array($tag['name'], $tags[$tag['taxonomy']]))) {
$tags[$tag['taxonomy']][] = str_replace(array('_', '-'), ' ', $tag['name']);
}
}
// Fix youtube videos.
$preg_search = array(
'#<object width="([0-9]+)" height="([0-9]+)"><param name="movie" value="http://www.youtube.com/v/([^"]+)"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/([^"]+)" type="application/x-shockwave-flash" wmode="transparent" width="([0-9]+)" height="([0-9]+)"></embed></object>#U',
'#http://www.youtube.com/embed/([^&]+)&[^"]+"#U',
'#<object width="([0-9]+)" height="([0-9]+)"><param name="movie" value="http://www.youtube.com/v/([^"]+)"></param><embed src="http://www.youtube.com/v/([^"]+)" type="application/x-shockwave-flash" width="([0-9]+)" height="([0-9]+)"></embed></object>#U',
);
$preg_replace = array(
'<iframe title="YouTube video player" width="\1" height="\2" src="http://www.youtube.com/embed/\3" frameborder="0"></iframe>',
'http://www.youtube.com/embed/\1"',
'<iframe title="YouTube video player" width="\1" height="\2" src="http://www.youtube.com/embed/\3" frameborder="0"></iframe>',
);
// write file
$has_quote = strpos($post['post_title'], '"');
$file_contents = "---\n"
. "title: " . ($has_quote !== false ? "'" : '"') . $post['post_title'] . ($has_quote !== false ? "'" : '"') . "\n"
. "kind: article\n"
. "created_at: " . $post['post_date'] . "\n"
. "tags: [" . (!empty($tags['post_tag']) ? implode(', ', $tags['post_tag']) : '') . "]\n"
. "categories: [" . (!empty($tags['category']) ? implode(', ', $tags['category']) : '') . "]\n"
. "status: " . $post['post_status'] . "\n"
. "---\n\n"
. str_replace(array("\r", '*'), array("", '\*'), preg_replace($preg_search, $preg_replace, $post['post_content']));
// Get unique filename.
$post_name = strtolower(!empty($post['post_name']) ? $post['post_name'] : (!empty($post['post_title']) ? to_slug($post['post_title']) : 'draft'));
$i = 1;
do {
$filename = '/content/article/' . $post_name . ($i > 1 ? '-' . $i : '') . '.html';
$i++;
} while (file_exists(__DIR__ . $filename));
// write file
file_put_contents(__DIR__ . $filename, $file_contents);
}
}
function to_slug($s) {
$s = preg_replace('#[^A-Za-z0-9-]+#', '-', $s);
return trim(preg_replace('#-+#', '-', $s), '-');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment