您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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