Nest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace dokuwiki\Parsing\Handler;
  3. /**
  4. * Generic call writer class to handle nesting of rendering instructions
  5. * within a render instruction. Also see nest() method of renderer base class
  6. *
  7. * @author Chris Smith <chris@jalakai.co.uk>
  8. */
  9. class Nest extends AbstractRewriter
  10. {
  11. protected $closingInstruction;
  12. /**
  13. * @inheritdoc
  14. *
  15. * @param CallWriterInterface $CallWriter the parser's current call writer, i.e. the one above us in the chain
  16. * @param string $close closing instruction name, this is required to properly terminate the
  17. * syntax mode if the document ends without a closing pattern
  18. */
  19. public function __construct(CallWriterInterface $CallWriter, $close = "nest_close")
  20. {
  21. parent::__construct($CallWriter);
  22. $this->closingInstruction = $close;
  23. }
  24. /** @inheritdoc */
  25. public function writeCall($call)
  26. {
  27. $this->calls[] = $call;
  28. }
  29. /** @inheritdoc */
  30. public function writeCalls($calls)
  31. {
  32. $this->calls = array_merge($this->calls, $calls);
  33. }
  34. /** @inheritdoc */
  35. public function finalise()
  36. {
  37. $last_call = end($this->calls);
  38. $this->writeCall(array($this->closingInstruction,array(), $last_call[2]));
  39. $this->process();
  40. $this->callWriter->finalise();
  41. unset($this->callWriter);
  42. }
  43. /** @inheritdoc */
  44. public function process()
  45. {
  46. // merge consecutive cdata
  47. $unmerged_calls = $this->calls;
  48. $this->calls = array();
  49. foreach ($unmerged_calls as $call) $this->addCall($call);
  50. $first_call = reset($this->calls);
  51. $this->callWriter->writeCall(array("nest", array($this->calls), $first_call[2]));
  52. return $this->callWriter;
  53. }
  54. /**
  55. * @param array $call
  56. */
  57. protected function addCall($call)
  58. {
  59. $key = count($this->calls);
  60. if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
  61. $this->calls[$key-1][1][0] .= $call[1][0];
  62. } elseif ($call[0] == 'eol') {
  63. // do nothing (eol shouldn't be allowed, to counter preformatted fix in #1652 & #1699)
  64. } else {
  65. $this->calls[] = $call;
  66. }
  67. }
  68. }