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