Skip to content

Instantly share code, notes, and snippets.

@mubashariqbal
Created July 31, 2012 17:22
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mubashariqbal/892c0f0a7c89b1a2396b to your computer and use it in GitHub Desktop.
Save mubashariqbal/892c0f0a7c89b1a2396b to your computer and use it in GitHub Desktop.
Quick hack to convert Wordpress Export XML File to Frontloaded YAML Markdown
<?php
// USAGE: php convert.php
// Edit the details below to your neds
$wp_file = 'data/wp.xml';
$export_folder = 'content/'; // existing files will be over-written use with care
if (file_exists($wp_file)) {
$xml = simplexml_load_file($wp_file);
$count = 0;
foreach ($xml->channel->item as $item) {
$count ++;
print "Exporting: (".$count.") " . $item->title."\n";
$title = $item->title;
$item_date = strtotime($item->pubDate);
$file_name = $export_folder.date("Y-m-d", $item_date)."-".slugify($title).".md";
if ($title == '') {
$title = 'untitled post';
}
print " -- filename: ".$file_name;
$markdown = "---\n";
$markdown .= "title: '" . $title ."'\n";
$markdown .= "---\n";
$markdown .= $item->children('content', true)->encoded;
$markdown .= "\n";
file_put_contents($file_name, $markdown);
print "\n";
}
}
// credit: http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment