xref: /dokuwiki/inc/Parsing/Handler/Preformatted.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\Handler;
4be906b56SAndreas Gohr
5533aca44SAndreas Gohrclass Preformatted extends AbstractRewriter
6be906b56SAndreas Gohr{
7be906b56SAndreas Gohr    protected $pos;
8be906b56SAndreas Gohr    protected $text = '';
9be906b56SAndreas Gohr
10be906b56SAndreas Gohr    /** @inheritdoc */
11be906b56SAndreas Gohr    public function finalise()
12be906b56SAndreas Gohr    {
13be906b56SAndreas Gohr        $last_call = end($this->calls);
14*bcaec9f4SAndreas Gohr        $this->writeCall(['preformatted_end', [], $last_call[2]]);
15be906b56SAndreas Gohr
16be906b56SAndreas Gohr        $this->process();
17be906b56SAndreas Gohr        $this->callWriter->finalise();
18be906b56SAndreas Gohr        unset($this->callWriter);
19be906b56SAndreas Gohr    }
20be906b56SAndreas Gohr
21be906b56SAndreas Gohr    /** @inheritdoc */
22be906b56SAndreas Gohr    public function process()
23be906b56SAndreas Gohr    {
24be906b56SAndreas Gohr        foreach ($this->calls as $call) {
25be906b56SAndreas Gohr            switch ($call[0]) {
26be906b56SAndreas Gohr                case 'preformatted_start':
27be906b56SAndreas Gohr                    $this->pos = $call[2];
28be906b56SAndreas Gohr                    break;
29be906b56SAndreas Gohr                case 'preformatted_newline':
30be906b56SAndreas Gohr                    $this->text .= "\n";
31be906b56SAndreas Gohr                    break;
32be906b56SAndreas Gohr                case 'preformatted_content':
33be906b56SAndreas Gohr                    $this->text .= $call[1][0];
34be906b56SAndreas Gohr                    break;
35be906b56SAndreas Gohr                case 'preformatted_end':
36be906b56SAndreas Gohr                    if (trim($this->text)) {
37*bcaec9f4SAndreas Gohr                        $this->callWriter->writeCall(['preformatted', [$this->text], $this->pos]);
38be906b56SAndreas Gohr                    }
39be906b56SAndreas Gohr                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
40*bcaec9f4SAndreas Gohr                    $this->callWriter->writeCall(['eol', [], $this->pos]);
41*bcaec9f4SAndreas Gohr                    $this->callWriter->writeCall(['eol', [], $this->pos]);
42be906b56SAndreas Gohr                    break;
43be906b56SAndreas Gohr            }
44be906b56SAndreas Gohr        }
45be906b56SAndreas Gohr
46be906b56SAndreas Gohr        return $this->callWriter;
47be906b56SAndreas Gohr    }
48be906b56SAndreas Gohr}
49