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