commonmark 3.9 KB

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