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