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