]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / plugin / WikicreoleTable.php
1 <?php
2
3 /*
4  * Copyright (C) 2008-2009, 2011 Marc-Etienne Vargenau, Alcatel-Lucent
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /*
24  * Standard Alcatel-Lucent disclaimer for contributing to open source
25  *
26  * "The WikicreoleTablePlugin ("Contribution") has not been tested and/or
27  * validated for release as or in products, combinations with products or
28  * other commercial use. Any use of the Contribution is entirely made at
29  * the user's own responsibility and the user can not rely on any features,
30  * functionalities or performances Alcatel-Lucent has attributed to the
31  * Contribution.
32  *
33  * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
34  * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
35  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
36  * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
37  * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
38  * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
39  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40  * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
41  * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
42  * ALONE BASIS."
43  */
44
45 /**
46  * WikicreoleTablePlugin
47  * A PhpWiki plugin that allows insertion of tables using the Wikicreole
48  * syntax.
49  */
50
51 class WikiPlugin_WikicreoleTable
52     extends WikiPlugin
53 {
54     function getDescription()
55     {
56         return _("Layout tables using the Wikicreole syntax.");
57     }
58
59     function getDefaultArguments()
60     {
61         return array();
62     }
63
64     function handle_plugin_args_cruft($argstr, $args)
65     {
66         return;
67     }
68
69     function run($dbi, $argstr, &$request, $basepage)
70     {
71         include_once 'lib/InlineParser.php';
72
73         $table = array();
74
75         $lines = preg_split('/\s*?\n\s*/', $argstr);
76
77         foreach ($lines as $line) {
78             if (!$line) {
79                 continue;
80             }
81             $line = trim($line);
82             // If line ends with a '|', remove it
83             if ($line[strlen($line) - 1] == '|') {
84                 $line = substr($line, 0, -1);
85             }
86             if ($line[0] == '|') {
87                 $table[] = $this->parse_row($line);
88             }
89         }
90
91         $nb_rows = sizeof($table);
92         // If table is empty, do not generate table markup
93         if ($nb_rows == 0) {
94             return HTML::raw('');
95         }
96
97         // Number of columns is the number of cells in the longer row
98         $nb_cols = 0;
99         for ($i = 0; $i < $nb_rows; $i++) {
100             $nb_cols = max($nb_cols, sizeof($table[$i]));
101         }
102
103         for ($i = 0; $i < $nb_rows; $i++) {
104             for ($j = 0; $j < $nb_cols; $j++) {
105                 if (!isset($table[$i][$j])) {
106                     $table[$i][$j] = '';
107                 } elseif (preg_match('/@@/', $table[$i][$j])) {
108                     $table[$i][$j] = $this->compute_table_cell($table, $i, $j, $nb_rows, $nb_cols);
109                 }
110             }
111         }
112
113         $html_table = HTML::table(array('class' => "bordered"));
114         foreach ($table as $row) {
115             $html_row = HTML::tr();
116             foreach ($row as $cell) {
117                 if ($cell && $cell[0] == '=') {
118                     $cell = trim(substr($cell, 1));
119                     $html_row->pushContent(HTML::th(TransformInline($cell, $basepage)));
120                 } else {
121                     if ($this->is_wiki_numeric($cell)) {
122                         $html_row->pushContent(HTML::td(array('style' => "text-align:right"), TransformInline($cell, $basepage)));
123                     } else {
124                         $html_row->pushContent(HTML::td(TransformInline($cell, $basepage)));
125                     }
126                 }
127             }
128             $html_table->pushContent($html_row);
129         }
130         return $html_table;
131     }
132
133     // $cell is a number, possibly in bold, italics or underlined
134     private function is_wiki_numeric($cell)
135     {
136         return is_numeric(trim($cell, "*/_'"));
137     }
138
139     private function parse_row($line)
140     {
141         $bracket_link = "\\[ .*? [^]\s] .*? \\]";
142         $cell_content = "(?: [^[] | " . ESCAPE_CHAR . "\\[ | $bracket_link )*?";
143
144         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
145             $line, $matches, PREG_SET_ORDER);
146
147         $row = array();
148
149         foreach ($matches as $m) {
150             $cell = $m[2];
151             $row[] = $cell;
152         }
153         return $row;
154     }
155
156     /**
157      * Compute cell in spreadsheet table
158      * $table: two-dimensional table
159      * $i and $j: indexes of cell to compute
160      * $imax and $jmax: table dimensions
161      */
162     private function compute_table_cell($table, $i, $j, $imax, $jmax)
163     {
164
165         // What is implemented:
166         // @@=SUM(R)@@ : sum of cells in current row
167         // @@=SUM(C)@@ : sum of cells in current column
168         // @@=AVERAGE(R)@@ : average of cells in current row
169         // @@=AVERAGE(C)@@ : average of cells in current column
170         // @@=MAX(R)@@ : maximum value of cells in current row
171         // @@=MAX(C)@@ : maximum value of cells in current column
172         // @@=MIN(R)@@ : minimum value of cells in current row
173         // @@=MIN(C)@@ : minimum value of cells in current column
174         // @@=COUNT(R)@@ : number of cells in current row
175         //                (numeric or not, excluding headers and current cell)
176         // @@=COUNT(C)@@ : number of cells in current column
177         //                (numeric or not, excluding headers and current cell)
178
179         $result = 0;
180         $counter = 0;
181         $found = false;
182
183         if (strpos($table[$i][$j], "@@=SUM(C)@@") !== false) {
184             for ($index = 0; $index < $imax; $index++) {
185                 if (is_numeric($table[$index][$j])) {
186                     $result += $table[$index][$j];
187                 }
188             }
189             return str_replace("@@=SUM(C)@@", $result, $table[$i][$j]);
190
191         } elseif (strpos($table[$i][$j], "@@=SUM(R)@@") !== false) {
192             for ($index = 0; $index < $jmax; $index++) {
193                 if (is_numeric($table[$i][$index])) {
194                     $result += $table[$i][$index];
195                 }
196             }
197             return str_replace("@@=SUM(R)@@", $result, $table[$i][$j]);
198
199         } elseif (strpos($table[$i][$j], "@@=AVERAGE(C)@@") !== false) {
200             for ($index = 0; $index < $imax; $index++) {
201                 if (is_numeric($table[$index][$j])) {
202                     $result += $table[$index][$j];
203                     $counter++;
204                 }
205             }
206             $result = $result / $counter;
207             return str_replace("@@=AVERAGE(C)@@", $result, $table[$i][$j]);
208
209         } elseif (strpos($table[$i][$j], "@@=AVERAGE(R)@@") !== false) {
210             for ($index = 0; $index < $jmax; $index++) {
211                 if (is_numeric($table[$i][$index])) {
212                     $result += $table[$i][$index];
213                     $counter++;
214                 }
215             }
216             $result = $result / $counter;
217             return str_replace("@@=AVERAGE(R)@@", $result, $table[$i][$j]);
218
219         } elseif (strpos($table[$i][$j], "@@=MAX(C)@@") !== false) {
220             for ($index = 0; $index < $imax; $index++) {
221                 if (is_numeric($table[$index][$j])) {
222                     if (!$found) {
223                         $found = true;
224                         $result = $table[$index][$j];
225                     } else {
226                         $result = max($result, $table[$index][$j]);
227                     }
228                 }
229             }
230             if (!$found) {
231                 $result = "";
232             }
233             return str_replace("@@=MAX(C)@@", $result, $table[$i][$j]);
234
235         } elseif (strpos($table[$i][$j], "@@=MAX(R)@@") !== false) {
236             for ($index = 0; $index < $jmax; $index++) {
237                 if (is_numeric($table[$i][$index])) {
238                     if (!$found) {
239                         $found = true;
240                         $result = $table[$i][$index];
241                     } else {
242                         $result = max($result, $table[$i][$index]);
243                     }
244                 }
245             }
246             if (!$found) {
247                 $result = "";
248             }
249             return str_replace("@@=MAX(R)@@", $result, $table[$i][$j]);
250
251         } elseif (strpos($table[$i][$j], "@@=MIN(C)@@") !== false) {
252             for ($index = 0; $index < $imax; $index++) {
253                 if (is_numeric($table[$index][$j])) {
254                     if (!$found) {
255                         $found = true;
256                         $result = $table[$index][$j];
257                     } else {
258                         $result = min($result, $table[$index][$j]);
259                     }
260                 }
261             }
262             if (!$found) {
263                 $result = "";
264             }
265             return str_replace("@@=MIN(C)@@", $result, $table[$i][$j]);
266
267         } elseif (strpos($table[$i][$j], "@@=MIN(R)@@") !== false) {
268             for ($index = 0; $index < $jmax; $index++) {
269                 if (is_numeric($table[$i][$index])) {
270                     if (!$found) {
271                         $found = true;
272                         $result = $table[$i][$index];
273                     } else {
274                         $result = min($result, $table[$i][$index]);
275                     }
276                 }
277             }
278             if (!$found) {
279                 $result = "";
280             }
281             return str_replace("@@=MIN(R)@@", $result, $table[$i][$j]);
282
283         } elseif (strpos($table[$i][$j], "@@=COUNT(C)@@") !== false) {
284             for ($index = 0; $index < $imax; $index++) {
285                 // exclude header
286                 if (!string_starts_with(trim($table[$index][$j]), "=")) {
287                     $counter++;
288                 }
289             }
290             $result = $counter - 1; // exclude self
291             return str_replace("@@=COUNT(C)@@", $result, $table[$i][$j]);
292
293         } elseif (strpos($table[$i][$j], "@@=COUNT(R)@@") !== false) {
294             for ($index = 0; $index < $jmax; $index++) {
295                 // exclude header
296                 if (!string_starts_with(trim($table[$i][$index]), "=")) {
297                     $counter++;
298                 }
299             }
300             $result = $counter - 1; // exclude self
301             return str_replace("@@=COUNT(R)@@", $result, $table[$i][$j]);
302         }
303
304         return $table[$i][$j];
305     }
306
307 }
308
309 // Local Variables:
310 // mode: php
311 // tab-width: 8
312 // c-basic-offset: 4
313 // c-hanging-comment-ender-p: nil
314 // indent-tabs-mode: nil
315 // End: