]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikicreoleTable.php
Normalize header
[SourceForge/phpwiki.git] / lib / plugin / WikicreoleTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  * Copyright (C) 2008-2009 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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 getName() {
55         return _("WikicreoleTable");
56     }
57
58     function getDescription() {
59       return _("Layout tables using the Wikicreole syntax.");
60     }
61
62     function getDefaultArguments() {
63         return array();
64     }
65
66     function getVersion() {
67         return preg_replace("/[Revision: $]/", '',
68                             "\$Revision$");
69     }
70
71     function handle_plugin_args_cruft($argstr, $args) {
72         return;
73     }
74
75     function run($dbi, $argstr, &$request, $basepage) {
76         global $WikiTheme;
77         include_once('lib/InlineParser.php');
78
79         $table = array();
80
81         $lines = preg_split('/\s*?\n\s*/', $argstr);
82
83         foreach ($lines as $line) {
84             if (!$line) {
85                 continue;
86             }
87             $line = trim($line);
88             // If line ends with a '|', remove it
89             if ($line[strlen($line)-1] == '|') {
90                 $line = substr($line, 0, -1);
91             }
92             if ($line[0] != '|') {
93                 // trigger_error(sprintf(_("Line %s does not begin with a '|'."), $line), E_USER_WARNING);
94             } else {
95                 $table[] = $this->_parse_row($line, $basepage);
96             }
97         }
98
99         $nbrows = sizeof($table);
100         // If table is empty, do not generate table markup
101         if ($nbrows == 0) {
102             return HTML::raw('');
103         }
104         $nbcols = sizeof($table[0]);
105
106         for ($i=0; $i<$nbrows; $i++) {
107             for ($j=0; $j<$nbcols; $j++) {
108                 if (preg_match('/@@/', $table[$i][$j])) {
109                     $table[$i][$j] = compute_tablecell($table, $i, $j, $nbrows, $nbcols);
110                 }
111             }
112         }
113
114         $htmltable = HTML::table(array('class' => "bordered"));
115         foreach ($table as $row) {
116             $htmlrow = HTML::tr();
117             foreach ($row as $cell) {
118                 if ($cell && $cell[0] == '=') {
119                     $cell = trim(substr($cell, 1));
120                     $htmlrow->pushContent(HTML::th(TransformInline($cell, 2.0, $basepage)));
121                 } else {
122                     if (is_numeric($cell)) {
123                         $htmlrow->pushContent(HTML::td(array('style' => "text-align:right"), $cell));
124                     } else {
125                         $htmlrow->pushContent(HTML::td(TransformInline($cell, 2.0, $basepage)));
126                     }
127                 }
128             }
129             $htmltable->pushContent($htmlrow);
130         }
131         return $htmltable;
132     }
133
134     function _parse_row ($line, $basepage) {
135         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
136         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
137         
138         preg_match_all("/(\\|+) \s* ($cell_content) \s* (?=\\||\$)/x",
139                        $line, $matches, PREG_SET_ORDER);
140
141         $row = array();
142
143         foreach ($matches as $m) {
144             $cell = $m[2];
145             $row[]= $cell;
146         }
147         return $row;
148     }
149
150 }
151
152 // For emacs users
153 // Local Variables:
154 // mode: php
155 // tab-width: 8
156 // c-basic-offset: 4
157 // c-hanging-comment-ender-p: nil
158 // indent-tabs-mode: nil
159 // End:
160 ?>