1#!/usr/bin/env php
2<?php
3
4trigger_error('The "bin/commonmark" command has been deprecated since league/commonmark 1.4', E_USER_DEPRECATED);
5
6requireAutoloader();
7
8ini_set('display_errors', 'stderr');
9
10$options = array();
11$options_raw = getopt('', array(
12    'use-asterisk',
13    'use-underscore',
14    'enable-strong',
15    'enable-em',
16    'safe',
17));
18foreach ($options_raw as $option => $value) {
19    switch ($option) {
20        case 'safe':
21            $options['html_input'] = 'strip';
22            $options['allow_unsafe_links'] = false;
23            break;
24        case 'use-asterisk':
25        case 'use-underscore':
26        case 'enable-strong':
27        case 'enable-em':
28            if ($value !== true && $value !== false) {
29                fail("Invalid value '$value' for option '$option'");
30            }
31            break;
32    }
33    $options[str_replace('-', '_', $option)] = $value;
34}
35
36foreach ($argv as $i => $arg) {
37    if ($i === 0) {
38        continue;
39    }
40
41    if (substr($arg, 0, 1) === '-') {
42        switch ($arg) {
43            case '-h':
44            case '--help':
45                echo getHelpText();
46                exit(0);
47            case '-v':
48            case '--version':
49                echo \League\CommonMark\CommonMarkConverter::VERSION . "\n";
50                exit(0);
51            case '--safe':
52                $options['safe'] = true;
53                break;
54            default:
55                $option = explode('=', $arg, 2)[0];
56                if (!preg_match('/^--[^-]/', $option)
57                    || !array_key_exists(ltrim($option, '-'), $options_raw)) {
58                    fail('Unknown option: ' . $arg);
59                }
60        }
61    } else {
62        $src = $arg;
63    }
64}
65
66if (isset($src)) {
67    if (!file_exists($src)) {
68        fail('File not found: ' . $src);
69    }
70
71    $markdown = file_get_contents($src);
72} else {
73    $stdin = fopen('php://stdin', 'r');
74
75    if (stream_set_blocking($stdin, false)) {
76        $markdown = stream_get_contents($stdin);
77    }
78
79    fclose($stdin);
80
81    if (empty($markdown)) {
82        fail(getHelpText());
83    }
84}
85
86$converter = new \League\CommonMark\CommonMarkConverter($options);
87echo $converter->convertToHtml($markdown);
88
89/**
90 * Get help and usage info
91 *
92 * @return string
93 */
94function getHelpText()
95{
96    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
97        return <<<WINDOWSHELP
98CommonMark - Markdown done right
99
100Usage: commonmark [OPTIONS] [FILE]
101
102    -h, --help    Shows help and usage information
103
104    -v, --version Shows the currently installed version
105
106    (Reading data from STDIN is not currently supported on Windows)
107
108Examples:
109
110    Converting a file named document.md:
111
112        commonmark document.md
113
114    Converting a file and saving its output:
115
116        commonmark document.md > output.html
117
118Full documentation can be found at http://commonmark.thephpleague.com/
119WINDOWSHELP;
120    }
121
122    return <<<HELP
123CommonMark - Markdown done right
124
125Usage: commonmark [OPTIONS] [FILE]
126
127    -h, --help    Shows help and usage information
128
129    -v, --version Shows the currently installed version
130
131    --safe        Escapes all raw HTML input and removes unsafe URLs
132
133    If no file is given, input will be read from STDIN
134
135Examples:
136
137    Converting a file named document.md:
138
139        commonmark document.md
140
141    Converting a file and saving its output:
142
143        commonmark document.md > output.html
144
145    Converting from STDIN:
146
147        echo -e '# Hello World!' | commonmark
148
149    Converting from STDIN and saving the output:
150
151        echo -e '# Hello World!' | commonmark > output.html
152
153Full documentation can be found at http://commonmark.thephpleague.com/
154
155HELP;
156}
157
158/**
159 * @param string $message Error message
160 */
161function fail($message)
162{
163    fwrite(STDERR, $message . "\n");
164    exit(1);
165}
166
167function requireAutoloader()
168{
169    $autoloadPaths = [
170        // Local package usage
171        __DIR__ . '/../vendor/autoload.php',
172        // Package was included as a library
173        __DIR__ . '/../../../autoload.php',
174    ];
175    foreach ($autoloadPaths as $path) {
176        if (file_exists($path)) {
177            require_once $path;
178            break;
179        }
180    }
181}
182