Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
172 jpm 1
<?php
2
// $Id: Table.php,v 1.1 2004-11-24 19:43:09 jpm Exp $
3
 
4
 
5
/**
6
*
7
* This class implements a Text_Wiki_Parse to find source text marked as a
8
* set of table rows, where a line start and ends with double-pipes (||)
9
* and uses double-pipes to separate table cells.  The rows must be on
10
* sequential lines (no blank lines between them) -- a blank line
11
* indicates the beginning of a new table.
12
*
13
* @author Paul M. Jones <pmjones@ciaweb.net>
14
*
15
* @package Text_Wiki
16
*
17
*/
18
 
19
class Text_Wiki_Parse_Table extends Text_Wiki_Parse {
20
 
21
 
22
    /**
23
    *
24
    * The regular expression used to parse the source text and find
25
    * matches conforming to this rule.  Used by the parse() method.
26
    *
27
    * @access public
28
    *
29
    * @var string
30
    *
31
    * @see parse()
32
    *
33
    */
34
 
35
    var $regex = '/\n((\[\|).+?)(\|\])/ms';
36
 
37
 
38
    /**
39
    *
40
    * Generates a replacement for the matched text.
41
    *
42
    * Token options are:
43
    *
44
    * 'type' =>
45
    *     'table_start' : the start of a bullet list
46
    *     'table_end'   : the end of a bullet list
47
    *     'row_start' : the start of a number list
48
    *     'row_end'   : the end of a number list
49
    *     'cell_start'   : the start of item text (bullet or number)
50
    *     'cell_end'     : the end of item text (bullet or number)
51
    *
52
    * 'cols' => the number of columns in the table (for 'table_start')
53
    *
54
    * 'rows' => the number of rows in the table (for 'table_start')
55
    *
56
    * 'span' => column span (for 'cell_start')
57
    *
58
    * 'attr' => column attribute flag (for 'cell_start')
59
    *
60
    * @access public
61
    *
62
    * @param array &$matches The array of matches from parse().
63
    *
64
    * @return A series of text and delimited tokens marking the different
65
    * table elements and cell text.
66
    *
67
    */
68
 
69
    function process(&$matches)
70
    {
71
        // our eventual return value
72
        $return = '';
73
 
74
        // the number of columns in the table
75
        $num_cols = 0;
76
 
77
        // the number of rows in the table
78
        $num_rows = 0;
79
 
80
        // rows are separated by newlines in the matched text
81
        $rows = explode("\n", $matches[1]);
82
 
83
        // loop through each row
84
        foreach ($rows as $row) {
85
 
86
            // increase the row count
87
            $num_rows ++;
88
 
89
            // start a new row
90
            $return .= $this->wiki->addToken(
91
                $this->rule,
92
                array('type' => 'row_start')
93
            );
94
 
95
            // cells are separated by double-pipes
96
            $cell = explode("|", $row);
97
 
98
            // get the number of cells (columns) in this row
99
            $last = count($cell) - 1;
100
 
101
            // is this more than the current column count?
102
            // (we decrease by 1 because we never use cell zero)
103
            if ($last - 1 > $num_cols) {
104
                // increase the column count
105
                $num_cols = $last - 1;
106
            }
107
 
108
            // by default, cells span only one column (their own)
109
            $span = 1;
110
 
111
            // ignore cell zero, and ignore the "last" cell; cell zero
112
            // is before the first double-pipe, and the "last" cell is
113
            // after the last double-pipe. both are always empty.
114
            for ($i = 1; $i < $last; $i ++) {
115
                // if there is no content at all, then it's an instance
116
                // of two sets of || next to each other, indicating a
117
                // span.
118
                if ($cell[$i] == '') {
119
 
120
                    // add to the span and loop to the next cell
121
                    $span += 1;
122
                    continue;
123
 
124
                } else {
125
 
126
                    // this cell has content.
127
 
128
                    // find any special "attr"ibute cell markers
129
                    if (substr($cell[$i], 0, 2) == '> ') {
130
                        // right-align
131
                        $attr = 'right';
132
                        $cell[$i] = substr($cell[$i], 2);
133
                    } elseif (substr($cell[$i], 0, 2) == '= ') {
134
                        // center-align
135
                        $attr = 'center';
136
                        $cell[$i] = substr($cell[$i], 2);
137
                    } elseif (substr($cell[$i], 0, 2) == '< ') {
138
                        // left-align
139
                        $attr = 'left';
140
                        $cell[$i] = substr($cell[$i], 2);
141
                    } elseif (substr($cell[$i], 0, 2) == '~ ') {
142
                        $attr = 'header';
143
                        $cell[$i] = substr($cell[$i], 2);
144
                    } else {
145
                        $attr = null;
146
                    }
147
 
148
                    // start a new cell...
149
                    $return .= $this->wiki->addToken(
150
                        $this->rule,
151
                        array (
152
                            'type' => 'cell_start',
153
                            'attr' => $attr,
154
                            'span' => $span
155
                        )
156
                    );
157
 
158
                    // ...add the content...
159
                    $return .= trim($cell[$i]);
160
 
161
                    // ...and end the cell.
162
                    $return .= $this->wiki->addToken(
163
                        $this->rule,
164
                        array (
165
                            'type' => 'cell_end',
166
                            'attr' => $attr,
167
                            'span' => $span
168
                        )
169
                    );
170
 
171
                    // reset the span.
172
                    $span = 1;
173
                }
174
 
175
            }
176
 
177
            // end the row
178
            $return .= $this->wiki->addToken(
179
                $this->rule,
180
                array('type' => 'row_end')
181
            );
182
 
183
        }
184
 
185
        // wrap the return value in start and end tokens
186
        $return =
187
            $this->wiki->addToken(
188
                $this->rule,
189
                array(
190
                    'type' => 'table_start',
191
                    'rows' => $num_rows,
192
                    'cols' => $num_cols
193
                )
194
            )
195
            . $return .
196
            $this->wiki->addToken(
197
                $this->rule,
198
                array(
199
                    'type' => 'table_end'
200
                )
201
            );
202
 
203
        // we're done!
204
        return "\n$return\n\n";
205
    }
206
}
207
?>