xref: /dokuwiki/inc/Parsing/Handler/Block.php (revision be906b566b9bdfd92c032ee07c4fd077d820a8d1)
1*be906b56SAndreas Gohr<?php
2*be906b56SAndreas Gohr
3*be906b56SAndreas Gohrnamespace dokuwiki\Parsing\Handler;
4*be906b56SAndreas Gohr
5*be906b56SAndreas Gohr/**
6*be906b56SAndreas Gohr * Handler for paragraphs
7*be906b56SAndreas Gohr *
8*be906b56SAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com>
9*be906b56SAndreas Gohr */
10*be906b56SAndreas Gohrclass Block
11*be906b56SAndreas Gohr{
12*be906b56SAndreas Gohr    protected $calls = array();
13*be906b56SAndreas Gohr    protected $skipEol = false;
14*be906b56SAndreas Gohr    protected $inParagraph = false;
15*be906b56SAndreas Gohr
16*be906b56SAndreas Gohr    // Blocks these should not be inside paragraphs
17*be906b56SAndreas Gohr    protected $blockOpen = array(
18*be906b56SAndreas Gohr        'header',
19*be906b56SAndreas Gohr        'listu_open','listo_open','listitem_open','listcontent_open',
20*be906b56SAndreas Gohr        'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
21*be906b56SAndreas Gohr        'quote_open',
22*be906b56SAndreas Gohr        'code','file','hr','preformatted','rss',
23*be906b56SAndreas Gohr        'htmlblock','phpblock',
24*be906b56SAndreas Gohr        'footnote_open',
25*be906b56SAndreas Gohr    );
26*be906b56SAndreas Gohr
27*be906b56SAndreas Gohr    protected $blockClose = array(
28*be906b56SAndreas Gohr        'header',
29*be906b56SAndreas Gohr        'listu_close','listo_close','listitem_close','listcontent_close',
30*be906b56SAndreas Gohr        'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
31*be906b56SAndreas Gohr        'quote_close',
32*be906b56SAndreas Gohr        'code','file','hr','preformatted','rss',
33*be906b56SAndreas Gohr        'htmlblock','phpblock',
34*be906b56SAndreas Gohr        'footnote_close',
35*be906b56SAndreas Gohr    );
36*be906b56SAndreas Gohr
37*be906b56SAndreas Gohr    // Stacks can contain paragraphs
38*be906b56SAndreas Gohr    protected $stackOpen = array(
39*be906b56SAndreas Gohr        'section_open',
40*be906b56SAndreas Gohr    );
41*be906b56SAndreas Gohr
42*be906b56SAndreas Gohr    protected $stackClose = array(
43*be906b56SAndreas Gohr        'section_close',
44*be906b56SAndreas Gohr    );
45*be906b56SAndreas Gohr
46*be906b56SAndreas Gohr
47*be906b56SAndreas Gohr    /**
48*be906b56SAndreas Gohr     * Constructor. Adds loaded syntax plugins to the block and stack
49*be906b56SAndreas Gohr     * arrays
50*be906b56SAndreas Gohr     *
51*be906b56SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
52*be906b56SAndreas Gohr     */
53*be906b56SAndreas Gohr    public function __construct()
54*be906b56SAndreas Gohr    {
55*be906b56SAndreas Gohr        global $DOKU_PLUGINS;
56*be906b56SAndreas Gohr        //check if syntax plugins were loaded
57*be906b56SAndreas Gohr        if (empty($DOKU_PLUGINS['syntax'])) return;
58*be906b56SAndreas Gohr        foreach ($DOKU_PLUGINS['syntax'] as $n => $p) {
59*be906b56SAndreas Gohr            $ptype = $p->getPType();
60*be906b56SAndreas Gohr            if ($ptype == 'block') {
61*be906b56SAndreas Gohr                $this->blockOpen[]  = 'plugin_'.$n;
62*be906b56SAndreas Gohr                $this->blockClose[] = 'plugin_'.$n;
63*be906b56SAndreas Gohr            } elseif ($ptype == 'stack') {
64*be906b56SAndreas Gohr                $this->stackOpen[]  = 'plugin_'.$n;
65*be906b56SAndreas Gohr                $this->stackClose[] = 'plugin_'.$n;
66*be906b56SAndreas Gohr            }
67*be906b56SAndreas Gohr        }
68*be906b56SAndreas Gohr    }
69*be906b56SAndreas Gohr
70*be906b56SAndreas Gohr    protected function openParagraph($pos)
71*be906b56SAndreas Gohr    {
72*be906b56SAndreas Gohr        if ($this->inParagraph) return;
73*be906b56SAndreas Gohr        $this->calls[] = array('p_open',array(), $pos);
74*be906b56SAndreas Gohr        $this->inParagraph = true;
75*be906b56SAndreas Gohr        $this->skipEol = true;
76*be906b56SAndreas Gohr    }
77*be906b56SAndreas Gohr
78*be906b56SAndreas Gohr    /**
79*be906b56SAndreas Gohr     * Close a paragraph if needed
80*be906b56SAndreas Gohr     *
81*be906b56SAndreas Gohr     * This function makes sure there are no empty paragraphs on the stack
82*be906b56SAndreas Gohr     *
83*be906b56SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
84*be906b56SAndreas Gohr     *
85*be906b56SAndreas Gohr     * @param string|integer $pos
86*be906b56SAndreas Gohr     */
87*be906b56SAndreas Gohr    protected function closeParagraph($pos)
88*be906b56SAndreas Gohr    {
89*be906b56SAndreas Gohr        if (!$this->inParagraph) return;
90*be906b56SAndreas Gohr        // look back if there was any content - we don't want empty paragraphs
91*be906b56SAndreas Gohr        $content = '';
92*be906b56SAndreas Gohr        $ccount = count($this->calls);
93*be906b56SAndreas Gohr        for ($i=$ccount-1; $i>=0; $i--) {
94*be906b56SAndreas Gohr            if ($this->calls[$i][0] == 'p_open') {
95*be906b56SAndreas Gohr                break;
96*be906b56SAndreas Gohr            } elseif ($this->calls[$i][0] == 'cdata') {
97*be906b56SAndreas Gohr                $content .= $this->calls[$i][1][0];
98*be906b56SAndreas Gohr            } else {
99*be906b56SAndreas Gohr                $content = 'found markup';
100*be906b56SAndreas Gohr                break;
101*be906b56SAndreas Gohr            }
102*be906b56SAndreas Gohr        }
103*be906b56SAndreas Gohr
104*be906b56SAndreas Gohr        if (trim($content)=='') {
105*be906b56SAndreas Gohr            //remove the whole paragraph
106*be906b56SAndreas Gohr            //array_splice($this->calls,$i); // <- this is much slower than the loop below
107*be906b56SAndreas Gohr            for ($x=$ccount; $x>$i;
108*be906b56SAndreas Gohr            $x--) array_pop($this->calls);
109*be906b56SAndreas Gohr        } else {
110*be906b56SAndreas Gohr            // remove ending linebreaks in the paragraph
111*be906b56SAndreas Gohr            $i=count($this->calls)-1;
112*be906b56SAndreas Gohr            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0], DOKU_PARSER_EOL);
113*be906b56SAndreas Gohr            $this->calls[] = array('p_close',array(), $pos);
114*be906b56SAndreas Gohr        }
115*be906b56SAndreas Gohr
116*be906b56SAndreas Gohr        $this->inParagraph = false;
117*be906b56SAndreas Gohr        $this->skipEol = true;
118*be906b56SAndreas Gohr    }
119*be906b56SAndreas Gohr
120*be906b56SAndreas Gohr    protected function addCall($call)
121*be906b56SAndreas Gohr    {
122*be906b56SAndreas Gohr        $key = count($this->calls);
123*be906b56SAndreas Gohr        if ($key and ($call[0] == 'cdata') and ($this->calls[$key-1][0] == 'cdata')) {
124*be906b56SAndreas Gohr            $this->calls[$key-1][1][0] .= $call[1][0];
125*be906b56SAndreas Gohr        } else {
126*be906b56SAndreas Gohr            $this->calls[] = $call;
127*be906b56SAndreas Gohr        }
128*be906b56SAndreas Gohr    }
129*be906b56SAndreas Gohr
130*be906b56SAndreas Gohr    // simple version of addCall, without checking cdata
131*be906b56SAndreas Gohr    protected function storeCall($call)
132*be906b56SAndreas Gohr    {
133*be906b56SAndreas Gohr        $this->calls[] = $call;
134*be906b56SAndreas Gohr    }
135*be906b56SAndreas Gohr
136*be906b56SAndreas Gohr    /**
137*be906b56SAndreas Gohr     * Processes the whole instruction stack to open and close paragraphs
138*be906b56SAndreas Gohr     *
139*be906b56SAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
140*be906b56SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
141*be906b56SAndreas Gohr     *
142*be906b56SAndreas Gohr     * @param array $calls
143*be906b56SAndreas Gohr     *
144*be906b56SAndreas Gohr     * @return array
145*be906b56SAndreas Gohr     */
146*be906b56SAndreas Gohr    public function process($calls)
147*be906b56SAndreas Gohr    {
148*be906b56SAndreas Gohr        // open first paragraph
149*be906b56SAndreas Gohr        $this->openParagraph(0);
150*be906b56SAndreas Gohr        foreach ($calls as $key => $call) {
151*be906b56SAndreas Gohr            $cname = $call[0];
152*be906b56SAndreas Gohr            if ($cname == 'plugin') {
153*be906b56SAndreas Gohr                $cname='plugin_'.$call[1][0];
154*be906b56SAndreas Gohr                $plugin = true;
155*be906b56SAndreas Gohr                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
156*be906b56SAndreas Gohr                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
157*be906b56SAndreas Gohr            } else {
158*be906b56SAndreas Gohr                $plugin = false;
159*be906b56SAndreas Gohr            }
160*be906b56SAndreas Gohr            /* stack */
161*be906b56SAndreas Gohr            if (in_array($cname, $this->stackClose) && (!$plugin || $plugin_close)) {
162*be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
163*be906b56SAndreas Gohr                $this->storeCall($call);
164*be906b56SAndreas Gohr                $this->openParagraph($call[2]);
165*be906b56SAndreas Gohr                continue;
166*be906b56SAndreas Gohr            }
167*be906b56SAndreas Gohr            if (in_array($cname, $this->stackOpen) && (!$plugin || $plugin_open)) {
168*be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
169*be906b56SAndreas Gohr                $this->storeCall($call);
170*be906b56SAndreas Gohr                $this->openParagraph($call[2]);
171*be906b56SAndreas Gohr                continue;
172*be906b56SAndreas Gohr            }
173*be906b56SAndreas Gohr            /* block */
174*be906b56SAndreas Gohr            // If it's a substition it opens and closes at the same call.
175*be906b56SAndreas Gohr            // To make sure next paragraph is correctly started, let close go first.
176*be906b56SAndreas Gohr            if (in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
177*be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
178*be906b56SAndreas Gohr                $this->storeCall($call);
179*be906b56SAndreas Gohr                $this->openParagraph($call[2]);
180*be906b56SAndreas Gohr                continue;
181*be906b56SAndreas Gohr            }
182*be906b56SAndreas Gohr            if (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
183*be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
184*be906b56SAndreas Gohr                $this->storeCall($call);
185*be906b56SAndreas Gohr                continue;
186*be906b56SAndreas Gohr            }
187*be906b56SAndreas Gohr            /* eol */
188*be906b56SAndreas Gohr            if ($cname == 'eol') {
189*be906b56SAndreas Gohr                // Check this isn't an eol instruction to skip...
190*be906b56SAndreas Gohr                if (!$this->skipEol) {
191*be906b56SAndreas Gohr                    // Next is EOL => double eol => mark as paragraph
192*be906b56SAndreas Gohr                    if (isset($calls[$key+1]) && $calls[$key+1][0] == 'eol') {
193*be906b56SAndreas Gohr                        $this->closeParagraph($call[2]);
194*be906b56SAndreas Gohr                        $this->openParagraph($call[2]);
195*be906b56SAndreas Gohr                    } else {
196*be906b56SAndreas Gohr                        //if this is just a single eol make a space from it
197*be906b56SAndreas Gohr                        $this->addCall(array('cdata',array(DOKU_PARSER_EOL), $call[2]));
198*be906b56SAndreas Gohr                    }
199*be906b56SAndreas Gohr                }
200*be906b56SAndreas Gohr                continue;
201*be906b56SAndreas Gohr            }
202*be906b56SAndreas Gohr            /* normal */
203*be906b56SAndreas Gohr            $this->addCall($call);
204*be906b56SAndreas Gohr            $this->skipEol = false;
205*be906b56SAndreas Gohr        }
206*be906b56SAndreas Gohr        // close last paragraph
207*be906b56SAndreas Gohr        $call = end($this->calls);
208*be906b56SAndreas Gohr        $this->closeParagraph($call[2]);
209*be906b56SAndreas Gohr        return $this->calls;
210*be906b56SAndreas Gohr    }
211*be906b56SAndreas Gohr}
212