Skip to content

Instantly share code, notes, and snippets.

@Alanaktion
Created October 31, 2014 03:03
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 Alanaktion/16a3bdf7619d5fcea4d9 to your computer and use it in GitHub Desktop.
Save Alanaktion/16a3bdf7619d5fcea4d9 to your computer and use it in GitHub Desktop.
Ghost to Jekyll with PHP
#!/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");
}
@Alanaktion
Copy link
Author

Seems to work fairly well, I was able to convert http://blog.phpizza.com to http://alanaktion.github.io/blog-test/ quite easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment