]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MediawikiTable.php
Add fieldset
[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         // We always generate an Id for the table.
88         // This is convenient for tables of class "sortable".
89         // If user provides an Id, the generated Id will be overwritten below.
90         $table->setAttr("id", GenerateId("MediawikiTable"));
91
92         if (substr($lines[0],0,2) == "{|") {
93             // Start of table
94             $lines[0] = substr($lines[0],2);
95         }
96         if (($lines[0][0] != '|') and ($lines[0][0] != '!')) {
97             $line = array_shift($lines);
98             $attrs = parse_attributes($line);
99             foreach ($attrs as $key => $value) {
100                 if (in_array ($key, array("id", "class", "title", "style",
101                                           "bgcolor", "frame", "rules", "border",
102                                           "cellspacing", "cellpadding",
103                                           "summary", "align", "width"))) {
104                     $table->setAttr($key, $value);
105                 }
106             }
107         }
108
109         foreach ($lines as $line){
110             if (substr($line,0,2) == "|}") {
111                 // End of table
112                 continue;
113             }
114             if (substr($line,0,2) == "|-") {
115                 if (isset($row)) {
116                     if (isset($cell)) {
117                         if (isset($content)) {
118                             if (is_numeric(trim($content))) {
119                                 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
120                             } else {
121                                 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
122                             }
123                             unset($content);
124                         }
125                         $row->pushContent($cell);
126                         unset($cell);
127                     }
128                     if (isset($thead)) {
129                         $thead->pushContent($row);
130                         $table->pushContent($thead);
131                         unset($thead);
132                         $tbody = HTML::tbody();
133                     } else {
134                         $tbody->pushContent($row);
135                     }
136                 }
137                 $row = HTML::tr();
138                 $attrs = parse_attributes(substr($line,2));
139                 foreach ($attrs as $key => $value) {
140                     if (in_array ($key, array("id", "class", "title", "style",
141                                               "bgcolor", "align", "valign"))) {
142                         $row->setAttr($key, $value);
143                     }
144                 }
145                 continue;
146             }
147
148             // Table summary
149             if (substr($line,0,2) == "|=") {
150                 $line = substr($line,2);
151                 $table->setAttr("summary", trim($line));
152             }
153
154             // Table caption
155             if (substr($line,0,2) == "|+") {
156
157                 $caption = HTML::caption();
158                 $line = substr($line,2);
159                 $pospipe = strpos($line, "|");
160                 $posbracket = strpos($line, "[");
161                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
162                     $attrs = parse_attributes(substr($line, 0, $pospipe));
163                     foreach ($attrs as $key => $value) {
164                         if (in_array ($key, array("id", "class", "title", "style",
165                                                   "align", "lang"))) {
166                             $caption->setAttr($key, $value);
167                         }
168                     }
169                     $line=substr($line, $pospipe+1);
170                 }
171
172                 $caption->pushContent(trim($line));
173                 $table->pushContent($caption);
174             }
175
176             if (((substr($line,0,1) == "|") or (substr($line,0,1) == "!")) and isset($row)) {
177                 if (isset($cell)) {
178                     if (isset ($content)) {
179                         if (is_numeric(trim($content))) {
180                             $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
181                         } else {
182                             $cell->pushContent(TransformText(trim($content), $markup, $basepage));
183                         }
184                         unset($content);
185                     }
186                     $row->pushContent($cell);
187                 }
188                 if (substr($line,0,1) == "!") {
189                     $cell = HTML::th();   // Header
190                     $thead = HTML::thead();
191                 } else { 
192                     $cell = HTML::td();
193                     if (!isset($tbody)) $tbody = HTML::tbody();
194                 }
195                 $line = substr($line, 1);
196
197                 // If there is a "|" in the line, the start of line
198                 // (before the "|") is made of attributes.
199                 // The end of the line (after the "|") is the cell content
200                 // This is not true if the pipe is inside [], {{}} or {{{}}}
201                 // | [foo|bar] 
202                 // The following cases must work:
203                 // | foo    
204                 // | [foo|bar]
205                 // | class="xxx" | foo
206                 // | class="xxx" | [foo|bar]
207                 // | {{tmpl|arg=val}}
208                 // | {{image.png|alt}}
209                 // | {{{ xxx | yyy }}}
210                 $pospipe = strpos($line, "|");
211                 $posbracket = strpos($line, "[");
212                 $poscurly = strpos($line, "{");
213                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe)) && (($poscurly === false) || ($poscurly > $pospipe))) {
214                     $attrs = parse_attributes(substr($line, 0, $pospipe));
215                     foreach ($attrs as $key => $value) {
216                         if (in_array ($key, array("id", "class", "title", "style",
217                                                   "colspan", "rowspan", "width", "height",
218                                                   "bgcolor", "align", "valign"))) {
219                             $cell->setAttr($key, $value);
220                         }
221                     }
222                     $line=substr($line, $pospipe+1);
223                     if (is_numeric(trim($line))) {
224                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($line)));
225                     } else {
226                         $cell->pushContent(TransformText(trim($line), $markup, $basepage));
227                     }
228                     continue;
229                 }
230             }
231             if (isset($row) and isset($cell)) {
232                 $line = str_replace("?\>", "?>", $line);
233                 $line = str_replace("\~", "~", $line);
234                 if (empty($content)) $content = '';
235                 $content .= $line . "\n";
236             }
237         }
238         if (isset($row)) {
239             if (isset($cell)) {
240                 if (isset($content)) {
241                     if (is_numeric(trim($content))) {
242                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
243                     } else {
244                         $cell->pushContent(TransformText(trim($content), $markup, $basepage));
245                     }
246
247                 }
248                 $row->pushContent($cell);
249             }
250             $tbody->pushContent($row);
251             $table->pushContent($tbody);
252         }
253         return $table;
254     }
255 }
256
257 // For emacs users
258 // Local Variables:
259 // mode: php
260 // tab-width: 8
261 // c-basic-offset: 4
262 // c-hanging-comment-ender-p: nil
263 // indent-tabs-mode: nil
264 // End:
265 ?>