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