You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

202 lines
6.7 KiB

  1. <?php
  2. namespace dokuwiki\Parsing\Handler;
  3. /**
  4. * Handler for paragraphs
  5. *
  6. * @author Harry Fuecks <hfuecks@gmail.com>
  7. */
  8. class Block
  9. {
  10. protected $calls = [];
  11. protected $skipEol = false;
  12. protected $inParagraph = false;
  13. // Blocks these should not be inside paragraphs
  14. protected $blockOpen = [
  15. 'header', 'listu_open', 'listo_open', 'listitem_open', 'listcontent_open', 'table_open', 'tablerow_open',
  16. 'tablecell_open', 'tableheader_open', 'tablethead_open', 'quote_open', 'code', 'file', 'hr', 'preformatted',
  17. 'rss', 'footnote_open'
  18. ];
  19. protected $blockClose = [
  20. 'header', 'listu_close', 'listo_close', 'listitem_close', 'listcontent_close', 'table_close',
  21. 'tablerow_close', 'tablecell_close', 'tableheader_close', 'tablethead_close', 'quote_close', 'code', 'file',
  22. 'hr', 'preformatted', 'rss', 'footnote_close'
  23. ];
  24. // Stacks can contain paragraphs
  25. protected $stackOpen = ['section_open'];
  26. protected $stackClose = ['section_close'];
  27. /**
  28. * Constructor. Adds loaded syntax plugins to the block and stack
  29. * arrays
  30. *
  31. * @author Andreas Gohr <andi@splitbrain.org>
  32. */
  33. public function __construct()
  34. {
  35. global $DOKU_PLUGINS;
  36. //check if syntax plugins were loaded
  37. if (empty($DOKU_PLUGINS['syntax'])) return;
  38. foreach ($DOKU_PLUGINS['syntax'] as $n => $p) {
  39. $ptype = $p->getPType();
  40. if ($ptype == 'block') {
  41. $this->blockOpen[] = 'plugin_' . $n;
  42. $this->blockClose[] = 'plugin_' . $n;
  43. } elseif ($ptype == 'stack') {
  44. $this->stackOpen[] = 'plugin_' . $n;
  45. $this->stackClose[] = 'plugin_' . $n;
  46. }
  47. }
  48. }
  49. protected function openParagraph($pos)
  50. {
  51. if ($this->inParagraph) return;
  52. $this->calls[] = ['p_open', [], $pos];
  53. $this->inParagraph = true;
  54. $this->skipEol = true;
  55. }
  56. /**
  57. * Close a paragraph if needed
  58. *
  59. * This function makes sure there are no empty paragraphs on the stack
  60. *
  61. * @author Andreas Gohr <andi@splitbrain.org>
  62. *
  63. * @param string|integer $pos
  64. */
  65. protected function closeParagraph($pos)
  66. {
  67. if (!$this->inParagraph) return;
  68. // look back if there was any content - we don't want empty paragraphs
  69. $content = '';
  70. $ccount = count($this->calls);
  71. for ($i = $ccount - 1; $i >= 0; $i--) {
  72. if ($this->calls[$i][0] == 'p_open') {
  73. break;
  74. } elseif ($this->calls[$i][0] == 'cdata') {
  75. $content .= $this->calls[$i][1][0];
  76. } else {
  77. $content = 'found markup';
  78. break;
  79. }
  80. }
  81. if (trim($content) == '') {
  82. //remove the whole paragraph
  83. //array_splice($this->calls,$i); // <- this is much slower than the loop below
  84. for (
  85. $x = $ccount; $x > $i;
  86. $x--
  87. ) array_pop($this->calls);
  88. } else {
  89. // remove ending linebreaks in the paragraph
  90. $i = count($this->calls) - 1;
  91. if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0], "\n");
  92. $this->calls[] = ['p_close', [], $pos];
  93. }
  94. $this->inParagraph = false;
  95. $this->skipEol = true;
  96. }
  97. protected function addCall($call)
  98. {
  99. $key = count($this->calls);
  100. if ($key && $call[0] == 'cdata' && $this->calls[$key - 1][0] == 'cdata') {
  101. $this->calls[$key - 1][1][0] .= $call[1][0];
  102. } else {
  103. $this->calls[] = $call;
  104. }
  105. }
  106. // simple version of addCall, without checking cdata
  107. protected function storeCall($call)
  108. {
  109. $this->calls[] = $call;
  110. }
  111. /**
  112. * Processes the whole instruction stack to open and close paragraphs
  113. *
  114. * @author Harry Fuecks <hfuecks@gmail.com>
  115. * @author Andreas Gohr <andi@splitbrain.org>
  116. *
  117. * @param array $calls
  118. *
  119. * @return array
  120. */
  121. public function process($calls)
  122. {
  123. // open first paragraph
  124. $this->openParagraph(0);
  125. foreach ($calls as $key => $call) {
  126. $cname = $call[0];
  127. if ($cname == 'plugin') {
  128. $cname = 'plugin_' . $call[1][0];
  129. $plugin = true;
  130. $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
  131. $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
  132. } else {
  133. $plugin = false;
  134. }
  135. /* stack */
  136. if (in_array($cname, $this->stackClose) && (!$plugin || $plugin_close)) {
  137. $this->closeParagraph($call[2]);
  138. $this->storeCall($call);
  139. $this->openParagraph($call[2]);
  140. continue;
  141. }
  142. if (in_array($cname, $this->stackOpen) && (!$plugin || $plugin_open)) {
  143. $this->closeParagraph($call[2]);
  144. $this->storeCall($call);
  145. $this->openParagraph($call[2]);
  146. continue;
  147. }
  148. /* block */
  149. // If it's a substition it opens and closes at the same call.
  150. // To make sure next paragraph is correctly started, let close go first.
  151. if (in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
  152. $this->closeParagraph($call[2]);
  153. $this->storeCall($call);
  154. $this->openParagraph($call[2]);
  155. continue;
  156. }
  157. if (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
  158. $this->closeParagraph($call[2]);
  159. $this->storeCall($call);
  160. continue;
  161. }
  162. /* eol */
  163. if ($cname == 'eol') {
  164. // Check this isn't an eol instruction to skip...
  165. if (!$this->skipEol) {
  166. // Next is EOL => double eol => mark as paragraph
  167. if (isset($calls[$key + 1]) && $calls[$key + 1][0] == 'eol') {
  168. $this->closeParagraph($call[2]);
  169. $this->openParagraph($call[2]);
  170. } else {
  171. //if this is just a single eol make a space from it
  172. $this->addCall(['cdata', ["\n"], $call[2]]);
  173. }
  174. }
  175. continue;
  176. }
  177. /* normal */
  178. $this->addCall($call);
  179. $this->skipEol = false;
  180. }
  181. // close last paragraph
  182. $call = end($this->calls);
  183. $this->closeParagraph($call[2]);
  184. return $this->calls;
  185. }
  186. }