commonmark 4.1 KB

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