1*be906b56SAndreas Gohr<?php 2*be906b56SAndreas Gohr 3*be906b56SAndreas Gohrnamespace dokuwiki\Parsing\Handler; 4*be906b56SAndreas Gohr 5*be906b56SAndreas Gohrclass Quote implements ReWriterInterface 6*be906b56SAndreas Gohr{ 7*be906b56SAndreas Gohr 8*be906b56SAndreas Gohr /** @var CallWriterInterface original CallWriter */ 9*be906b56SAndreas Gohr protected $callWriter; 10*be906b56SAndreas Gohr 11*be906b56SAndreas Gohr protected $calls = array(); 12*be906b56SAndreas Gohr 13*be906b56SAndreas Gohr protected $quoteCalls = array(); 14*be906b56SAndreas Gohr 15*be906b56SAndreas Gohr /** @inheritdoc */ 16*be906b56SAndreas Gohr public function __construct(CallWriterInterface $CallWriter) 17*be906b56SAndreas Gohr { 18*be906b56SAndreas Gohr $this->callWriter = $CallWriter; 19*be906b56SAndreas Gohr } 20*be906b56SAndreas Gohr 21*be906b56SAndreas Gohr /** @inheritdoc */ 22*be906b56SAndreas Gohr public function writeCall($call) 23*be906b56SAndreas Gohr { 24*be906b56SAndreas Gohr $this->calls[] = $call; 25*be906b56SAndreas Gohr } 26*be906b56SAndreas Gohr 27*be906b56SAndreas Gohr /** 28*be906b56SAndreas Gohr * @inheritdoc 29*be906b56SAndreas Gohr * 30*be906b56SAndreas Gohr * Probably not needed but just in case... 31*be906b56SAndreas Gohr */ 32*be906b56SAndreas Gohr public function writeCalls($calls) 33*be906b56SAndreas Gohr { 34*be906b56SAndreas Gohr $this->calls = array_merge($this->calls, $calls); 35*be906b56SAndreas Gohr } 36*be906b56SAndreas Gohr 37*be906b56SAndreas Gohr /** @inheritdoc */ 38*be906b56SAndreas Gohr public function finalise() 39*be906b56SAndreas Gohr { 40*be906b56SAndreas Gohr $last_call = end($this->calls); 41*be906b56SAndreas Gohr $this->writeCall(array('quote_end',array(), $last_call[2])); 42*be906b56SAndreas Gohr 43*be906b56SAndreas Gohr $this->process(); 44*be906b56SAndreas Gohr $this->callWriter->finalise(); 45*be906b56SAndreas Gohr unset($this->callWriter); 46*be906b56SAndreas Gohr } 47*be906b56SAndreas Gohr 48*be906b56SAndreas Gohr /** @inheritdoc */ 49*be906b56SAndreas Gohr public function process() 50*be906b56SAndreas Gohr { 51*be906b56SAndreas Gohr 52*be906b56SAndreas Gohr $quoteDepth = 1; 53*be906b56SAndreas Gohr 54*be906b56SAndreas Gohr foreach ($this->calls as $call) { 55*be906b56SAndreas Gohr switch ($call[0]) { 56*be906b56SAndreas Gohr 57*be906b56SAndreas Gohr /** @noinspection PhpMissingBreakStatementInspection */ 58*be906b56SAndreas Gohr case 'quote_start': 59*be906b56SAndreas Gohr $this->quoteCalls[] = array('quote_open',array(),$call[2]); 60*be906b56SAndreas Gohr // fallthrough 61*be906b56SAndreas Gohr case 'quote_newline': 62*be906b56SAndreas Gohr $quoteLength = $this->getDepth($call[1][0]); 63*be906b56SAndreas Gohr 64*be906b56SAndreas Gohr if ($quoteLength > $quoteDepth) { 65*be906b56SAndreas Gohr $quoteDiff = $quoteLength - $quoteDepth; 66*be906b56SAndreas Gohr for ($i = 1; $i <= $quoteDiff; $i++) { 67*be906b56SAndreas Gohr $this->quoteCalls[] = array('quote_open',array(),$call[2]); 68*be906b56SAndreas Gohr } 69*be906b56SAndreas Gohr } elseif ($quoteLength < $quoteDepth) { 70*be906b56SAndreas Gohr $quoteDiff = $quoteDepth - $quoteLength; 71*be906b56SAndreas Gohr for ($i = 1; $i <= $quoteDiff; $i++) { 72*be906b56SAndreas Gohr $this->quoteCalls[] = array('quote_close',array(),$call[2]); 73*be906b56SAndreas Gohr } 74*be906b56SAndreas Gohr } else { 75*be906b56SAndreas Gohr if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]); 76*be906b56SAndreas Gohr } 77*be906b56SAndreas Gohr 78*be906b56SAndreas Gohr $quoteDepth = $quoteLength; 79*be906b56SAndreas Gohr 80*be906b56SAndreas Gohr break; 81*be906b56SAndreas Gohr 82*be906b56SAndreas Gohr case 'quote_end': 83*be906b56SAndreas Gohr if ($quoteDepth > 1) { 84*be906b56SAndreas Gohr $quoteDiff = $quoteDepth - 1; 85*be906b56SAndreas Gohr for ($i = 1; $i <= $quoteDiff; $i++) { 86*be906b56SAndreas Gohr $this->quoteCalls[] = array('quote_close',array(),$call[2]); 87*be906b56SAndreas Gohr } 88*be906b56SAndreas Gohr } 89*be906b56SAndreas Gohr 90*be906b56SAndreas Gohr $this->quoteCalls[] = array('quote_close',array(),$call[2]); 91*be906b56SAndreas Gohr 92*be906b56SAndreas Gohr $this->callWriter->writeCalls($this->quoteCalls); 93*be906b56SAndreas Gohr break; 94*be906b56SAndreas Gohr 95*be906b56SAndreas Gohr default: 96*be906b56SAndreas Gohr $this->quoteCalls[] = $call; 97*be906b56SAndreas Gohr break; 98*be906b56SAndreas Gohr } 99*be906b56SAndreas Gohr } 100*be906b56SAndreas Gohr 101*be906b56SAndreas Gohr return $this->callWriter; 102*be906b56SAndreas Gohr } 103*be906b56SAndreas Gohr 104*be906b56SAndreas Gohr protected function getDepth($marker) 105*be906b56SAndreas Gohr { 106*be906b56SAndreas Gohr preg_match('/>{1,}/', $marker, $matches); 107*be906b56SAndreas Gohr $quoteLength = strlen($matches[0]); 108*be906b56SAndreas Gohr return $quoteLength; 109*be906b56SAndreas Gohr } 110*be906b56SAndreas Gohr} 111