markdown.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php namespace HashOver;
  2. // Copyright (C) 2015-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class Markdown
  27. {
  28. public $blockCodeRegex = '/```([\s\S]+?)```/';
  29. protected $paragraphRegex = '/(?:\r\n|\r|\n){2}/';
  30. public $inlineCodeRegex = '/(^|[^a-z0-9`])`([^`]+?[\s\S]+?)`([^a-z0-9`]|$)/i';
  31. // Array for inline code and code block markers
  32. protected $codeMarkers = array (
  33. 'block' => array ('marks' => array (), 'count' => 0),
  34. 'inline' => array ('marks' => array (), 'count' => 0)
  35. );
  36. // Markdown patterns to search for
  37. public $search = array (
  38. '/\*\*([^ *])([\s\S]+?)([^ *])\*\*/',
  39. '/\*([^ *])([\s\S]+?)([^ *])\*/',
  40. '/(^|\W)_([^_]+?[\s\S]+?)_(\W|$)/',
  41. '/__([^ _])([\s\S]+?)([^ _])__/',
  42. '/~~([^ ~])([\s\S]+?)([^ ~])~~/'
  43. );
  44. // HTML replacements for markdown patterns
  45. public $replace = array (
  46. '<strong>\\1\\2\\3</strong>',
  47. '<em>\\1\\2\\3</em>',
  48. '\\1<u>\\2</u>\\3',
  49. '<u>\\1\\2\\3</u>',
  50. '<s>\\1\\2\\3</s>'
  51. );
  52. // Replaces markdown for inline code with a marker
  53. protected function codeReplace ($grp, $display)
  54. {
  55. $markName = 'CODE_' . strtoupper ($display);
  56. $markCount = $this->codeMarkers[$display]['count']++;
  57. if ($display !== 'block') {
  58. $codeMarker = $grp[1] . $markName . '[' . $markCount . ']' . $grp[3];
  59. $this->codeMarkers[$display]['marks'][$markCount] = trim ($grp[2], "\r\n");
  60. } else {
  61. $codeMarker = $markName . '[' . $markCount . ']';
  62. $this->codeMarkers[$display]['marks'][$markCount] = trim ($grp[1], "\r\n");
  63. }
  64. return $codeMarker;
  65. }
  66. // Replaces markdown for code block with a marker
  67. protected function blockCodeReplace ($grp)
  68. {
  69. return $this->codeReplace ($grp, 'block');
  70. }
  71. // Replaces markdown for inline code with a marker
  72. protected function inlineCodeReplace ($grp)
  73. {
  74. return $this->codeReplace ($grp, 'inline');
  75. }
  76. // Returns the original inline markdown code with HTML replacement
  77. protected function inlineCodeReturn ($grp)
  78. {
  79. return '<code class="hashover-inline">' . $this->codeMarkers['inline']['marks'][($grp[1])] . '</code>';
  80. }
  81. // Returns the original markdown code block with HTML replacement
  82. protected function blockCodeReturn ($grp)
  83. {
  84. return '<code>' . $this->codeMarkers['block']['marks'][($grp[1])] . '</code>';
  85. }
  86. // Parses a string as markdown
  87. public function parseMarkdown ($string)
  88. {
  89. // Reset marker arrays
  90. $this->codeMarkers = array (
  91. 'block' => array ('marks' => array (), 'count' => 0),
  92. 'inline' => array ('marks' => array (), 'count' => 0)
  93. );
  94. // Replace code blocks with markers
  95. $string = preg_replace_callback ($this->blockCodeRegex, 'self::blockCodeReplace', $string);
  96. // Break string into paragraphs
  97. $paragraphs = preg_split ($this->paragraphRegex, $string);
  98. // Run through each paragraph
  99. for ($i = 0, $il = count ($paragraphs); $i < $il; $i++) {
  100. // Replace inline code with markers
  101. $paragraphs[$i] = preg_replace_callback ($this->inlineCodeRegex, 'self::inlineCodeReplace', $paragraphs[$i]);
  102. // Replace markdown patterns
  103. $paragraphs[$i] = preg_replace ($this->search, $this->replace, $paragraphs[$i]);
  104. // Replace markers with original markdown code
  105. $paragraphs[$i] = preg_replace_callback ('/CODE_INLINE\[([0-9]+)\]/', 'self::inlineCodeReturn', $paragraphs[$i]);
  106. }
  107. // Join paragraphs
  108. $string = implode (PHP_EOL . PHP_EOL, $paragraphs);
  109. // Replace code block markers with original markdown code
  110. $string = preg_replace_callback ('/CODE_BLOCK\[([0-9]+)\]/', 'self::blockCodeReturn', $string);
  111. return $string;
  112. }
  113. }