xref: /dokuwiki/inc/Parsing/Handler/Block.php (revision bcaec9f47d06126b3e653fea89a86d8b6a6cbef8)
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{
12*bcaec9f4SAndreas Gohr    protected $calls = [];
13be906b56SAndreas Gohr    protected $skipEol = false;
14be906b56SAndreas Gohr    protected $inParagraph = false;
15be906b56SAndreas Gohr
16be906b56SAndreas Gohr    // Blocks these should not be inside paragraphs
17*bcaec9f4SAndreas Gohr    protected $blockOpen = [
18*bcaec9f4SAndreas Gohr        'header', 'listu_open', 'listo_open', 'listitem_open', 'listcontent_open', 'table_open', 'tablerow_open',
19*bcaec9f4SAndreas Gohr        'tablecell_open', 'tableheader_open', 'tablethead_open', 'quote_open', 'code', 'file', 'hr', 'preformatted',
20*bcaec9f4SAndreas Gohr        'rss', 'footnote_open'
21*bcaec9f4SAndreas Gohr    ];
22be906b56SAndreas Gohr
23*bcaec9f4SAndreas Gohr    protected $blockClose = [
24*bcaec9f4SAndreas Gohr        'header', 'listu_close', 'listo_close', 'listitem_close', 'listcontent_close', 'table_close',
25*bcaec9f4SAndreas Gohr        'tablerow_close', 'tablecell_close', 'tableheader_close', 'tablethead_close', 'quote_close', 'code', 'file',
26*bcaec9f4SAndreas Gohr        'hr', 'preformatted', 'rss', 'footnote_close'
27*bcaec9f4SAndreas Gohr    ];
28be906b56SAndreas Gohr
29be906b56SAndreas Gohr    // Stacks can contain paragraphs
30*bcaec9f4SAndreas Gohr    protected $stackOpen = ['section_open'];
31be906b56SAndreas Gohr
32*bcaec9f4SAndreas 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;
61*bcaec9f4SAndreas 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
95be906b56SAndreas Gohr            for ($x=$ccount; $x>$i;
96be906b56SAndreas Gohr            $x--) array_pop($this->calls);
97be906b56SAndreas Gohr        } else {
98be906b56SAndreas Gohr            // remove ending linebreaks in the paragraph
99be906b56SAndreas Gohr            $i=count($this->calls)-1;
10023f4cc47SAndreas Gohr            if ($this->calls[$i][0] == 'cdata') $this->calls[$i][1][0] = rtrim($this->calls[$i][1][0], "\n");
101*bcaec9f4SAndreas Gohr            $this->calls[] = ['p_close', [], $pos];
102be906b56SAndreas Gohr        }
103be906b56SAndreas Gohr
104be906b56SAndreas Gohr        $this->inParagraph = false;
105be906b56SAndreas Gohr        $this->skipEol = true;
106be906b56SAndreas Gohr    }
107be906b56SAndreas Gohr
108be906b56SAndreas Gohr    protected function addCall($call)
109be906b56SAndreas Gohr    {
110be906b56SAndreas Gohr        $key = count($this->calls);
111*bcaec9f4SAndreas Gohr        if ($key && $call[0] == 'cdata' && $this->calls[$key-1][0] == 'cdata') {
112be906b56SAndreas Gohr            $this->calls[$key-1][1][0] .= $call[1][0];
113be906b56SAndreas Gohr        } else {
114be906b56SAndreas Gohr            $this->calls[] = $call;
115be906b56SAndreas Gohr        }
116be906b56SAndreas Gohr    }
117be906b56SAndreas Gohr
118be906b56SAndreas Gohr    // simple version of addCall, without checking cdata
119be906b56SAndreas Gohr    protected function storeCall($call)
120be906b56SAndreas Gohr    {
121be906b56SAndreas Gohr        $this->calls[] = $call;
122be906b56SAndreas Gohr    }
123be906b56SAndreas Gohr
124be906b56SAndreas Gohr    /**
125be906b56SAndreas Gohr     * Processes the whole instruction stack to open and close paragraphs
126be906b56SAndreas Gohr     *
127be906b56SAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
128be906b56SAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
129be906b56SAndreas Gohr     *
130be906b56SAndreas Gohr     * @param array $calls
131be906b56SAndreas Gohr     *
132be906b56SAndreas Gohr     * @return array
133be906b56SAndreas Gohr     */
134be906b56SAndreas Gohr    public function process($calls)
135be906b56SAndreas Gohr    {
136be906b56SAndreas Gohr        // open first paragraph
137be906b56SAndreas Gohr        $this->openParagraph(0);
138be906b56SAndreas Gohr        foreach ($calls as $key => $call) {
139be906b56SAndreas Gohr            $cname = $call[0];
140be906b56SAndreas Gohr            if ($cname == 'plugin') {
141be906b56SAndreas Gohr                $cname='plugin_'.$call[1][0];
142be906b56SAndreas Gohr                $plugin = true;
143be906b56SAndreas Gohr                $plugin_open = (($call[1][2] == DOKU_LEXER_ENTER) || ($call[1][2] == DOKU_LEXER_SPECIAL));
144be906b56SAndreas Gohr                $plugin_close = (($call[1][2] == DOKU_LEXER_EXIT) || ($call[1][2] == DOKU_LEXER_SPECIAL));
145be906b56SAndreas Gohr            } else {
146be906b56SAndreas Gohr                $plugin = false;
147be906b56SAndreas Gohr            }
148be906b56SAndreas Gohr            /* stack */
149be906b56SAndreas Gohr            if (in_array($cname, $this->stackClose) && (!$plugin || $plugin_close)) {
150be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
151be906b56SAndreas Gohr                $this->storeCall($call);
152be906b56SAndreas Gohr                $this->openParagraph($call[2]);
153be906b56SAndreas Gohr                continue;
154be906b56SAndreas Gohr            }
155be906b56SAndreas Gohr            if (in_array($cname, $this->stackOpen) && (!$plugin || $plugin_open)) {
156be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
157be906b56SAndreas Gohr                $this->storeCall($call);
158be906b56SAndreas Gohr                $this->openParagraph($call[2]);
159be906b56SAndreas Gohr                continue;
160be906b56SAndreas Gohr            }
161be906b56SAndreas Gohr            /* block */
162be906b56SAndreas Gohr            // If it's a substition it opens and closes at the same call.
163be906b56SAndreas Gohr            // To make sure next paragraph is correctly started, let close go first.
164be906b56SAndreas Gohr            if (in_array($cname, $this->blockClose) && (!$plugin || $plugin_close)) {
165be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
166be906b56SAndreas Gohr                $this->storeCall($call);
167be906b56SAndreas Gohr                $this->openParagraph($call[2]);
168be906b56SAndreas Gohr                continue;
169be906b56SAndreas Gohr            }
170be906b56SAndreas Gohr            if (in_array($cname, $this->blockOpen) && (!$plugin || $plugin_open)) {
171be906b56SAndreas Gohr                $this->closeParagraph($call[2]);
172be906b56SAndreas Gohr                $this->storeCall($call);
173be906b56SAndreas Gohr                continue;
174be906b56SAndreas Gohr            }
175be906b56SAndreas Gohr            /* eol */
176be906b56SAndreas Gohr            if ($cname == 'eol') {
177be906b56SAndreas Gohr                // Check this isn't an eol instruction to skip...
178be906b56SAndreas Gohr                if (!$this->skipEol) {
179be906b56SAndreas Gohr                    // Next is EOL => double eol => mark as paragraph
180be906b56SAndreas Gohr                    if (isset($calls[$key+1]) && $calls[$key+1][0] == 'eol') {
181be906b56SAndreas Gohr                        $this->closeParagraph($call[2]);
182be906b56SAndreas Gohr                        $this->openParagraph($call[2]);
183be906b56SAndreas Gohr                    } else {
184be906b56SAndreas Gohr                        //if this is just a single eol make a space from it
185*bcaec9f4SAndreas Gohr                        $this->addCall(['cdata', ["\n"], $call[2]]);
186be906b56SAndreas Gohr                    }
187be906b56SAndreas Gohr                }
188be906b56SAndreas Gohr                continue;
189be906b56SAndreas Gohr            }
190be906b56SAndreas Gohr            /* normal */
191be906b56SAndreas Gohr            $this->addCall($call);
192be906b56SAndreas Gohr            $this->skipEol = false;
193be906b56SAndreas Gohr        }
194be906b56SAndreas Gohr        // close last paragraph
195be906b56SAndreas Gohr        $call = end($this->calls);
196be906b56SAndreas Gohr        $this->closeParagraph($call[2]);
197be906b56SAndreas Gohr        return $this->calls;
198be906b56SAndreas Gohr    }
199be906b56SAndreas Gohr}
200