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