From f05a1cc5fcdb4c2b6ee3cbf499f980f800dbd105 Mon Sep 17 00:00:00 2001
From: Gerrit Uitslag <klapinklapin@gmail.com>
Date: Thu, 17 Oct 2013 23:11:33 +0200
Subject: Wrap thead around 1st row, when 1st cell at 1st row is tableheader.
 Implements FS#1764

---
 inc/parser/handler.php | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

(limited to 'inc/parser/handler.php')

diff --git a/inc/parser/handler.php b/inc/parser/handler.php
index 1de981b48..55f344994 100644
--- a/inc/parser/handler.php
+++ b/inc/parser/handler.php
@@ -1278,6 +1278,7 @@ class Doku_Handler_Table {
 
         $lastRow = 0;
         $lastCell = 0;
+        $isThead = false;
         $cellKey = array();
         $toDelete = array();
 
@@ -1292,6 +1293,14 @@ class Doku_Handler_Table {
 
                     $lastRow++;
                     $lastCell = 0;
+
+                    if($lastRow === 1 && $this->tableCalls[$key+1][0] == 'tableheader_open') {
+                        $isThead = true;
+
+                        array_splice($this->tableCalls, $key, 0, array(
+                              array('tablethead_open', array(), $call[2])));
+                        $key += 1;
+                    }
                     break;
 
                 case 'tablecell_open':
@@ -1396,6 +1405,12 @@ class Doku_Handler_Table {
                         $key += 3;
                     }
 
+                    if($isThead) {
+                        array_splice($this->tableCalls, $key+1, 0, array(
+                              array('tablethead_close', array(), $call[2])));
+
+                        $isThead = false;
+                    }
                     break;
 
             }
@@ -1437,7 +1452,7 @@ class Doku_Handler_Block {
     var $blockOpen = array(
             'header',
             'listu_open','listo_open','listitem_open','listcontent_open',
-            'table_open','tablerow_open','tablecell_open','tableheader_open',
+            'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open',
             'quote_open',
             'code','file','hr','preformatted','rss',
             'htmlblock','phpblock',
@@ -1447,7 +1462,7 @@ class Doku_Handler_Block {
     var $blockClose = array(
             'header',
             'listu_close','listo_close','listitem_close','listcontent_close',
-            'table_close','tablerow_close','tablecell_close','tableheader_close',
+            'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close',
             'quote_close',
             'code','file','hr','preformatted','rss',
             'htmlblock','phpblock',
-- 
cgit v1.2.3


From 9060b8b02222ada7d08f4f6b77b4dd1e125fe5a2 Mon Sep 17 00:00:00 2001
From: Christopher Smith <chris@jalakai.co.uk>
Date: Fri, 18 Oct 2013 14:34:55 +0100
Subject: add <THEAD> to top rows of tables when all cells are <TH>

---
 inc/parser/handler.php | 45 +++++++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 18 deletions(-)

(limited to 'inc/parser/handler.php')

diff --git a/inc/parser/handler.php b/inc/parser/handler.php
index 55f344994..196cafc57 100644
--- a/inc/parser/handler.php
+++ b/inc/parser/handler.php
@@ -1149,6 +1149,8 @@ class Doku_Handler_Table {
     var $currentCols = 0;
     var $firstCell = false;
     var $lastCellType = 'tablecell';
+    var $inTableHead = true;
+    var $countTableHeadRows = 0;
 
     function Doku_Handler_Table(& $CallWriter) {
         $this->CallWriter = & $CallWriter;
@@ -1219,6 +1221,9 @@ class Doku_Handler_Table {
     }
 
     function tableRowClose($call) {
+        if ($this->inTableHead) {
+            $this->countTableHeadRows++;
+        }
         // Strip off final cell opening and anything after it
         while ( $discard = array_pop($this->tableCalls ) ) {
 
@@ -1234,6 +1239,9 @@ class Doku_Handler_Table {
     }
 
     function tableCell($call) {
+        if ($call[0] != 'tableheader') {
+            $this->inTableHead = false;
+        }
         if ( !$this->firstCell ) {
 
             // Increase the span
@@ -1278,7 +1286,6 @@ class Doku_Handler_Table {
 
         $lastRow = 0;
         $lastCell = 0;
-        $isThead = false;
         $cellKey = array();
         $toDelete = array();
 
@@ -1289,18 +1296,18 @@ class Doku_Handler_Table {
             $call = $this->tableCalls[$key];
 
             switch ($call[0]) {
+                case 'table_open' :
+                    if($this->countTableHeadRows) {
+                        array_splice($this->tableCalls, $key+1, 0, array(
+                              array('tablethead_open', array(), $call[2]))
+                        );
+                    }
+                    break;
+
                 case 'tablerow_open':
 
                     $lastRow++;
                     $lastCell = 0;
-
-                    if($lastRow === 1 && $this->tableCalls[$key+1][0] == 'tableheader_open') {
-                        $isThead = true;
-
-                        array_splice($this->tableCalls, $key, 0, array(
-                              array('tablethead_open', array(), $call[2])));
-                        $key += 1;
-                    }
                     break;
 
                 case 'tablecell_open':
@@ -1366,15 +1373,19 @@ class Doku_Handler_Table {
                     } else {
 
                         $spanning_cell = null;
-                        for($i = $lastRow-1; $i > 0; $i--) {
 
-                            if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
+                        // can't cross thead/tbody boundary
+                        if (!$this->countTableHeadRows || ($lastRow-1 != $this->countTableHeadRows)) {
+                            for($i = $lastRow-1; $i > 0; $i--) {
 
-                                if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
-                                    $spanning_cell = $i;
-                                    break;
-                                }
+                                if ( $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tablecell_open' || $this->tableCalls[$cellKey[$i][$lastCell]][0] == 'tableheader_open' ) {
 
+                                    if ($this->tableCalls[$cellKey[$i][$lastCell]][1][2] >= $lastRow - $i) {
+                                        $spanning_cell = $i;
+                                        break;
+                                    }
+
+                                }
                             }
                         }
                         if (is_null($spanning_cell)) {
@@ -1405,11 +1416,9 @@ class Doku_Handler_Table {
                         $key += 3;
                     }
 
-                    if($isThead) {
+                    if($this->countTableHeadRows == $lastRow) {
                         array_splice($this->tableCalls, $key+1, 0, array(
                               array('tablethead_close', array(), $call[2])));
-
-                        $isThead = false;
                     }
                     break;
 
-- 
cgit v1.2.3


From cef031c123914a2d88f125a8363c0917011e8ed0 Mon Sep 17 00:00:00 2001
From: Christopher Smith <chris@jalakai.co.uk>
Date: Thu, 13 Mar 2014 17:52:03 +0000
Subject: implement rules for when the top rows of a table qualify for a THEAD
 element 1. TD < 2 in a row 2. TD <= 50% of total cells 3. Not all table rows
 can be THEAD rows

---
 inc/parser/handler.php | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

(limited to 'inc/parser/handler.php')

diff --git a/inc/parser/handler.php b/inc/parser/handler.php
index c4104dac9..d8382e7fc 100644
--- a/inc/parser/handler.php
+++ b/inc/parser/handler.php
@@ -1150,6 +1150,7 @@ class Doku_Handler_Table {
     var $firstCell = false;
     var $lastCellType = 'tablecell';
     var $inTableHead = true;
+    var $currentRow = array('tableheader' => 0, 'tablecell' => 0);
     var $countTableHeadRows = 0;
 
     function Doku_Handler_Table(& $CallWriter) {
@@ -1218,10 +1219,13 @@ class Doku_Handler_Table {
         $this->firstCell = true;
         $this->lastCellType = 'tablecell';
         $this->maxRows++;
+        if ($this->inTableHead) {
+            $this->currentRow = array('tablecell' => 0, 'tableheader' => 0);
+        }
     }
 
     function tableRowClose($call) {
-        if ($this->inTableHead) {
+        if ($this->inTableHead && ($this->inTableHead = $this->isTableHeadRow())) {
             $this->countTableHeadRows++;
         }
         // Strip off final cell opening and anything after it
@@ -1230,6 +1234,9 @@ class Doku_Handler_Table {
             if ( $discard[0] == 'tablecell_open' || $discard[0] == 'tableheader_open') {
                 break;
             }
+            if (!empty($this->currentRow[$discard[0]])) {
+                $this->currentRow[$discard[0]]--;
+            }
         }
         $this->tableCalls[] = array('tablerow_close', array(), $call[2]);
 
@@ -1238,9 +1245,19 @@ class Doku_Handler_Table {
         }
     }
 
+    function isTableHeadRow() {
+        $td = $this->currentRow['tablecell'];
+        $th = $this->currentRow['tableheader'];
+
+        if (!$th || $td > 2) return false;
+        if (2*$td > $th) return false;
+
+        return true;
+    }
+
     function tableCell($call) {
-        if ($call[0] != 'tableheader') {
-            $this->inTableHead = false;
+        if ($this->inTableHead) {
+            $this->currentRow[$call[0]]++;
         }
         if ( !$this->firstCell ) {
 
@@ -1289,6 +1306,13 @@ class Doku_Handler_Table {
         $cellKey = array();
         $toDelete = array();
 
+        // if still in tableheader, then there can be no table header
+        // as all rows can't be within <THEAD>
+        if ($this->inTableHead) {
+            $this->inTableHead = false;
+            $this->countTableHeadRows = 0;
+        }
+
         // Look for the colspan elements and increment the colspan on the
         // previous non-empty opening cell. Once done, delete all the cells
         // that contain colspans
-- 
cgit v1.2.3