table.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: table.php,v 1.1 2004/01/04 01:35:23 pmjones Exp $
  20. /**
  21. *
  22. * This class implements a Text_Wiki_Rule to find source text marked as a
  23. * set of table rows, where a line start and ends with double-pipes (||)
  24. * and uses double-pipes to separate table cells. The rows must be on
  25. * sequential lines (no blank lines between them) -- a blank line
  26. * indicates the beginning of a new table.
  27. *
  28. * @author Paul M. Jones <pmjones@ciaweb.net>
  29. *
  30. * @package Text_Wiki
  31. *
  32. */
  33. class Text_Wiki_Rule_table extends Text_Wiki_Rule {
  34. /**
  35. *
  36. * The regular expression used to parse the source text and find
  37. * matches conforming to this rule. Used by the parse() method.
  38. *
  39. * @access public
  40. *
  41. * @var string
  42. *
  43. * @see parse()
  44. *
  45. */
  46. var $regex = '/\n((\|\|).*)(\n)(?!(\|\|))/Us';
  47. /**
  48. *
  49. * Generates a replacement for the matched text. Token options are:
  50. *
  51. * 'type' =>
  52. * 'table_start' : the start of a bullet list
  53. * 'table_end' : the end of a bullet list
  54. * 'row_start' : the start of a number list
  55. * 'row_end' : the end of a number list
  56. * 'cell_start' : the start of item text (bullet or number)
  57. * 'cell_end' : the end of item text (bullet or number)
  58. *
  59. * 'colspan' => column span (for a cell)
  60. *
  61. * @access public
  62. *
  63. * @param array &$matches The array of matches from parse().
  64. *
  65. * @return A series of text and delimited tokens marking the different
  66. * table elements and cell text.
  67. *
  68. */
  69. function process(&$matches)
  70. {
  71. // out eventual return value
  72. $return = '';
  73. // start a new table
  74. $return .= $this->addToken(array('type' => 'table_start'));
  75. // rows are separated by newlines in the matched text
  76. $rows = explode("\n", $matches[1]);
  77. // loop through each row
  78. foreach ($rows as $row) {
  79. // start a new row
  80. $return .= $this->addToken(array('type' => 'row_start'));
  81. // cells are separated by double-pipes
  82. $cell = explode("||", $row);
  83. // get the last cell number
  84. $last = count($cell) - 1;
  85. // by default, cells span only one column (their own)
  86. $span = 1;
  87. // ignore cell zero, and ignore the "last" cell; cell zero
  88. // is before the first double-pipe, and the "last" cell is
  89. // after the last double-pipe. both are always empty.
  90. for ($i = 1; $i < $last; $i ++) {
  91. // if there is no content at all, then it's an instance
  92. // of two sets of || next to each other, indicating a
  93. // colspan.
  94. if ($cell[$i] == '') {
  95. // add to the span and loop to the next cell
  96. $span += 1;
  97. continue;
  98. } else {
  99. // this cell has content.
  100. // start a new cell...
  101. $return .= $this->addToken(
  102. array (
  103. 'type' => 'cell_start',
  104. 'colspan' => $span
  105. )
  106. );
  107. // ...add the content...
  108. $return .= trim($cell[$i]);
  109. // ...and end the cell.
  110. $return .= $this->addToken(
  111. array (
  112. 'type' => 'cell_end',
  113. 'colspan' => $span
  114. )
  115. );
  116. // reset the colspan.
  117. $span = 1;
  118. }
  119. }
  120. // end the row
  121. $return .= $this->addToken(array('type' => 'row_end'));
  122. }
  123. // end the table
  124. $return .= $this->addToken(array('type' => 'table_end'));
  125. // we're done!
  126. return "\n$return";
  127. }
  128. /**
  129. *
  130. * Renders a token into text matching the requested format.
  131. *
  132. * @access public
  133. *
  134. * @param array $options The "options" portion of the token (second
  135. * element).
  136. *
  137. * @return string The text rendered from the token options.
  138. *
  139. */
  140. function renderXhtml($options)
  141. {
  142. // make nice variable names (type, colspan)
  143. extract($options);
  144. $pad = ' ';
  145. switch ($type) {
  146. case 'table_start':
  147. return "<table border=\"1\" cellspacing=\"0\" cellpadding=\"4\">\n";
  148. break;
  149. case 'table_end':
  150. return "</table>\n";
  151. break;
  152. case 'row_start':
  153. return "$pad<tr>\n";
  154. break;
  155. case 'row_end':
  156. return "$pad</tr>\n";
  157. break;
  158. case 'cell_start':
  159. if ($colspan > 1) {
  160. return "$pad$pad<td colspan=\"$colspan\">";
  161. } else {
  162. return "$pad$pad<td>";
  163. }
  164. break;
  165. case 'cell_end':
  166. return "</td>\n";
  167. break;
  168. default:
  169. return '';
  170. }
  171. }
  172. }
  173. ?>