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.
 
 
 
 
 

81 lines
2.2 KiB

  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([$this->closingInstruction, [], $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 = [];
  49. foreach ($unmerged_calls as $call) $this->addCall($call);
  50. $first_call = reset($this->calls);
  51. $this->callWriter->writeCall(["nest", [$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 && $call[0] == 'cdata' && $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. }