]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MediawikiTable.php
Match for @@
[SourceForge/phpwiki.git] / lib / plugin / MediawikiTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4   MediawikiTablePlugin
5   A PhpWiki plugin that allows insertion of tables using a Mediawiki-like
6   syntax.
7 */
8 /*
9  * Copyright (C) 2003 Sameer D. Sahasrabuddhe
10  * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
11  * Copyright (C) 2008-2009 Alcatel-Lucent
12  *
13  * This file is part of PhpWiki.
14  *
15  * PhpWiki is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * PhpWiki is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with PhpWiki; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 */
29
30 /*
31  * Standard Alcatel-Lucent disclaimer for contributing to open source
32  *
33  * "The MediawikiTablePlugin ("Contribution") has not been tested and/or 
34  * validated for release as or in products, combinations with products or
35  * other commercial use. Any use of the Contribution is entirely made at 
36  * the user's own responsibility and the user can not rely on any features,
37  * functionalities or performances Alcatel-Lucent has attributed to the 
38  * Contribution.
39  *
40  * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY 
41  * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
42  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
43  * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE 
44  * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL 
45  * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN 
46  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
47  * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER 
48  * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND 
49  * ALONE BASIS."
50  */
51
52 class WikiPlugin_MediawikiTable
53 extends WikiPlugin
54 {
55     function getName() {
56         return _("MediawikiTable");
57     }
58
59     function getDescription() {
60       return _("Layout tables using a Mediawiki-like markup style.");
61     }
62
63     function getDefaultArguments() {
64         return array();
65     }
66
67     function getVersion() {
68         return preg_replace("/[Revision: $]/", '',
69                             "\$Revision$");
70     }
71
72     function run($dbi, $argstr, &$request, $basepage) {
73         global $WikiTheme;
74         include_once("lib/BlockParser.php");
75         // MediawikiTablePlugin markup is new.
76         $markup = 2.0;
77
78         // We allow the compact Mediawiki syntax with:
79         // - multiple cells on the same line (separated by "||"),
80         // - multiple header cells on the same line (separated by "!!").
81         $argstr = str_replace("||", "\n|", $argstr);
82         $argstr = str_replace("!!", "\n!", $argstr);
83
84         $lines = preg_split('/\n/', $argstr);
85         $table = HTML::table();
86
87         if (substr($lines[0],0,2) == "{|") {
88             // Start of table
89             $lines[0] = substr($lines[0],2);
90         }
91         if (($lines[0][0] != '|') and ($lines[0][0] != '!')) {
92             $line = array_shift($lines);
93             $attrs = parse_attributes($line);
94             foreach ($attrs as $key => $value) {
95                 if (in_array ($key, array("id", "class", "title", "style",
96                                           "bgcolor", "frame", "rules", "border",
97                                           "cellspacing", "cellpadding",
98                                           "summary", "align", "width"))) {
99                     $table->setAttr($key, $value);
100                 }
101             }
102         }
103
104         foreach ($lines as $line){
105             if (substr($line,0,2) == "|}") {
106                 // End of table
107                 continue;
108             }
109             if (substr($line,0,2) == "|-") {
110                 if (isset($row)) {
111                     if (isset($cell)) {
112                         if (isset($content)) {
113                             if (is_numeric(trim($content))) {
114                                 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
115                             } else {
116                                 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
117                             }
118                             unset($content);
119                         }
120                         $row->pushContent($cell);
121                         unset($cell);
122                     }
123                     if (isset($thead)) {
124                         $thead->pushContent($row);
125                         $table->pushContent($thead);
126                         unset($thead);
127                         $tbody = HTML::tbody();
128                     } else {
129                         $tbody->pushContent($row);
130                     }
131                 }
132                 $row = HTML::tr();
133                 $attrs = parse_attributes(substr($line,2));
134                 foreach ($attrs as $key => $value) {
135                     if (in_array ($key, array("id", "class", "title", "style",
136                                               "bgcolor", "align", "valign"))) {
137                         $row->setAttr($key, $value);
138                     }
139                 }
140                 continue;
141             }
142
143             // Table summary
144             if (substr($line,0,2) == "|=") {
145                 $line = substr($line,2);
146                 $table->setAttr("summary", trim($line));
147             }
148
149             // Table caption
150             if (substr($line,0,2) == "|+") {
151
152                 $caption = HTML::caption();
153                 $line = substr($line,2);
154                 $pospipe = strpos($line, "|");
155                 $posbracket = strpos($line, "[");
156                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
157                     $attrs = parse_attributes(substr($line, 0, $pospipe));
158                     foreach ($attrs as $key => $value) {
159                         if (in_array ($key, array("id", "class", "title", "style",
160                                                   "align", "lang"))) {
161                             $caption->setAttr($key, $value);
162                         }
163                     }
164                     $line=substr($line, $pospipe+1);
165                 }
166
167                 $caption->pushContent(trim($line));
168                 $table->pushContent($caption);
169             }
170
171             if (((substr($line,0,1) == "|") or (substr($line,0,1) == "!")) and isset($row)) {
172                 if (isset($cell)) {
173                     if (isset ($content)) {
174                         if (is_numeric(trim($content))) {
175                             $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
176                         } else {
177                             $cell->pushContent(TransformText(trim($content), $markup, $basepage));
178                         }
179                         unset($content);
180                     }
181                     $row->pushContent($cell);
182                 }
183                 if (substr($line,0,1) == "!") {
184                     $cell = HTML::th();   // Header
185                     $thead = HTML::thead();
186                 } else { 
187                     $cell = HTML::td();
188                     if (!isset($tbody)) $tbody = HTML::tbody();
189                 }
190                 $line = substr($line, 1);
191
192                 // If there is a "|" in the line, the start of line
193                 // (before the "|") is made of attributes.
194                 // The end of the line (after the "|") is the cell content
195                 // This is not true if the pipe is inside []
196                 // | [foo|bar] 
197                 // The following cases must work:
198                 // | foo    
199                 // | [foo|bar]
200                 // | class="xxx" | foo
201                 // | class="xxx" | [foo|bar]
202                 $pospipe = strpos($line, "|");
203                 $posbracket = strpos($line, "[");
204                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
205                     $attrs = parse_attributes(substr($line, 0, $pospipe));
206                     foreach ($attrs as $key => $value) {
207                         if (in_array ($key, array("id", "class", "title", "style",
208                                                   "colspan", "rowspan", "width", "height",
209                                                   "bgcolor", "align", "valign"))) {
210                             $cell->setAttr($key, $value);
211                         }
212                     }
213                     $line=substr($line, $pospipe+1);
214                     if (is_numeric(trim($content))) {
215                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
216                     } else {
217                         $cell->pushContent(TransformText(trim($content), $markup, $basepage));
218                     }
219                     continue;
220                 }
221             }
222             if (isset($row) and isset($cell)) {
223                 $line = str_replace("?\>", "?>", $line);
224                 $line = str_replace("\~", "~", $line);
225                 if (empty($content)) $content = '';
226                 $content .= $line . "\n";
227             }
228         }
229         if (isset($row)) {
230             if (isset($cell)) {
231                 if (isset($content)) {
232                     if (is_numeric(trim($content))) {
233                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
234                     } else {
235                         $cell->pushContent(TransformText(trim($content), $markup, $basepage));
236                     }
237
238                 }
239                 $row->pushContent($cell);
240             }
241             $tbody->pushContent($row);
242             $table->pushContent($tbody);
243         }
244         return $table;
245     }
246 }
247
248 // For emacs users
249 // Local Variables:
250 // mode: php
251 // tab-width: 8
252 // c-basic-offset: 4
253 // c-hanging-comment-ender-p: nil
254 // indent-tabs-mode: nil
255 // End:
256 ?>