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