summaryrefslogtreecommitdiff
path: root/platform/www/inc/Parsing/Handler/Quote.php
blob: 74861b1b2f7cb5ee99173ab29bd844d821346f95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace dokuwiki\Parsing\Handler;

class Quote extends AbstractRewriter
{
    protected $quoteCalls = array();

    /** @inheritdoc */
    public function finalise()
    {
        $last_call = end($this->calls);
        $this->writeCall(array('quote_end',array(), $last_call[2]));

        $this->process();
        $this->callWriter->finalise();
        unset($this->callWriter);
    }

    /** @inheritdoc */
    public function process()
    {

        $quoteDepth = 1;

        foreach ($this->calls as $call) {
            switch ($call[0]) {

                /** @noinspection PhpMissingBreakStatementInspection */
                case 'quote_start':
                    $this->quoteCalls[] = array('quote_open',array(),$call[2]);
                    // fallthrough
                case 'quote_newline':
                    $quoteLength = $this->getDepth($call[1][0]);

                    if ($quoteLength > $quoteDepth) {
                        $quoteDiff = $quoteLength - $quoteDepth;
                        for ($i = 1; $i <= $quoteDiff; $i++) {
                            $this->quoteCalls[] = array('quote_open',array(),$call[2]);
                        }
                    } elseif ($quoteLength < $quoteDepth) {
                        $quoteDiff = $quoteDepth - $quoteLength;
                        for ($i = 1; $i <= $quoteDiff; $i++) {
                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
                        }
                    } else {
                        if ($call[0] != 'quote_start') $this->quoteCalls[] = array('linebreak',array(),$call[2]);
                    }

                    $quoteDepth = $quoteLength;

                    break;

                case 'quote_end':
                    if ($quoteDepth > 1) {
                        $quoteDiff = $quoteDepth - 1;
                        for ($i = 1; $i <= $quoteDiff; $i++) {
                            $this->quoteCalls[] = array('quote_close',array(),$call[2]);
                        }
                    }

                    $this->quoteCalls[] = array('quote_close',array(),$call[2]);

                    $this->callWriter->writeCalls($this->quoteCalls);
                    break;

                default:
                    $this->quoteCalls[] = $call;
                    break;
            }
        }

        return $this->callWriter;
    }

    /**
     * @param string $marker
     * @return int
     */
    protected function getDepth($marker)
    {
        preg_match('/>{1,}/', $marker, $matches);
        $quoteLength = strlen($matches[0]);
        return $quoteLength;
    }
}