5 * Parses for table markup.
11 * @author Paul M. Jones <pmjones@php.net>
15 * @version $Id: Table.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $
21 * Parses for table markup.
23 * This class implements a Text_Wiki_Parse to find source text marked as a
24 * set of table rows, where a line start and ends with double-pipes (||)
25 * and uses double-pipes to separate table cells. The rows must be on
26 * sequential lines (no blank lines between them) -- a blank line
27 * indicates the beginning of a new table.
33 * @author Paul M. Jones <pmjones@php.net>
37 class Text_Wiki_Parse_Table
extends Text_Wiki_Parse
{
42 * The regular expression used to parse the source text and find
43 * matches conforming to this rule. Used by the parse() method.
53 var $regex = '/\n((\|\|).*)(\n)(?!(\|\|))/Us';
58 * Generates a replacement for the matched text.
63 * 'table_start' : the start of a bullet list
64 * 'table_end' : the end of a bullet list
65 * 'row_start' : the start of a number list
66 * 'row_end' : the end of a number list
67 * 'cell_start' : the start of item text (bullet or number)
68 * 'cell_end' : the end of item text (bullet or number)
70 * 'cols' => the number of columns in the table (for 'table_start')
72 * 'rows' => the number of rows in the table (for 'table_start')
74 * 'span' => column span (for 'cell_start')
76 * 'attr' => column attribute flag (for 'cell_start')
80 * @param array &$matches The array of matches from parse().
82 * @return A series of text and delimited tokens marking the different
83 * table elements and cell text.
87 function process(&$matches)
89 // our eventual return value
92 // the number of columns in the table
95 // the number of rows in the table
98 // rows are separated by newlines in the matched text
99 $rows = explode("\n", $matches[1]);
101 // loop through each row
102 foreach ($rows as $row) {
104 // increase the row count
108 $return .= $this->wiki
->addToken(
110 array('type' => 'row_start')
113 // cells are separated by double-pipes
114 $cell = explode("||", $row);
116 // get the number of cells (columns) in this row
117 $last = count($cell) - 1;
119 // is this more than the current column count?
120 // (we decrease by 1 because we never use cell zero)
121 if ($last - 1 > $num_cols) {
122 // increase the column count
123 $num_cols = $last - 1;
126 // by default, cells span only one column (their own)
129 // ignore cell zero, and ignore the "last" cell; cell zero
130 // is before the first double-pipe, and the "last" cell is
131 // after the last double-pipe. both are always empty.
132 for ($i = 1; $i < $last; $i ++
) {
134 // if there is no content at all, then it's an instance
135 // of two sets of || next to each other, indicating a
137 if ($cell[$i] == '') {
139 // add to the span and loop to the next cell
145 // this cell has content.
147 // find any special "attr"ibute cell markers
148 if (substr($cell[$i], 0, 2) == '> ') {
151 $cell[$i] = substr($cell[$i], 2);
152 } elseif (substr($cell[$i], 0, 2) == '= ') {
155 $cell[$i] = substr($cell[$i], 2);
156 } elseif (substr($cell[$i], 0, 2) == '< ') {
159 $cell[$i] = substr($cell[$i], 2);
160 } elseif (substr($cell[$i], 0, 2) == '~ ') {
162 $cell[$i] = substr($cell[$i], 2);
167 // start a new cell...
168 $return .= $this->wiki
->addToken(
171 'type' => 'cell_start',
177 // ...add the content...
178 $return .= trim($cell[$i]);
180 // ...and end the cell.
181 $return .= $this->wiki
->addToken(
184 'type' => 'cell_end',
197 $return .= $this->wiki
->addToken(
199 array('type' => 'row_end')
204 // wrap the return value in start and end tokens
206 $this->wiki
->addToken(
209 'type' => 'table_start',
215 $this->wiki
->addToken(
218 'type' => 'table_end'
223 return "\n$return\n\n";