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