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