Subversion Repositories Applications.papyrus

Rev

Rev 173 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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