/assets/php/markdown_extended.php

https://github.com/davidturner/Wee_ · PHP · 171 lines · 131 code · 25 blank · 15 comment · 4 complexity · eacc938d88028bea64289aa8b037f326 MD5 · raw file

  1. <?php
  2. /*
  3. from: https://github.com/egil/php-markdown-extra-extended
  4. */
  5. require_once('markdown.php');
  6. define( 'MARKDOWNEXTRAEXTENDED_VERSION', "0.3" );
  7. function MarkdownExtended($text, $default_claases = array()){
  8. $parser = new MarkdownExtraExtended_Parser($default_claases);
  9. return $parser->transform($text);
  10. }
  11. class MarkdownExtraExtended_Parser extends MarkdownExtra_Parser {
  12. # Tags that are always treated as block tags:
  13. var $block_tags_re = 'figure|figcaption|p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend';
  14. var $default_classes;
  15. function MarkdownExtraExtended_Parser($default_classes = array()) {
  16. $default_classes = $default_classes;
  17. $this->block_gamut += array(
  18. "doFencedFigures" => 7,
  19. );
  20. parent::MarkdownExtra_Parser();
  21. }
  22. function transform($text) {
  23. $text = parent::transform($text);
  24. return $text;
  25. }
  26. function doHardBreaks($text) {
  27. # Do hard breaks:
  28. # EXTENDED: changed to allow breaks without two spaces and just one new line
  29. # original code /* return preg_replace_callback('/ {2,}\n/', */
  30. return preg_replace_callback('/ *\n/',
  31. array(&$this, '_doHardBreaks_callback'), $text);
  32. }
  33. function doBlockQuotes($text) {
  34. $text = preg_replace_callback('/
  35. (?>^[ ]*>[ ]?
  36. (?:\((.+?)\))?
  37. [ ]*(.+\n(?:.+\n)*)
  38. )+
  39. /xm',
  40. array(&$this, '_doBlockQuotes_callback'), $text);
  41. return $text;
  42. }
  43. function _doBlockQuotes_callback($matches) {
  44. $cite = $matches[1];
  45. $bq = '> ' . $matches[2];
  46. # trim one level of quoting - trim whitespace-only lines
  47. $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq);
  48. $bq = $this->runBlockGamut($bq); # recurse
  49. $bq = preg_replace('/^/m', " ", $bq);
  50. # These leading spaces cause problem with <pre> content,
  51. # so we need to fix that:
  52. $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
  53. array(&$this, '_doBlockQuotes_callback2'), $bq);
  54. $res = "<blockquote";
  55. $res .= empty($cite) ? ">" : " cite=\"$cite\">";
  56. $res .= "\n$bq\n</blockquote>";
  57. return "\n". $this->hashBlock($res)."\n\n";
  58. }
  59. function doFencedCodeBlocks($text) {
  60. $less_than_tab = $this->tab_width;
  61. $text = preg_replace_callback('{
  62. (?:\n|\A)
  63. # 1: Opening marker
  64. (
  65. ~{3,}|`{3,} # Marker: three tilde or more.
  66. )
  67. [ ]?(\w+)?(?:,[ ]?(\d+))?[ ]* \n # Whitespace and newline following marker.
  68. # 3: Content
  69. (
  70. (?>
  71. (?!\1 [ ]* \n) # Not a closing marker.
  72. .*\n+
  73. )+
  74. )
  75. # Closing marker.
  76. \1 [ ]* \n
  77. }xm',
  78. array(&$this, '_doFencedCodeBlocks_callback'), $text);
  79. return $text;
  80. }
  81. function _doFencedCodeBlocks_callback($matches) {
  82. $codeblock = $matches[4];
  83. $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
  84. $codeblock = preg_replace_callback('/^\n+/',
  85. array(&$this, '_doFencedCodeBlocks_newlines'), $codeblock);
  86. //$codeblock = "<pre><code>$codeblock</code></pre>";
  87. //$cb = "<pre><code";
  88. $class="";
  89. if(!empty($matches[2])){
  90. $class="code";
  91. }
  92. $cb = empty($matches[3]) ? '<pre class="'.$class.'"><code' : '<pre class="linenums:'.$matches[3].' '.$class.'"><code';
  93. $cb .= empty($matches[2]) ? ">" : ' class="prettyprint language-'.$matches[2].' '.$matches[2].'">';
  94. $cb .= "$codeblock</code></pre>";
  95. return "\n\n".$this->hashBlock($cb)."\n\n";
  96. }
  97. function doFencedFigures($text){
  98. $text = preg_replace_callback('{
  99. (?:\n|\A)
  100. # 1: Opening marker
  101. (
  102. ={3,} # Marker: equal sign.
  103. )
  104. [ ]?(?:\[([^\]]+)\])?[ ]* \n # Whitespace and newline following marker.
  105. # 3: Content
  106. (
  107. (?>
  108. (?!\1 [ ]?(?:\[([^\]]+)\])?[ ]* \n) # Not a closing marker.
  109. .*\n+
  110. )+
  111. )
  112. # Closing marker.
  113. \1 [ ]?(?:\[([^\]]+)\])?[ ]* \n
  114. }xm', array(&$this, '_doFencedFigures_callback'), $text);
  115. return $text;
  116. }
  117. function _doFencedFigures_callback($matches) {
  118. # get figcaption
  119. $topcaption = empty($matches[2]) ? null : $this->runBlockGamut($matches[2]);
  120. $bottomcaption = empty($matches[4]) ? null : $this->runBlockGamut($matches[4]);
  121. $figure = $matches[3];
  122. $figure = $this->runBlockGamut($figure); # recurse
  123. $figure = preg_replace('/^/m', " ", $figure);
  124. # These leading spaces cause problem with <pre> content,
  125. # so we need to fix that - reuse blockqoute code to handle this:
  126. $figure = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
  127. array(&$this, '_doBlockQuotes_callback2'), $figure);
  128. $res = "<figure>";
  129. if(!empty($topcaption)){
  130. $res .= "\n<figcaption>$topcaption</figcaption>";
  131. }
  132. $res .= "\n$figure\n";
  133. if(!empty($bottomcaption) && empty($topcaption)){
  134. $res .= "<figcaption>$bottomcaption</figcaption>";
  135. }
  136. $res .= "</figure>";
  137. return "\n". $this->hashBlock($res)."\n\n";
  138. }
  139. }
  140. ?>