bjori           Sun Aug  5 18:54:57 2007 UTC

  Added files:                 
    /phd/tests/xhtml    001.phpt 
    /phd/tests/xhtml/data       001-1.xml 

  Modified files:              
    /phd/formats        xhtml.php 
    /phd/include        PhDFormat.class.php 
  Log:
  - Fix colspan logic
  - Fix regression in last commit
  - Implement tfoot
  - Add test
  
  
http://cvs.php.net/viewvc.cgi/phd/formats/xhtml.php?r1=1.9&r2=1.10&diff_format=u
Index: phd/formats/xhtml.php
diff -u phd/formats/xhtml.php:1.9 phd/formats/xhtml.php:1.10
--- phd/formats/xhtml.php:1.9   Sun Aug  5 17:47:09 2007
+++ phd/formats/xhtml.php       Sun Aug  5 18:54:57 2007
@@ -1,5 +1,5 @@
 <?php
-/*  $Id: xhtml.php,v 1.9 2007/08/05 17:47:09 bjori Exp $ */
+/*  $Id: xhtml.php,v 1.10 2007/08/05 18:54:57 bjori Exp $ */
 
 class XHTMLPhDFormat extends PhDFormat {
     protected $map = array( /* {{{ */
@@ -94,6 +94,8 @@
         'systemitem'            => 'format_systemitem',
         'table'                 => 'format_table',
         'term'                  => 'span',
+        'tfoot'                 => 'format_th',
+        'thead'                 => 'format_th',
         'title'                 => array(
             /* DEFAULT */          'h1',
             'legalnotice'       => 'h4',
@@ -311,31 +313,36 @@
         }
         return "</colgroup>\n";
     }
+    private function parse_table_entry_attributes($attrs) {
+        $retval = sprintf('align="%s"', $attrs["align"]);
+        if ($attrs["align"] == "char" && isset($attrs["char"])) {
+            $retval .= sprintf(' char="%s"', htmlspecialchars($attrs["char"], 
ENT_QUOTES));
+            if (isset($attrs["charoff"])) {
+                $retval .= sprintf(' charoff="%s"', 
htmlspecialchars($attrs["charoff"], ENT_QUOTES));
+            }
+        }
+        if (isset($attrs["valign"])) {
+            $retval .= sprintf(' valign="%s"', $attrs["valign"]);
+        }
+        if (isset($attrs["colwidth"])) {
+            $retval .= sprintf(' width="%d"', $attrs["colwidth"]);
+        }
+        return $retval;
+    }
     public function format_colspec($open, $name) {
         if ($open) {
-            $colspec = PhDFormat::colspec();
+            $attrs = 
self::parse_table_entry_attributes(PhDFormat::colspec(PhDFormat::getAttributes()));
 
-            $moreattr = "";
-            if ($colspec["align"] == "char" && isset($colspec["char"])) {
-                $moreattr .= sprintf(' char="%s"', 
htmlspecialchars($colspec["char"], ENT_QUOTES));
-                if (isset($colspec["charoff"])) {
-                    $moreattr .= sprintf(' charoff="%s"', 
htmlspecialchars($colspec["charoff"], ENT_QUOTES));
-                }
-            }
-            if (isset($colspec["colwidth"])) {
-                $moreattr .= sprintf(' width="%d"', $colspec["colwidth"]);
-            }
-
-            return sprintf('<col align="%s"%s />', $colspec["align"], 
$moreattr);
+            return sprintf('<col %s />', $attrs);
         }
         /* noop */
     }
-    public function format_thead($open, $name) {
+    public function format_th($open, $name) {
         if ($open) {
             $valign = PhDFormat::valign();
-            return sprintf('<thead valign="%s">', $valign);
+            return sprintf('<%s valign="%s">', $name, $valign);
         }
-        return "</thead>\n";
+        return "</$name>\n";
     }
     public function format_tbody($open, $name) {
         if ($open) {
@@ -353,16 +360,20 @@
     }
     public function format_th_entry($open, $name) {
         if ($open) {
-            $colspan = PhDFormat::colspan();
+            $attrs = PhDFormat::getAttributes();
+            $colspan = PhDFormat::colspan($attrs);
             return sprintf('<th colspan="%d">', $colspan);
         }
         return '</th>';
     }
     public function format_entry($open, $name) {
         if ($open) {
-            $colspan = PhDFormat::colspan();
-            $rowspan = PhDFormat::rowspan();
-            return sprintf('<td colspan="%d" rowspan="%d">', $colspan, 
$rowspan);
+            $attrs = PhDFormat::getColspec(PhDFormat::getAttributes());
+
+            $colspan = PhDFormat::colspan($attrs);
+            $rowspan = PhDFormat::rowspan($attrs);
+            $moreattrs = self::parse_table_entry_attributes($attrs);
+            return sprintf('<td colspan="%d" rowspan="%d" %s>', $colspan, 
$rowspan, $moreattrs);
         }
         return "</td>";
     }
http://cvs.php.net/viewvc.cgi/phd/include/PhDFormat.class.php?r1=1.2&r2=1.3&diff_format=u
Index: phd/include/PhDFormat.class.php
diff -u phd/include/PhDFormat.class.php:1.2 phd/include/PhDFormat.class.php:1.3
--- phd/include/PhDFormat.class.php:1.2 Sun Aug  5 17:47:08 2007
+++ phd/include/PhDFormat.class.php     Sun Aug  5 18:54:57 2007
@@ -1,5 +1,5 @@
 <?php
-/*  $Id: PhDFormat.class.php,v 1.2 2007/08/05 17:47:08 bjori Exp $ */
+/*  $Id: PhDFormat.class.php,v 1.3 2007/08/05 18:54:57 bjori Exp $ */
 
 abstract class PhDFormat {
     private $reader;
@@ -69,37 +69,45 @@
 
         return $attrs;
     }
-    public function colspec() {
+    public function colspec(array $attrs) {
+        $colspec = self::getColSpec($attrs);
+        $this->TABLE["colspec"][$colspec["colnum"]] = $colspec;
+        return $colspec;
+    }
+    public function getColspec(array $attrs) {
         /* defaults */
         $defaults["colname"] = count($this->TABLE["colspec"])+1;
         $defaults["colnum"]  = count($this->TABLE["colspec"])+1;
         $defaults["align"]   = "left";
 
-        $attrs = self::getAttributes();
-        $colspec = array_merge($defaults, $this->TABLE["defaults"], $attrs);
-
-        $this->TABLE["colspec"][$colspec["colnum"]] = $colspec;
-        return $colspec;
+        return array_merge($defaults, $this->TABLE["defaults"], $attrs);
     }
+
     public function valign() {
         $valign = self::readAttribute("valign");
         return $valign ? $valign : "middle";
     }
-    public function colspan() {
-        if ($start = $this->readAttribute("namest")) {
-            $from = array_search($start, $this->TABLE["colspec"]);
-            $end = $this->readAttribute("nameend");
-            $to = array_search($end, $this->TABLE["colspec"]);
-            return $end-$to;
+    public function colspan(array $attrs) {
+        if (isset($attrs["namest"])) {
+            foreach($this->TABLE["colspec"] as $colnum => $spec) {
+                if ($spec["colname"] == $attrs["namest"]) {
+                    $from = $spec["colnum"];
+                    continue;
+                }
+                if ($spec["colname"] == $attrs["nameend"]) {
+                    $to = $spec["colnum"];
+                    continue;
+                }
+            }
+            return $to-$from+1;
         }
         return 1;
     }
-    public function rowspan() {
-        $rows = 1;
-        if ($morerows = $this->readAttribute("morerows")) {
-            $rows += $morerows;
+    public function rowspan($attrs) {
+        if (isset($attrs["morerows"])) {
+            return $attrs["morerows"]+1;
         }
-        return $rows;
+        return 1;
     }
 
 }

http://cvs.php.net/viewvc.cgi/phd/tests/xhtml/001.phpt?view=markup&rev=1.1
Index: phd/tests/xhtml/001.phpt
+++ phd/tests/xhtml/001.phpt
--TEST--
CALS Table rendering
--FILE--
<?php
/*  $Id: 001.phpt,v 1.1 2007/08/05 18:54:57 bjori Exp $ */

require "include/PhDReader.class.php";
require "include/PhDFormat.class.php";
require "formats/xhtml.php";

$reader = new PhDReader(dirname(__FILE__) ."/data/001-1.xml");
$format = new XHTMLPhDFormat($reader, array(), array());

$map = $format->getMap();

while($reader->read()) {
    $type = $reader->nodeType;
    $name = $reader->name;

    switch($type) {
    case XMLReader::ELEMENT:
    case XMLReader::END_ELEMENT:
        $open = $type == XMLReader::ELEMENT;

        $funcname = "format_$name";
        if (isset($map[$name])) {
            $tag = $map[$name];
            if (is_array($tag)) {
                $tag = $reader->notXPath($tag);
            }
            if (strncmp($tag, "format_", 7)) {
                $retval = $format->transformFromMap($open, $tag, $name);
                break;
            }
            $funcname = $tag;
        }

        $retval = $format->{$funcname}($open, $name);
        break;

    case XMLReader::TEXT:
        $retval = htmlspecialchars($reader->value, ENT_QUOTES);
        break;

    case XMLReader::CDATA:
        $retval = $format->CDATA($reader->value);
        break;

    case XMLReader::COMMENT:
    case XMLReader::WHITESPACE:
    case XMLReader::SIGNIFICANT_WHITESPACE:
    case XMLReader::DOC_TYPE:
        /* swallow it */
        continue 2;

    default:
        trigger_error("Don't know how to handle {$name} {$type}", E_USER_ERROR);
        return;
    }
    echo $retval, "\n";
}

$reader->close();
--EXPECT--
<div id="" class="article">
<h1 class="title">
Example table
</h1>
<table border="5">
<h1 class="title">
Sample CALS Table
</h1>
<colgroup>

<col align="left" />
<col align="left" />
<col align="left" />
<col align="left" />
<thead valign="middle">
<tr valign="middle">
<th colspan="2">
Horizontal Span
</th>
<th colspan="1">
a3
</th>
<th colspan="1">
a4
</th>
<th colspan="1">
a5
</th>
</tr>

</thead>

<tfoot valign="middle">
<tr valign="middle">
<th colspan="1">
f1
</th>
<th colspan="1">
f2
</th>
<th colspan="1">
f3
</th>
<th colspan="1">
f4
</th>
<th colspan="1">
f5
</th>
</tr>

</tfoot>

<tbody valign="middle">
<tr valign="middle">
<td colspan="1" rowspan="1" align="left">
b1
</td>
<td colspan="1" rowspan="1" align="left">
b2
</td>
<td colspan="1" rowspan="1" align="left">
b3
</td>
<td colspan="1" rowspan="1" align="left">
b4
</td>
<td colspan="1" rowspan="2" align="left" valign="middle">
<p class="para">
Vertical Span
</p>
</td>
</tr>

<tr valign="middle">
<td colspan="1" rowspan="1" align="left">
c1
</td>
<td colspan="2" rowspan="2" align="center" valign="bottom">
Span Both
</td>
<td colspan="1" rowspan="1" align="left">
c4
</td>
</tr>

<tr valign="middle">
<td colspan="1" rowspan="1" align="left">
d1
</td>
<td colspan="1" rowspan="1" align="left">
d4
</td>
<td colspan="1" rowspan="1" align="left">
d5
</td>
</tr>

</tbody>
</colgroup>

</table>

</div>

http://cvs.php.net/viewvc.cgi/phd/tests/xhtml/data/001-1.xml?view=markup&rev=1.1
Index: phd/tests/xhtml/data/001-1.xml
+++ phd/tests/xhtml/data/001-1.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- copy&paste from http://www.docbook.org/tdg5/en/html/cals.table.html -->
<article xmlns='http://docbook.org/ns/docbook'>
<title>Example table</title>

<table xml:id="ex.calstable" frame='all'>
<title>Sample CALS Table</title>
<tgroup cols='5' align='left' colsep='1' rowsep='1'>
<colspec colname='c1'/>
<colspec colname='c2'/>
<colspec colname='c3'/>
<colspec colnum='5' colname='c5'/>
<thead>
<row>
  <entry namest="c1" nameend="c2" align="center">Horizontal Span</entry>
  <entry>a3</entry>
  <entry>a4</entry>
  <entry>a5</entry>
</row>
</thead>
<tfoot>
<row>
  <entry>f1</entry>
  <entry>f2</entry>
  <entry>f3</entry>
  <entry>f4</entry>
  <entry>f5</entry>
</row>
</tfoot>
<tbody>
<row>
  <entry>b1</entry>
  <entry>b2</entry>
  <entry>b3</entry>
  <entry>b4</entry>
  <entry morerows='1' valign='middle'><para>Vertical Span</para></entry>
</row>
<row>
  <entry>c1</entry>
  <entry namest="c2" nameend="c3" align='center' morerows='1' 
valign='bottom'>Span Both</entry>
  <entry>c4</entry>
</row>
<row>
  <entry>d1</entry>
  <entry>d4</entry>
  <entry>d5</entry>
</row>
</tbody>
</tgroup>
</table>

</article>

Reply via email to