Created
September 7, 2012 22:20
-
-
Save nfreear/3670216 to your computer and use it in GitHub Desktop.
Run PHP-Markdown-Extra, optionally adding automatic IDs and contents table / (c) N.D.Freear
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
<?php | |
/** | |
* Run PHP-Markdown[-Extra] [-x], optionally adding automatic IDs [-a], | |
* and a table of contents [-ac]. | |
* | |
* @copyright 2012-09-07 N.D.Freear. | |
*/ | |
if (php_sapi_name() != 'cli') { | |
_error('must be in CLI mode.'); | |
} | |
if ($argc < 2) { | |
_error('missing argument. Usage: php md.php [-x|-a|-ac] input.md > output.html'); | |
} | |
$filename = $argv[$argc - 1]; | |
$input = $interm = file_get_contents($filename); | |
$flag_x = $argv[1]; | |
$flag = substr($flag_x, 0, 2); | |
#if ($argc > 2) { | |
switch ($flag) { | |
case '-a': | |
_log('Auto-ID mode'); | |
$interm = Mde_Auto_Ids::parse($input); | |
if ('-ac' == $flag_x) { | |
_log('Auto-ID-with-TOC mode'); | |
$interm = Mde_Auto_Ids::render_contents() . $interm; | |
} | |
case '-x': // Fall-through. | |
_log('PHP-Markdown-Extra mode'); | |
require_once 'php-markdown-extra-1.2.5/markdown.php'; | |
break; | |
case '-h': // Help | |
_error('help!'); | |
break; | |
default: | |
if ($argc > 2) { | |
_error("unrecognized flag '$flag'."); | |
} | |
_log('PHP-Markdown mode'); | |
require_once 'php-markdown-1.0.1o/markdown.php'; | |
break; | |
} | |
$output = Markdown($interm); | |
if ('-ac' == $flag_x) { | |
$output = Mde_Auto_Ids::post_contents($output); | |
} | |
fwrite(STDOUT, $output); | |
exit (0); | |
// -------------------------------------------------------- | |
// Functions. | |
class Mde_Auto_Ids { | |
protected static $contents = array(); | |
public static function parse($input) { | |
$toc_r = array(); | |
$callback = function ($matches) use (&$toc_r) { | |
$level = strlen($matches[1]); | |
// "My Heading 1.1" becomes, id='my-heading-1_1' | |
$head = trim($matches[2]); | |
$id = str_replace(' ', '-', strtolower($head)); | |
// Hmm, we'd prefer to replace '%' with '.', but PHP-Markdown no like! | |
$id = str_replace(array('%', '.'), '_', urlencode($id)); | |
$output = rtrim($matches[0], "\r\n") .' {#'. $id .'}' .PHP_EOL; | |
$toc_r[] = (object) array( | |
'head' => $head, | |
'id' => $id, | |
'level' => $level, | |
); | |
return $output; | |
}; | |
// End callback. | |
$output = preg_replace_callback( | |
'/(#+)([^#]+?)\n/', | |
$callback, #'_auto_ids_callback', | |
$input); | |
self::$contents = $toc_r; | |
#var_dump($toc_r); | |
return $output; | |
} | |
public static function render_contents() { | |
$toc = '{TOC}' .PHP_EOL.PHP_EOL; | |
$toc .= '## Contents' .PHP_EOL.PHP_EOL; | |
foreach (self::$contents as $it) { | |
$R = $it->level > 1 ? ($it->level - 1) * 4 - 1 : NULL; | |
$toc .= $R ? str_repeat(' ', $R) : ''; | |
$toc .= '* ['. $it->head .'](#'. $it->id .')' .PHP_EOL; | |
} | |
$toc .= PHP_EOL. '{/TOC}' .PHP_EOL.PHP_EOL; | |
#var_dump($toc); | |
return $toc; | |
} | |
public static function post_contents($input) { | |
return strtr($input, array( | |
'<p>{TOC}</p>' => '<div id="toc">', | |
'<p>{/TOC}</p>'=> '</div>', | |
)); | |
} | |
} | |
function _log($msg) { | |
fwrite(STDERR, '> '. $msg .PHP_EOL); | |
} | |
function _error($msg) { | |
fwrite(STDERR, 'Fatal error, '. $msg .PHP_EOL); | |
exit (1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment