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