Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active December 14, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satooshi/5023141 to your computer and use it in GitHub Desktop.
Save satooshi/5023141 to your computer and use it in GitHub Desktop.
Convert tumblr v1 xml response to Octopress markdown. See gist 5023226 if you would like to convert body.html to markdown (pandoc required).
<?php
$file = './tumblr.xml';
$xml = simplexml_load_file($file);
$posts = $xml->posts;
$root = "./_posts";
if (!is_dir($root)) {
mkdir($root);
}
foreach ($posts->post as $post) {
$id = (string)$post['id'];
$url = (string)$post['url-with-slug'];
$date = (string)$post['date'];
$slug = (string)$post['slug'];
$dt = new \DateTime($date);
$tag = array();
if (isset($post->tag)) {
foreach ($post->tag as $t) {
$tag[] = "$t";
}
}
$titleVar = "regular-title";
$title = isset($post->$titleVar) ? $post->$titleVar : null;
$title = "$title";
$bodyVar = "regular-body";
$body = isset($post->$bodyVar) ? $post->$bodyVar : null;
$body = "$body";
$dir = $root . "/$id";
if (!is_dir($dir)) {
mkdir($dir);
}
$metaFile = sprintf("%s-%s.markdown", $dt->format('Y-m-d'), $slug);
$meta = array(
'---',
'layout: post',
sprintf('title: "%s"', $title),
sprintf('date: %s', $dt->format('Y-m-d H:i')),
'comments: false',
sprintf('categories: [%s]', implode(', ', array_map(function($t){ return sprintf("'%s'", $t);}, $tag))),
'---',
);
file_put_contents($dir . "/" . $metaFile, implode("\n", $meta) . "\n");
$bodyFile = "body.html";
file_put_contents($dir . "/" . $bodyFile, $body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment