Skip to content

Instantly share code, notes, and snippets.

@vjeantet
Last active June 19, 2016 10:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vjeantet/d1f6cf824a2344dd6b4e to your computer and use it in GitHub Desktop.
Save vjeantet/d1f6cf824a2344dd6b4e to your computer and use it in GitHub Desktop.
Use this php script to convert a Ghost export json file to Hugo content file. Files will be created in a "output" folder, to reuse images place the ghost content folder into the hugo's static folder
<?php
$ghost_data = json_decode(file_get_contents("./GhostData.json"),true) ;
foreach ($ghost_data['data']['posts'] as $key => $value) {
$created_at = date("c",$value["created_at"]/1000);
$title = str_replace('\\','\\\\',$value["title"]) ;
$title = str_replace('"','\"',$title) ;
$slug = $value["slug"] ;
$markdown = $value["markdown"] ;
$draft = ($value["published_at"] == null) ? 'true' : 'false' ;
$published_at = ($value["published_at"] == null) ? date("c",$value["created_at"]/1000) : date("c",$value["published_at"]/1000); ;
$output_dir = "./output" ;
if (!is_dir($output_dir)) {
mkdir($output_dir);
}
$file = $output_dir."/".$slug.".md" ;
file_put_contents($file, '+++');
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'date = "' .$published_at.'"', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'draft = ' .$draft, FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'title = "' .$title.'"', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, 'slug = "' .$slug.'"', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, '+++', FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, "\n", FILE_APPEND);
file_put_contents($file, $markdown, FILE_APPEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment