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