xref: /dokuwiki/inc/Parsing/Handler/Table.php (revision 643ea3a6eaa27d23e64c1e9e0bac118f6174594d)
1be906b56SAndreas Gohr<?php
2be906b56SAndreas Gohr
3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\Handler;
4be906b56SAndreas Gohr
5533aca44SAndreas Gohrclass Table extends AbstractRewriter
6be906b56SAndreas Gohr{
7bcaec9f4SAndreas Gohr    protected $tableCalls = [];
8be906b56SAndreas Gohr    protected $maxCols = 0;
9be906b56SAndreas Gohr    protected $maxRows = 1;
10be906b56SAndreas Gohr    protected $currentCols = 0;
11be906b56SAndreas Gohr    protected $firstCell = false;
12be906b56SAndreas Gohr    protected $lastCellType = 'tablecell';
13be906b56SAndreas Gohr    protected $inTableHead = true;
14bcaec9f4SAndreas Gohr    protected $currentRow = ['tableheader' => 0, 'tablecell' => 0];
15be906b56SAndreas Gohr    protected $countTableHeadRows = 0;
16be906b56SAndreas Gohr
17be906b56SAndreas Gohr    /** @inheritdoc */
18be906b56SAndreas Gohr    public function finalise()
19be906b56SAndreas Gohr    {
20be906b56SAndreas Gohr        $last_call = end($this->calls);
21bcaec9f4SAndreas Gohr        $this->writeCall(['table_end', [], $last_call[2]]);
22be906b56SAndreas Gohr
23be906b56SAndreas Gohr        $this->process();
24be906b56SAndreas Gohr        $this->callWriter->finalise();
25be906b56SAndreas Gohr        unset($this->callWriter);
26be906b56SAndreas Gohr    }
27be906b56SAndreas Gohr
28be906b56SAndreas Gohr    /** @inheritdoc */
29be906b56SAndreas Gohr    public function process()
30be906b56SAndreas Gohr    {
31be906b56SAndreas Gohr        foreach ($this->calls as $call) {
32be906b56SAndreas Gohr            switch ($call[0]) {
33be906b56SAndreas Gohr                case 'table_start':
34be906b56SAndreas Gohr                    $this->tableStart($call);
35be906b56SAndreas Gohr                    break;
36be906b56SAndreas Gohr                case 'table_row':
37be906b56SAndreas Gohr                    $this->tableRowClose($call);
38bcaec9f4SAndreas Gohr                    $this->tableRowOpen(['tablerow_open', $call[1], $call[2]]);
39be906b56SAndreas Gohr                    break;
40be906b56SAndreas Gohr                case 'tableheader':
41be906b56SAndreas Gohr                case 'tablecell':
42be906b56SAndreas Gohr                    $this->tableCell($call);
43be906b56SAndreas Gohr                    break;
44be906b56SAndreas Gohr                case 'table_end':
45be906b56SAndreas Gohr                    $this->tableRowClose($call);
46be906b56SAndreas Gohr                    $this->tableEnd($call);
47be906b56SAndreas Gohr                    break;
48be906b56SAndreas Gohr                default:
49be906b56SAndreas Gohr                    $this->tableDefault($call);
50be906b56SAndreas Gohr                    break;
51be906b56SAndreas Gohr            }
52be906b56SAndreas Gohr        }
53be906b56SAndreas Gohr        $this->callWriter->writeCalls($this->tableCalls);
54be906b56SAndreas Gohr
55be906b56SAndreas Gohr        return $this->callWriter;
56be906b56SAndreas Gohr    }
57be906b56SAndreas Gohr
58be906b56SAndreas Gohr    protected function tableStart($call)
59be906b56SAndreas Gohr    {
60bcaec9f4SAndreas Gohr        $this->tableCalls[] = ['table_open', $call[1], $call[2]];
61bcaec9f4SAndreas Gohr        $this->tableCalls[] = ['tablerow_open', [], $call[2]];
62be906b56SAndreas Gohr        $this->firstCell = true;
63be906b56SAndreas Gohr    }
64be906b56SAndreas Gohr
65be906b56SAndreas Gohr    protected function tableEnd($call)
66be906b56SAndreas Gohr    {
67bcaec9f4SAndreas Gohr        $this->tableCalls[] = ['table_close', $call[1], $call[2]];
68be906b56SAndreas Gohr        $this->finalizeTable();
69be906b56SAndreas Gohr    }
70be906b56SAndreas Gohr
71be906b56SAndreas Gohr    protected function tableRowOpen($call)
72be906b56SAndreas Gohr    {
73be906b56SAndreas Gohr        $this->tableCalls[] = $call;
74be906b56SAndreas Gohr        $this->currentCols = 0;
75be906b56SAndreas Gohr        $this->firstCell = true;
76be906b56SAndreas Gohr        $this->lastCellType = 'tablecell';
77be906b56SAndreas Gohr        $this->maxRows++;
78be906b56SAndreas Gohr        if ($this->inTableHead) {
79bcaec9f4SAndreas Gohr            $this->currentRow = ['tablecell' => 0, 'tableheader' => 0];
80be906b56SAndreas Gohr        }
81be906b56SAndreas Gohr    }
82be906b56SAndreas Gohr
83be906b56SAndreas Gohr    protected function tableRowClose($call)
84be906b56SAndreas Gohr    {
85be906b56SAndreas Gohr        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
86be906b56SAndreas Gohr            $this->countTableHeadRows++;
87be906b56SAndreas Gohr        }
88be906b56SAndreas Gohr        // Strip off final cell opening and anything after it
89be906b56SAndreas Gohr        while ($discard = array_pop($this->tableCalls)) {
90be906b56SAndreas Gohr            if ($discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
91be906b56SAndreas Gohr                break;
92be906b56SAndreas Gohr            }
93be906b56SAndreas Gohr            if (!empty($this->currentRow[$discard[0]])) {
94be906b56SAndreas Gohr                $this->currentRow[$discard[0]]--;
95be906b56SAndreas Gohr            }
96be906b56SAndreas Gohr        }
97bcaec9f4SAndreas Gohr        $this->tableCalls[] = ['tablerow_close', [], $call[2]];
98be906b56SAndreas Gohr
99be906b56SAndreas Gohr        if ($this->currentCols > $this->maxCols) {
100be906b56SAndreas Gohr            $this->maxCols = $this->currentCols;
101be906b56SAndreas Gohr        }
102be906b56SAndreas Gohr    }
103be906b56SAndreas Gohr
104be906b56SAndreas Gohr    protected function isTableHeadRow()
105be906b56SAndreas Gohr    {
106be906b56SAndreas Gohr        $td = $this->currentRow['tablecell'];
107be906b56SAndreas Gohr        $th = $this->currentRow['tableheader'];
108be906b56SAndreas Gohr
109be906b56SAndreas Gohr        if (!$th || $td > 2) return false;
110be906b56SAndreas Gohr        if (2 * $td > $th) return false;
111be906b56SAndreas Gohr
112be906b56SAndreas Gohr        return true;
113be906b56SAndreas Gohr    }
114be906b56SAndreas Gohr
115be906b56SAndreas Gohr    protected function tableCell($call)
116be906b56SAndreas Gohr    {
117be906b56SAndreas Gohr        if ($this->inTableHead) {
118be906b56SAndreas Gohr            $this->currentRow[$call[0]]++;
119be906b56SAndreas Gohr        }
120be906b56SAndreas Gohr        if (!$this->firstCell) {
121be906b56SAndreas Gohr            // Increase the span
122be906b56SAndreas Gohr            $lastCall = end($this->tableCalls);
123be906b56SAndreas Gohr
124be906b56SAndreas Gohr            // A cell call which follows an open cell means an empty cell so span
125be906b56SAndreas Gohr            if ($lastCall[0] == 'tablecell_open' || $lastCall[0] == 'tableheader_open') {
126bcaec9f4SAndreas Gohr                $this->tableCalls[] = ['colspan', [], $call[2]];
127be906b56SAndreas Gohr            }
128be906b56SAndreas Gohr
129bcaec9f4SAndreas Gohr            $this->tableCalls[] = [$this->lastCellType . '_close', [], $call[2]];
130bcaec9f4SAndreas Gohr            $this->tableCalls[] = [$call[0] . '_open', [1, null, 1], $call[2]];
131be906b56SAndreas Gohr            $this->lastCellType = $call[0];
132be906b56SAndreas Gohr        } else {
133bcaec9f4SAndreas Gohr            $this->tableCalls[] = [$call[0] . '_open', [1, null, 1], $call[2]];
134be906b56SAndreas Gohr            $this->lastCellType = $call[0];
135be906b56SAndreas Gohr            $this->firstCell = false;
136be906b56SAndreas Gohr        }
137be906b56SAndreas Gohr
138be906b56SAndreas Gohr        $this->currentCols++;
139be906b56SAndreas Gohr    }
140be906b56SAndreas Gohr
141be906b56SAndreas Gohr    protected function tableDefault($call)
142be906b56SAndreas Gohr    {
143be906b56SAndreas Gohr        $this->tableCalls[] = $call;
144be906b56SAndreas Gohr    }
145be906b56SAndreas Gohr
146be906b56SAndreas Gohr    protected function finalizeTable()
147be906b56SAndreas Gohr    {
148be906b56SAndreas Gohr
149be906b56SAndreas Gohr        // Add the max cols and rows to the table opening
150be906b56SAndreas Gohr        if ($this->tableCalls[0][0] == 'table_open') {
151be906b56SAndreas Gohr            // Adjust to num cols not num col delimeters
152be906b56SAndreas Gohr            $this->tableCalls[0][1][] = $this->maxCols - 1;
153be906b56SAndreas Gohr            $this->tableCalls[0][1][] = $this->maxRows;
154be906b56SAndreas Gohr            $this->tableCalls[0][1][] = array_shift($this->tableCalls[0][1]);
155be906b56SAndreas Gohr        } else {
156be906b56SAndreas Gohr            trigger_error('First element in table call list is not table_open');
157be906b56SAndreas Gohr        }
158be906b56SAndreas Gohr
159be906b56SAndreas Gohr        $lastRow = 0;
160be906b56SAndreas Gohr        $lastCell = 0;
161bcaec9f4SAndreas Gohr        $cellKey = [];
162bcaec9f4SAndreas Gohr        $toDelete = [];
163be906b56SAndreas Gohr
164be906b56SAndreas Gohr        // if still in tableheader, then there can be no table header
165be906b56SAndreas Gohr        // as all rows can't be within <THEAD>
166be906b56SAndreas Gohr        if ($this->inTableHead) {
167be906b56SAndreas Gohr            $this->inTableHead = false;
168be906b56SAndreas Gohr            $this->countTableHeadRows = 0;
169be906b56SAndreas Gohr        }
170be906b56SAndreas Gohr
171be906b56SAndreas Gohr        // Look for the colspan elements and increment the colspan on the
172be906b56SAndreas Gohr        // previous non-empty opening cell. Once done, delete all the cells
173be906b56SAndreas Gohr        // that contain colspans
174*643ea3a6SAndreas Gohr        $key = -1;
175*643ea3a6SAndreas Gohr        while (++$key < count($this->tableCalls)) {
176be906b56SAndreas Gohr            $call = $this->tableCalls[$key];
177be906b56SAndreas Gohr
178be906b56SAndreas Gohr            switch ($call[0]) {
179be906b56SAndreas Gohr                case 'table_open':
180be906b56SAndreas Gohr                    if ($this->countTableHeadRows) {
181bcaec9f4SAndreas Gohr                        array_splice($this->tableCalls, $key + 1, 0, [['tablethead_open', [], $call[2]]]);
182be906b56SAndreas Gohr                    }
183be906b56SAndreas Gohr                    break;
184be906b56SAndreas Gohr
185be906b56SAndreas Gohr                case 'tablerow_open':
186be906b56SAndreas Gohr                    $lastRow++;
187be906b56SAndreas Gohr                    $lastCell = 0;
188be906b56SAndreas Gohr                    break;
189be906b56SAndreas Gohr
190be906b56SAndreas Gohr                case 'tablecell_open':
191be906b56SAndreas Gohr                case 'tableheader_open':
192be906b56SAndreas Gohr                    $lastCell++;
193be906b56SAndreas Gohr                    $cellKey[$lastRow][$lastCell] = $key;
194be906b56SAndreas Gohr                    break;
195be906b56SAndreas Gohr
196be906b56SAndreas Gohr                case 'table_align':
197bcaec9f4SAndreas Gohr                    $prev = in_array($this->tableCalls[$key - 1][0], ['tablecell_open', 'tableheader_open']);
198bcaec9f4SAndreas Gohr                    $next = in_array($this->tableCalls[$key + 1][0], ['tablecell_close', 'tableheader_close']);
199be906b56SAndreas Gohr                    // If the cell is empty, align left
200be906b56SAndreas Gohr                    if ($prev && $next) {
201be906b56SAndreas Gohr                        $this->tableCalls[$key - 1][1][1] = 'left';
202be906b56SAndreas Gohr
203be906b56SAndreas Gohr                        // If the previous element was a cell open, align right
204be906b56SAndreas Gohr                    } elseif ($prev) {
205be906b56SAndreas Gohr                        $this->tableCalls[$key - 1][1][1] = 'right';
206be906b56SAndreas Gohr
207be906b56SAndreas Gohr                        // If the next element is the close of an element, align either center or left
208be906b56SAndreas Gohr                    } elseif ($next) {
209be906b56SAndreas Gohr                        if ($this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] == 'right') {
210be906b56SAndreas Gohr                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'center';
211be906b56SAndreas Gohr                        } else {
212be906b56SAndreas Gohr                            $this->tableCalls[$cellKey[$lastRow][$lastCell]][1][1] = 'left';
213be906b56SAndreas Gohr                        }
214be906b56SAndreas Gohr                    }
215be906b56SAndreas Gohr
216be906b56SAndreas Gohr                    // Now convert the whitespace back to cdata
217be906b56SAndreas Gohr                    $this->tableCalls[$key][0] = 'cdata';
218be906b56SAndreas Gohr                    break;
219be906b56SAndreas Gohr
220be906b56SAndreas Gohr                case 'colspan':
221be906b56SAndreas Gohr                    $this->tableCalls[$key - 1][1][0] = false;
222be906b56SAndreas Gohr
223be906b56SAndreas Gohr                    for ($i = $key - 2; $i >= $cellKey[$lastRow][1]; $i--) {
2247d34963bSAndreas Gohr                        if (
2257d34963bSAndreas Gohr                            $this->tableCalls[$i][0] == 'tablecell_open' ||
226be906b56SAndreas Gohr                            $this->tableCalls[$i][0] == 'tableheader_open'
227be906b56SAndreas Gohr                        ) {
228be906b56SAndreas Gohr                            if (false !== $this->tableCalls[$i][1][0]) {
229be906b56SAndreas Gohr                                $this->tableCalls[$i][1][0]++;
230be906b56SAndreas Gohr                                break;
231be906b56SAndreas Gohr                            }
232be906b56SAndreas Gohr                        }
233be906b56SAndreas Gohr                    }
234be906b56SAndreas Gohr
235be906b56SAndreas Gohr                    $toDelete[] = $key - 1;
236be906b56SAndreas Gohr                    $toDelete[] = $key;
237be906b56SAndreas Gohr                    $toDelete[] = $key + 1;
238be906b56SAndreas Gohr                    break;
239be906b56SAndreas Gohr
240be906b56SAndreas Gohr                case 'rowspan':
241be906b56SAndreas Gohr                    if ($this->tableCalls[$key - 1][0] == 'cdata') {
242be906b56SAndreas Gohr                        // ignore rowspan if previous call was cdata (text mixed with :::)
243be906b56SAndreas Gohr                        // we don't have to check next call as that wont match regex
244be906b56SAndreas Gohr                        $this->tableCalls[$key][0] = 'cdata';
245be906b56SAndreas Gohr                    } else {
246be906b56SAndreas Gohr                        $spanning_cell = null;
247be906b56SAndreas Gohr
248be906b56SAndreas Gohr                        // can't cross thead/tbody boundary
249be906b56SAndreas Gohr                        if (!$this->countTableHeadRows || ($lastRow - 1 != $this->countTableHeadRows)) {
250be906b56SAndreas Gohr                            for ($i = $lastRow - 1; $i > 0; $i--) {
2517d34963bSAndreas Gohr                                if (
2527d34963bSAndreas Gohr                                    $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' ||
253be906b56SAndreas Gohr                                    $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open'
254be906b56SAndreas Gohr                                ) {
255be906b56SAndreas Gohr                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
256be906b56SAndreas Gohr                                        $spanning_cell = $i;
257be906b56SAndreas Gohr                                        break;
258be906b56SAndreas Gohr                                    }
259be906b56SAndreas Gohr                                }
260be906b56SAndreas Gohr                            }
261be906b56SAndreas Gohr                        }
262be906b56SAndreas Gohr                        if (is_null($spanning_cell)) {
263be906b56SAndreas Gohr                            // No spanning cell found, so convert this cell to
264be906b56SAndreas Gohr                            // an empty one to avoid broken tables
265be906b56SAndreas Gohr                            $this->tableCalls[$key][0] = 'cdata';
266be906b56SAndreas Gohr                            $this->tableCalls[$key][1][0] = '';
267277113f1SAndreas Gohr                            break;
268be906b56SAndreas Gohr                        }
269be906b56SAndreas Gohr                        $this->tableCalls[$cellKey[$spanning_cell][$lastCell]][1][2]++;
270be906b56SAndreas Gohr
271be906b56SAndreas Gohr                        $this->tableCalls[$key - 1][1][2] = false;
272be906b56SAndreas Gohr
273be906b56SAndreas Gohr                        $toDelete[] = $key - 1;
274be906b56SAndreas Gohr                        $toDelete[] = $key;
275be906b56SAndreas Gohr                        $toDelete[] = $key + 1;
276be906b56SAndreas Gohr                    }
277be906b56SAndreas Gohr                    break;
278be906b56SAndreas Gohr
279be906b56SAndreas Gohr                case 'tablerow_close':
280be906b56SAndreas Gohr                    // Fix broken tables by adding missing cells
281bcaec9f4SAndreas Gohr                    $moreCalls = [];
282be906b56SAndreas Gohr                    while (++$lastCell < $this->maxCols) {
283bcaec9f4SAndreas Gohr                        $moreCalls[] = ['tablecell_open', [1, null, 1], $call[2]];
284bcaec9f4SAndreas Gohr                        $moreCalls[] = ['cdata', [''], $call[2]];
285bcaec9f4SAndreas Gohr                        $moreCalls[] = ['tablecell_close', [], $call[2]];
286be906b56SAndreas Gohr                    }
287be906b56SAndreas Gohr                    $moreCallsLength = count($moreCalls);
288be906b56SAndreas Gohr                    if ($moreCallsLength) {
289be906b56SAndreas Gohr                        array_splice($this->tableCalls, $key, 0, $moreCalls);
290be906b56SAndreas Gohr                        $key += $moreCallsLength;
291be906b56SAndreas Gohr                    }
292be906b56SAndreas Gohr
293be906b56SAndreas Gohr                    if ($this->countTableHeadRows == $lastRow) {
294bcaec9f4SAndreas Gohr                        array_splice($this->tableCalls, $key + 1, 0, [['tablethead_close', [], $call[2]]]);
295be906b56SAndreas Gohr                    }
296be906b56SAndreas Gohr                    break;
297be906b56SAndreas Gohr            }
298be906b56SAndreas Gohr        }
299be906b56SAndreas Gohr
300be906b56SAndreas Gohr        // condense cdata
301be906b56SAndreas Gohr        $cnt = count($this->tableCalls);
302be906b56SAndreas Gohr        for ($key = 0; $key < $cnt; $key++) {
303be906b56SAndreas Gohr            if ($this->tableCalls[$key][0] == 'cdata') {
304be906b56SAndreas Gohr                $ckey = $key;
305be906b56SAndreas Gohr                $key++;
306be906b56SAndreas Gohr                while ($this->tableCalls[$key][0] == 'cdata') {
307be906b56SAndreas Gohr                    $this->tableCalls[$ckey][1][0] .= $this->tableCalls[$key][1][0];
308be906b56SAndreas Gohr                    $toDelete[] = $key;
309be906b56SAndreas Gohr                    $key++;
310be906b56SAndreas Gohr                }
311be906b56SAndreas Gohr                continue;
312be906b56SAndreas Gohr            }
313be906b56SAndreas Gohr        }
314be906b56SAndreas Gohr
315be906b56SAndreas Gohr        foreach ($toDelete as $delete) {
316be906b56SAndreas Gohr            unset($this->tableCalls[$delete]);
317be906b56SAndreas Gohr        }
318be906b56SAndreas Gohr        $this->tableCalls = array_values($this->tableCalls);
319be906b56SAndreas Gohr    }
320be906b56SAndreas Gohr}
321