xref: /dokuwiki/inc/Parsing/Handler/Preformatted.php (revision 113171bb2c574564644d283fd1ae323072dfa865)
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 */
118c5fa126SAndreas Gohr    protected function getClosingCall(): string
12be906b56SAndreas Gohr    {
138c5fa126SAndreas Gohr        return 'preformatted_end';
14be906b56SAndreas Gohr    }
15be906b56SAndreas Gohr
16be906b56SAndreas Gohr    /** @inheritdoc */
17be906b56SAndreas Gohr    public function process()
18be906b56SAndreas Gohr    {
19be906b56SAndreas Gohr        foreach ($this->calls as $call) {
20be906b56SAndreas Gohr            switch ($call[0]) {
21be906b56SAndreas Gohr                case 'preformatted_start':
22be906b56SAndreas Gohr                    $this->pos = $call[2];
23be906b56SAndreas Gohr                    break;
24be906b56SAndreas Gohr                case 'preformatted_newline':
25be906b56SAndreas Gohr                    $this->text .= "\n";
26be906b56SAndreas Gohr                    break;
27be906b56SAndreas Gohr                case 'preformatted_content':
28be906b56SAndreas Gohr                    $this->text .= $call[1][0];
29be906b56SAndreas Gohr                    break;
30be906b56SAndreas Gohr                case 'preformatted_end':
31*113171bbSAndreas Gohr                    // Skip blocks whose only content is whitespace. For
32*113171bbSAndreas Gohr                    // the rest, strip leading/trailing newline runs
33be906b56SAndreas Gohr                    if (trim($this->text)) {
34*113171bbSAndreas Gohr                        $text = trim($this->text, "\n");
35*113171bbSAndreas Gohr                        $this->callWriter->writeCall(['preformatted', [$text], $this->pos]);
36be906b56SAndreas Gohr                    }
37be906b56SAndreas Gohr                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
38bcaec9f4SAndreas Gohr                    $this->callWriter->writeCall(['eol', [], $this->pos]);
39bcaec9f4SAndreas Gohr                    $this->callWriter->writeCall(['eol', [], $this->pos]);
40be906b56SAndreas Gohr                    break;
41be906b56SAndreas Gohr            }
42be906b56SAndreas Gohr        }
43be906b56SAndreas Gohr
44be906b56SAndreas Gohr        return $this->callWriter;
45be906b56SAndreas Gohr    }
46be906b56SAndreas Gohr}
47