Block.php 6.7 KB

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