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