#!/usr/bin/env php ['cebe\\markdown\\GithubMarkdown', __DIR__ . '/../GithubMarkdown.php'], 'extra' => ['cebe\\markdown\\MarkdownExtra', __DIR__ . '/../MarkdownExtra.php'], ]; $full = false; $src = []; foreach($argv as $k => $arg) { if ($k == 0) { continue; } if ($arg[0] == '-') { $arg = explode('=', $arg); switch($arg[0]) { case '--flavor': if (isset($arg[1])) { if (isset($flavors[$arg[1]])) { require($flavors[$arg[1]][1]); $flavor = $flavors[$arg[1]][0]; } else { error("Unknown flavor: " . $arg[1], "usage"); } } else { error("Incomplete argument --flavor!", "usage"); } break; case '--full': $full = true; break; case '-h': case '--help': echo "PHP Markdown to HTML converter\n"; echo "------------------------------\n\n"; echo "by Carsten Brandt \n\n"; usage(); break; default: error("Unknown argument " . $arg[0], "usage"); } } else { $src[] = $arg; } } if (empty($src)) { $markdown = file_get_contents("php://stdin"); } elseif (count($src) == 1) { $file = reset($src); if (!file_exists($file)) { error("File does not exist:" . $file); } $markdown = file_get_contents($file); } else { error("Converting multiple files is not yet supported.", "usage"); } /** @var cebe\markdown\Parser $md */ $md = new $flavor(); $markup = $md->parse($markdown); if ($full) { echo << $markup HTML; } else { echo $markup; } // functions /** * Display usage information */ function usage() { global $argv; $cmd = $argv[0]; echo <<] [--full] [file.md] --flavor specifies the markdown flavor to use. If omitted the original markdown by John Gruber [1] will be used. Available flavors: gfm - Github flavored markdown [2] extra - Markdown Extra [3] --full ouput a full HTML page with head and body. If not given, only the parsed markdown will be output. --help shows this usage information. If no file is specified input will be read from STDIN. Examples: Render a file with original markdown: $cmd README.md > README.html Render a file using gihtub flavored markdown: $cmd --flavor=gfm README.md > README.html Convert the original markdown description to html using STDIN: curl http://daringfireball.net/projects/markdown/syntax.text | $cmd > md.html [1] http://daringfireball.net/projects/markdown/syntax [2] https://help.github.com/articles/github-flavored-markdown [3] http://michelf.ca/projects/php-markdown/extra/ EOF; exit(1); } /** * Send custom error message to stderr * @param $message string * @param $callback mixed called before script exit * @return void */ function error($message, $callback = null) { $fe = fopen("php://stderr", "w"); fwrite($fe, "Error: " . $message . "\n"); if (is_callable($callback)) { call_user_func($callback); } exit(1); }