4 * @copyright Copyright (c) 2014 Carsten Brandt
5 * @license https://github.com/cebe/markdown/blob/master/LICENSE
6 * @link https://github.com/cebe/markdown#readme
10 __DIR__ . '/../vendor/autoload.php', // standalone with "composer install" run
11 __DIR__ . '/../../../autoload.php', // script is installed as a composer binary
13 foreach ($composerAutoload as $autoload) {
14 if (file_exists($autoload)) {
20 // Send all errors to stderr
21 ini_set('display_errors', 'stderr');
23 $flavor = 'cebe\\markdown\\Markdown';
25 'gfm' => ['cebe\\markdown\\GithubMarkdown', __DIR__ . '/../GithubMarkdown.php'],
26 'extra' => ['cebe\\markdown\\MarkdownExtra', __DIR__ . '/../MarkdownExtra.php'],
31 foreach($argv as $k => $arg) {
36 $arg = explode('=', $arg);
40 if (isset($flavors[$arg[1]])) {
41 require($flavors[$arg[1]][1]);
42 $flavor = $flavors[$arg[1]][0];
44 error("Unknown flavor: " . $arg[1], "usage");
47 error("Incomplete argument --flavor!", "usage");
55 echo "PHP Markdown to HTML converter\n";
56 echo "------------------------------\n\n";
57 echo "by Carsten Brandt <mail@cebe.cc>\n\n";
61 error("Unknown argument " . $arg[0], "usage");
69 $markdown = file_get_contents("php://stdin");
70 } elseif (count($src) == 1) {
72 if (!file_exists($file)) {
73 error("File does not exist:" . $file);
75 $markdown = file_get_contents($file);
77 error("Converting multiple files is not yet supported.", "usage");
80 /** @var cebe\markdown\Parser $md */
82 $markup = $md->parse($markdown);
86 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
87 "http://www.w3.org/TR/html4/loose.dtd">
90 <meta http-equiv="content-type" content="text/html; charset=utf-8">
92 body { font-family: Arial, sans-serif; }
93 code { background: #eeeeff; padding: 2px; }
94 li { margin-bottom: 5px; }
95 img { max-width: 1200px; }
96 table, td, th { border: solid 1px #ccc; border-collapse: collapse; }
111 * Display usage information
118 $cmd [--flavor=<flavor>] [--full] [file.md]
120 --flavor specifies the markdown flavor to use. If omitted the original markdown by John Gruber [1] will be used.
123 gfm - Github flavored markdown [2]
124 extra - Markdown Extra [3]
126 --full ouput a full HTML page with head and body. If not given, only the parsed markdown will be output.
128 --help shows this usage information.
130 If no file is specified input will be read from STDIN.
134 Render a file with original markdown:
136 $cmd README.md > README.html
138 Render a file using gihtub flavored markdown:
140 $cmd --flavor=gfm README.md > README.html
142 Convert the original markdown description to html using STDIN:
144 curl http://daringfireball.net/projects/markdown/syntax.text | $cmd > md.html
147 [1] http://daringfireball.net/projects/markdown/syntax
148 [2] https://help.github.com/articles/github-flavored-markdown
149 [3] http://michelf.ca/projects/php-markdown/extra/
156 * Send custom error message to stderr
157 * @param $message string
158 * @param $callback mixed called before script exit
161 function error($message, $callback = null) {
162 $fe = fopen("php://stderr", "w");
163 fwrite($fe, "Error: " . $message . "\n");
165 if (is_callable($callback)) {
166 call_user_func($callback);