Quote.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace dokuwiki\Parsing\Handler;
  3. class Quote extends AbstractRewriter
  4. {
  5. protected $quoteCalls = array();
  6. /** @inheritdoc */
  7. public function finalise()
  8. {
  9. $last_call = end($this->calls);
  10. $this->writeCall(array('quote_end',array(), $last_call[2]));
  11. $this->process();
  12. $this->callWriter->finalise();
  13. unset($this->callWriter);
  14. }
  15. /** @inheritdoc */
  16. public function process()
  17. {
  18. $quoteDepth = 1;
  19. foreach ($this->calls as $call) {
  20. switch ($call[0]) {
  21. /** @noinspection PhpMissingBreakStatementInspection */
  22. case 'quote_start':
  23. $this->quoteCalls[] = array('quote_open',array(),$call[2]);
  24. // fallthrough
  25. case 'quote_newline':
  26. $quoteLength = $this->getDepth($call[1][0]);
  27. if ($quoteLength > $quoteDepth) {
  28. $quoteDiff = $quoteLength - $quoteDepth;
  29. for ($i = 1; $i <= $quoteDiff; $i++) {
  30. $this->quoteCalls[] = array('quote_open',array(),$call[2]);
  31. }
  32. } elseif ($quoteLength < $quoteDepth) {
  33. $quoteDiff = $quoteDepth - $quoteLength;
  34. for ($i = 1; $i <= $quoteDiff; $i++) {
  35. $this->quoteCalls[] = array('quote_close',array(),$call[2]);
  36. }
  37. } else {
  38. if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
  39. }
  40. $quoteDepth = $quoteLength;
  41. break;
  42. case 'quote_end':
  43. if ($quoteDepth > 1) {
  44. $quoteDiff = $quoteDepth - 1;
  45. for ($i = 1; $i <= $quoteDiff; $i++) {
  46. $this->quoteCalls[] = array('quote_close',array(),$call[2]);
  47. }
  48. }
  49. $this->quoteCalls[] = array('quote_close',array(),$call[2]);
  50. $this->callWriter->writeCalls($this->quoteCalls);
  51. break;
  52. default:
  53. $this->quoteCalls[] = $call;
  54. break;
  55. }
  56. }
  57. return $this->callWriter;
  58. }
  59. /**
  60. * @param string $marker
  61. * @return int
  62. */
  63. protected function getDepth($marker)
  64. {
  65. preg_match('/>{1,}/', $marker, $matches);
  66. $quoteLength = strlen($matches[0]);
  67. return $quoteLength;
  68. }
  69. }