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