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: [PATCH] Wrap thead around 1st row, when 1st cell at 1st row is
 tableheader. Implements FS#1764

---
 _test/tests/inc/parser/parser_table.test.php | 51 ++++++++++++++++++++
 inc/parser/handler.php                       | 19 +++++++-
 inc/parser/renderer.php                      |  4 ++
 inc/parser/xhtml.php                         |  8 +++
 4 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/_test/tests/inc/parser/parser_table.test.php b/_test/tests/inc/parser/parser_table.test.php
index 542a307b8..5258afe3d 100644
--- a/_test/tests/inc/parser/parser_table.test.php
+++ b/_test/tests/inc/parser/parser_table.test.php
@@ -125,6 +125,7 @@ def');
             array('cdata',array("\n\nabc")),
             array('p_close',array()),
             array('table_open',array(3, 1, 6)),
+            array('tablethead_open',array()),
             array('tablerow_open',array()),
             array('tableheader_open',array(1,NULL,1)),
             array('cdata',array(' X ')),
@@ -136,6 +137,7 @@ def');
             array('cdata',array(' Z ')),
             array('tableheader_close',array()),
             array('tablerow_close',array()),
+            array('tablethead_close',array()),
             array('table_close',array(19)),
             array('p_open',array()),
             array('cdata',array('def')),
@@ -146,6 +148,55 @@ def');
         $this->assertEquals(array_map('stripbyteindex',$this->H->calls),$calls);
 
     }
+
+    function testTableHeadersMultilines() {
+        $this->P->addMode('table',new Doku_Parser_Mode_Table());
+        $this->P->parse('
+abc
+^ X | Y ^ Z |
+| A | B | C |
+def');
+
+        $calls = array (
+            array('document_start',array()),
+            array('p_open',array()),
+            array('cdata',array("\n\nabc")),
+            array('p_close',array()),
+            array('table_open',array(3, 2, 6)),
+            array('tablethead_open',array()),
+            array('tablerow_open',array()),
+            array('tableheader_open',array(1,NULL,1)),
+            array('cdata',array(' X ')),
+            array('tableheader_close',array()),
+            array('tablecell_open',array(1,NULL,1)),
+            array('cdata',array(' Y ')),
+            array('tablecell_close',array()),
+            array('tableheader_open',array(1,NULL,1)),
+            array('cdata',array(' Z ')),
+            array('tableheader_close',array()),
+            array('tablerow_close',array()),
+            array('tablethead_close',array()),
+            array('tablerow_open',array()),
+            array('tablecell_open',array(1,NULL,1)),
+            array('cdata',array(' A ')),
+            array('tablecell_close',array()),
+            array('tablecell_open',array(1,NULL,1)),
+            array('cdata',array(' B ')),
+            array('tablecell_close',array()),
+            array('tablecell_open',array(1,NULL,1)),
+            array('cdata',array(' C ')),
+            array('tablecell_close',array()),
+            array('tablerow_close',array()),
+            array('table_close',array(33)),
+            array('p_open',array()),
+            array('cdata',array('def')),
+            array('p_close',array()),
+            array('document_end',array()),
+        );
+
+        $this->assertEquals(array_map('stripbyteindex',$this->H->calls),$calls);
+
+    }
     
     function testCellAlignment() {
         $this->P->addMode('table',new Doku_Parser_Mode_Table());
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',
diff --git a/inc/parser/renderer.php b/inc/parser/renderer.php
index c697e990c..ee0b2344d 100644
--- a/inc/parser/renderer.php
+++ b/inc/parser/renderer.php
@@ -245,6 +245,10 @@ class Doku_Renderer extends DokuWiki_Plugin {
 
     function table_close($pos = null){}
 
+    function tablethead_open(){}
+
+    function tablethead_close(){}
+
     function tablerow_open(){}
 
     function tablerow_close(){}
diff --git a/inc/parser/xhtml.php b/inc/parser/xhtml.php
index fd02c0ce0..e9f2cc037 100644
--- a/inc/parser/xhtml.php
+++ b/inc/parser/xhtml.php
@@ -946,6 +946,14 @@ class Doku_Renderer_xhtml extends Doku_Renderer {
         }
     }
 
+    function tablethead_open(){
+        $this->doc .= DOKU_TAB . '<thead>' . DOKU_LF;
+    }
+
+    function tablethead_close(){
+        $this->doc .= DOKU_TAB . '</thead>' . DOKU_LF;
+    }
+
     function tablerow_open(){
         // initialize the cell counter used for classes
         $this->_counter['cell_counter'] = 0;
-- 
GitLab