1#!/usr/bin/env php
2<?php
3/**
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
7 */
8
9$composerAutoload = [
10	__DIR__ . '/../vendor/autoload.php', // standalone with "composer install" run
11	__DIR__ . '/../../../autoload.php',  // script is installed as a composer binary
12];
13foreach ($composerAutoload as $autoload) {
14	if (file_exists($autoload)) {
15		require($autoload);
16		break;
17	}
18}
19
20// Send all errors to stderr
21ini_set('display_errors', 'stderr');
22
23$flavor = 'cebe\\markdown\\Markdown';
24$flavors = [
25	'gfm'   => ['cebe\\markdown\\GithubMarkdown', __DIR__ . '/../GithubMarkdown.php'],
26	'extra' => ['cebe\\markdown\\MarkdownExtra',  __DIR__ . '/../MarkdownExtra.php'],
27];
28
29$full = false;
30$src = [];
31foreach($argv as $k => $arg) {
32	if ($k == 0) {
33		continue;
34	}
35	if ($arg[0] == '-') {
36		$arg = explode('=', $arg);
37		switch($arg[0]) {
38			case '--flavor':
39				if (isset($arg[1])) {
40					if (isset($flavors[$arg[1]])) {
41						require($flavors[$arg[1]][1]);
42						$flavor = $flavors[$arg[1]][0];
43					} else {
44						error("Unknown flavor: " . $arg[1], "usage");
45					}
46				} else {
47					error("Incomplete argument --flavor!", "usage");
48				}
49			break;
50			case '--full':
51				$full = true;
52			break;
53			case '-h':
54			case '--help':
55				echo "PHP Markdown to HTML converter\n";
56				echo "------------------------------\n\n";
57				echo "by Carsten Brandt <mail@cebe.cc>\n\n";
58				usage();
59			break;
60			default:
61				error("Unknown argument " . $arg[0], "usage");
62		}
63	} else {
64		$src[] = $arg;
65	}
66}
67
68if (empty($src)) {
69	$markdown = file_get_contents("php://stdin");
70} elseif (count($src) == 1) {
71	$file = reset($src);
72	if (!file_exists($file)) {
73		error("File does not exist:" . $file);
74	}
75	$markdown = file_get_contents($file);
76} else {
77	error("Converting multiple files is not yet supported.", "usage");
78}
79
80/** @var cebe\markdown\Parser $md */
81$md = new $flavor();
82$markup = $md->parse($markdown);
83
84if ($full) {
85	echo <<<HTML
86<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
87	"http://www.w3.org/TR/html4/loose.dtd">
88<html>
89<head>
90	<meta http-equiv="content-type" content="text/html; charset=utf-8">
91	<style>
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; }
97	</style>
98</head>
99<body>
100$markup
101</body>
102</html>
103HTML;
104} else {
105	echo $markup;
106}
107
108// functions
109
110/**
111 * Display usage information
112 */
113function usage() {
114	global $argv;
115	$cmd = $argv[0];
116	echo <<<EOF
117Usage:
118    $cmd [--flavor=<flavor>] [--full] [file.md]
119
120    --flavor  specifies the markdown flavor to use. If omitted the original markdown by John Gruber [1] will be used.
121              Available flavors:
122
123              gfm   - Github flavored markdown [2]
124              extra - Markdown Extra [3]
125
126    --full    ouput a full HTML page with head and body. If not given, only the parsed markdown will be output.
127
128    --help    shows this usage information.
129
130    If no file is specified input will be read from STDIN.
131
132Examples:
133
134    Render a file with original markdown:
135
136        $cmd README.md > README.html
137
138    Render a file using gihtub flavored markdown:
139
140        $cmd --flavor=gfm README.md > README.html
141
142    Convert the original markdown description to html using STDIN:
143
144        curl http://daringfireball.net/projects/markdown/syntax.text | $cmd > md.html
145
146
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/
150
151EOF;
152	exit(1);
153}
154
155/**
156 * Send custom error message to stderr
157 * @param $message string
158 * @param $callback mixed called before script exit
159 * @return void
160 */
161function error($message, $callback = null) {
162	$fe = fopen("php://stderr", "w");
163	fwrite($fe, "Error: " . $message . "\n");
164
165	if (is_callable($callback)) {
166		call_user_func($callback);
167	}
168
169	exit(1);
170}
171