Created
October 31, 2014 03:03
-
-
Save Alanaktion/16a3bdf7619d5fcea4d9 to your computer and use it in GitHub Desktop.
Ghost to Jekyll with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
if (empty($argv[1])) { | |
die("Filename parameter is required." . PHP_EOL . "Example: ./ghost2jekyll.php GhostData.json" . PHP_EOL); | |
} | |
@mkdir("_drafts"); | |
@mkdir("_posts"); | |
$json = json_decode(file_get_contents($argv[1])) or die("Failed to load file {$argv[1]}"); | |
$tags = array(); | |
foreach($json->data->tags as $tag) | |
$tags[$tag->id] = $tag->slug; | |
$ptags = array(); | |
foreach($json->data->posts_tags as $ptag) { | |
if(!isset($ptags[$ptag->post_id])) | |
$ptags[$ptag->post_id] = array(); | |
$ptags[$ptag->post_id][] = $tags[$ptag->tag_id]; | |
} | |
foreach($json->data->posts as $post) { | |
$fm = "---\nlayout: post\ntitle: {$post->title}\ndate: " . date("Y-m-d H:i:s", $post->published_at / 1000) . "\n"; | |
if(isset($ptags[$post->id])) | |
$fm .= "tags: " .implode(" ", $ptags[$post->id]) . "\n"; | |
if($post->image) | |
$fm .= "image: {$post->image}\n"; | |
$fm .= "---"; | |
if($post->status == "draft") { | |
$fn = "_drafts/{$post->slug}.md"; | |
} else { | |
$fn = "_posts/" . date("Y-m-d", $post->published_at / 1000) . "-{$post->slug}.md"; | |
} | |
file_put_contents($fn, "$fm\n$post->markdown"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems to work fairly well, I was able to convert http://blog.phpizza.com to http://alanaktion.github.io/blog-test/ quite easily.