xref: /dokuwiki/inc/Parsing/Handler/Preformatted.php (revision 84e1c54096731e0b1fea154e6d561e2bf083b667)
1<?php
2
3namespace dokuwiki\Parsing\Handler;
4
5class Preformatted implements ReWriterInterface
6{
7
8    /** @var CallWriterInterface original call writer */
9    protected $callWriter;
10
11    protected $calls = array();
12    protected $pos;
13    protected $text ='';
14
15    /**
16     * @inheritdoc
17     */
18    public function __construct(CallWriterInterface $CallWriter)
19    {
20        $this->callWriter = $CallWriter;
21    }
22
23    /** @inheritdoc */
24    public function writeCall($call)
25    {
26        $this->calls[] = $call;
27    }
28
29    /**
30     * @inheritdoc
31     * Probably not needed but just in case...
32     */
33    public function writeCalls($calls)
34    {
35        $this->calls = array_merge($this->calls, $calls);
36    }
37
38    /** @inheritdoc */
39    public function finalise()
40    {
41        $last_call = end($this->calls);
42        $this->writeCall(array('preformatted_end',array(), $last_call[2]));
43
44        $this->process();
45        $this->callWriter->finalise();
46        unset($this->callWriter);
47    }
48
49    /** @inheritdoc */
50    public function process()
51    {
52        foreach ($this->calls as $call) {
53            switch ($call[0]) {
54                case 'preformatted_start':
55                    $this->pos = $call[2];
56                    break;
57                case 'preformatted_newline':
58                    $this->text .= "\n";
59                    break;
60                case 'preformatted_content':
61                    $this->text .= $call[1][0];
62                    break;
63                case 'preformatted_end':
64                    if (trim($this->text)) {
65                        $this->callWriter->writeCall(array('preformatted', array($this->text), $this->pos));
66                    }
67                    // see FS#1699 & FS#1652, add 'eol' instructions to ensure proper triggering of following p_open
68                    $this->callWriter->writeCall(array('eol', array(), $this->pos));
69                    $this->callWriter->writeCall(array('eol', array(), $this->pos));
70                    break;
71            }
72        }
73
74        return $this->callWriter;
75    }
76}
77