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.
 
 
 
 
 

87 lines
2.5 KiB

  1. <?php
  2. namespace dokuwiki\Parsing\Handler;
  3. class Quote extends AbstractRewriter
  4. {
  5. protected $quoteCalls = [];
  6. /** @inheritdoc */
  7. public function finalise()
  8. {
  9. $last_call = end($this->calls);
  10. $this->writeCall(['quote_end', [], $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[] = ['quote_open', [], $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[] = ['quote_open', [], $call[2]];
  31. }
  32. } elseif ($quoteLength < $quoteDepth) {
  33. $quoteDiff = $quoteDepth - $quoteLength;
  34. for ($i = 1; $i <= $quoteDiff; $i++) {
  35. $this->quoteCalls[] = ['quote_close', [], $call[2]];
  36. }
  37. } elseif ($call[0] != 'quote_start') {
  38. $this->quoteCalls[] = ['linebreak', [], $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[] = ['quote_close', [], $call[2]];
  47. }
  48. }
  49. $this->quoteCalls[] = ['quote_close', [], $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. }