Last active
April 14, 2017 11:03
-
-
Save michaelmcandrew/fc650529473538d14e7c3e3690ed3075 to your computer and use it in GitHub Desktop.
Convert (civicrm-docs) markdown to epub
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/env php | |
<?php | |
// composer require symfony/yaml (or incorperate it into civicrm-docs (or similar) to get this up and running. | |
require __DIR__ . '/vendor/autoload.php'; | |
if(!realpath($argv[1])){ | |
die("Could not find {$argv[1]}\n"); | |
} | |
$mkdocsFile = realpath($argv[1]); | |
$mkdocsDir = dirname($mkdocsFile); | |
$mkdocs = Symfony\Component\Yaml\Yaml::parse(file_get_contents($mkdocsFile)); | |
$flattened = []; | |
flatten($mkdocs['pages'], $flattened); | |
$epubMetadata = [ | |
'identifier' => $mkdocs['repo_url'], | |
'title' => $mkdocs['site_name'], | |
'creator' => $mkdocs['site_author'], | |
'date' => date('YYYY-MM-DD'), | |
'lang' => '', // TODO | |
'description' => $mkdocs['site_description'], | |
'rights' => '' // TODO we should require licenses on all books we publish | |
]; | |
$titleFile = '{$mkdocsDir}/docs/.civicrm-docs-epub-metadata'; | |
file_put_contents($titleFile, Symfony\Component\Yaml\Yaml::dump($epubMetadata)); | |
$outputFile = preg_replace('/\W+/', '-', strtolower($mkdocs['site_name'])) . '.epub' ; | |
foreach($flattened as $chapter){ | |
if($chapter['file']){ | |
$chapters[] = "{$mkdocsDir}/docs/{$chapter['file']}"; | |
} | |
} | |
$chapters = implode(' ', $chapters); | |
// Unfortunatley, I hit a roadblock with how pandoc handles relative URLs. When creating the epub, it produces lots of | |
// errors along the lines of `pandoc: Could not find media `../img/petition_new.png', skipping...` | |
// Basically, pandoc concats all the chapters and presumes that the URLs are relative to the first chapter that you added. | |
// This assumption is not valid when you have documentation in subdirectories (like the user and developer guide do). | |
// | |
// One solution is to use https://github.com/jgrassler/mkdocs-pandoc (which I discovered when I hit this roadblock | |
// and might make a lot of this script obselete. I coudn't get that lib to work though didn't spend too long trying. | |
// | |
// Another would be to do our own pre-processesing of the markdown or make a copy of the images where mkdocs would be able | |
// to find them. I did try and do that by symlinking everthing in docs/img to img in the hope that the ../ would then resolve | |
// but it didn't. | |
// | |
// So not an insurmountable problem, but something for another day... | |
exec("pandoc -S -o $outputFile $titleFile $chapters"); | |
unlink($titleFile); | |
// Functions | |
function flatten($section, &$flattened){ | |
foreach($section as $chapter){ | |
foreach($chapter as $title => $file){ | |
if(is_array($file)){ | |
$flattened[]=['title' => $title, 'file' => '']; | |
flatten($file, $flattened); | |
}else{ | |
$flattened[]=['title' => $title, 'file' => $file]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment