]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MediawikiTable.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[SourceForge/phpwiki.git] / lib / plugin / MediawikiTable.php
1 <?php
2
3 /*
4  * Copyright (C) 2003 Sameer D. Sahasrabuddhe
5  * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
6  * Copyright (C) 2008-2010 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 along
21  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 getDescription()
56     {
57         return _("Layout tables using a Mediawiki-like markup style.");
58     }
59
60     function getDefaultArguments()
61     {
62         return array();
63     }
64
65     function run($dbi, $argstr, &$request, $basepage)
66     {
67         include_once 'lib/BlockParser.php';
68
69         // We allow the compact Mediawiki syntax with:
70         // - multiple cells on the same line (separated by "||"),
71         // - multiple header cells on the same line (separated by "!!").
72         $argstr = str_replace("||", "\n| ", $argstr);
73         $argstr = str_replace("!!", "\n! ", $argstr);
74
75         $lines = explode("\n", $argstr);
76
77         $table = HTML::table();
78         $caption = HTML::caption();
79         $thead = HTML::thead();
80         $tbody = HTML::tbody();
81
82         // Do we need a <thead>?
83         // 0 = unknown
84         // 1 = inside (parsing cells)
85         // 2 = false (no thead, only tbody)
86         // 3 = true (there is a thead)
87         $theadstatus = 0;
88
89         // We always generate an Id for the table.
90         // This is convenient for tables of class "sortable".
91         // If user provides an Id, the generated Id will be overwritten below.
92         $table->setAttr("id", GenerateId("MediawikiTable"));
93
94         if (substr($lines[0], 0, 2) == "{|") {
95             // Start of table
96             $lines[0] = substr($lines[0], 2);
97         }
98         if (($lines[0][0] != '|') and ($lines[0][0] != '!')) {
99             $line = array_shift($lines);
100             $attrs = parse_attributes($line);
101             foreach ($attrs as $key => $value) {
102                 if (in_array($key, array("id", "class", "title", "style",
103                     "bgcolor", "frame", "rules", "border",
104                     "cellspacing", "cellpadding",
105                     "summary", "align", "width"))
106                 ) {
107                     $table->setAttr($key, $value);
108                 }
109             }
110         }
111
112         if (count($lines) == 1) { // empty table, we only have closing "|}" line
113             return HTML::raw('');
114         }
115
116         foreach ($lines as $line) {
117             if (substr($line, 0, 2) == "|}") {
118                 // End of table
119                 continue;
120             }
121             if (substr($line, 0, 2) == "|-") {
122                 if (isset($row)) {
123                     if (isset($cell)) {
124                         if (isset($content)) {
125                             if (is_numeric(trim($content))) {
126                                 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
127                             } else {
128                                 $cell->pushContent(TransformText(trim($content), $basepage));
129                             }
130                             unset($content);
131                         }
132                         $row->pushContent($cell);
133                         unset($cell);
134                     }
135                     if (!empty($row->_content)) {
136                         if ($theadstatus == 1) { // inside
137                             $theadstatus = 3; // true
138                             $thead->pushContent($row);
139                         } else {
140                             $tbody->pushContent($row);
141                         }
142                     }
143                 }
144                 $row = HTML::tr();
145                 $attrs = parse_attributes(substr($line, 2));
146                 foreach ($attrs as $key => $value) {
147                     if (in_array($key, array("id", "class", "title", "style",
148                         "bgcolor", "align", "valign"))
149                     ) {
150                         $row->setAttr($key, $value);
151                     }
152                 }
153                 continue;
154             }
155
156             // Table summary
157             if (substr($line, 0, 2) == "|=") {
158                 $line = substr($line, 2);
159                 $table->setAttr("summary", trim($line));
160             }
161
162             // Table caption
163             if (substr($line, 0, 2) == "|+") {
164
165                 $line = substr($line, 2);
166                 $pospipe = strpos($line, "|");
167                 $posbracket = strpos($line, "[");
168                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
169                     $attrs = parse_attributes(substr($line, 0, $pospipe));
170                     foreach ($attrs as $key => $value) {
171                         if (in_array($key, array("id", "class", "title", "style",
172                             "align", "lang"))
173                         ) {
174                             $caption->setAttr($key, $value);
175                         }
176                     }
177                     $line = substr($line, $pospipe + 1);
178                 }
179
180                 $caption->setContent(TransformInline(trim($line)));
181             }
182
183             if (((substr($line, 0, 1) == "|") or (substr($line, 0, 1) == "!")) and isset($row)) {
184                 if (isset($cell)) {
185                     if (isset ($content)) {
186                         if (is_numeric(trim($content))) {
187                             $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
188                         } else {
189                             $cell->pushContent(TransformText(trim($content), $basepage));
190                         }
191                         unset($content);
192                     }
193                     $row->pushContent($cell);
194                 }
195                 if (substr($line, 0, 1) == "!") {
196                     if ($theadstatus == 0) { // unknown
197                         $theadstatus = 1; // inside
198                     }
199                     $cell = HTML::th(); // Header
200                 } else {
201                     if ($theadstatus == 1) { // inside
202                         $theadstatus = 2; // false
203                     }
204                     $cell = HTML::td();
205                 }
206                 $line = substr($line, 1);
207
208                 // If there is a "|" in the line, the start of line
209                 // (before the "|") is made of attributes.
210                 // The end of the line (after the "|") is the cell content
211                 // This is not true if the pipe is inside [], {{}} or {{{}}}
212                 // | [foo|bar]
213                 // The following cases must work:
214                 // | foo
215                 // | [foo|bar]
216                 // | class="xxx" | foo
217                 // | class="xxx" | [foo|bar]
218                 // | {{tmpl|arg=val}}
219                 // | {{image.png|alt}}
220                 // | {{{ xxx | yyy }}}
221                 $pospipe = strpos($line, "|");
222                 $posbracket = strpos($line, "[");
223                 $poscurly = strpos($line, "{");
224                 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe)) && (($poscurly === false) || ($poscurly > $pospipe))) {
225                     $attrs = parse_attributes(substr($line, 0, $pospipe));
226                     foreach ($attrs as $key => $value) {
227                         if (in_array($key, array("id", "class", "title", "style", "scope",
228                             "colspan", "rowspan", "width", "height",
229                             "bgcolor", "align", "valign"))
230                         ) {
231                             $cell->setAttr($key, $value);
232                         }
233                     }
234                     $line = substr($line, $pospipe + 1);
235                     if (is_numeric(trim($line))) {
236                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($line)));
237                     } else {
238                         $cell->pushContent(TransformText(trim($line), $basepage));
239                     }
240                     continue;
241                 }
242             }
243             if (isset($row) and isset($cell)) {
244                 $line = str_replace("?\>", "?>", $line);
245                 $line = str_replace("\~", "~", $line);
246                 if (empty($content)) $content = '';
247                 $content .= $line . "\n";
248             }
249         }
250         if (isset($row)) {
251             if (isset($cell)) {
252                 if (isset($content)) {
253                     if (is_numeric(trim($content))) {
254                         $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
255                     } else {
256                         $cell->pushContent(TransformText(trim($content), $basepage));
257                     }
258
259                 }
260                 $row->pushContent($cell);
261             }
262             // If user put and extra "|-" without cells just before "|}"
263             // we ignore it to get valid XHTML code
264             if (!empty($row->_content)) {
265                 $tbody->pushContent($row);
266             }
267         }
268         if (!empty($caption->_content)) {
269             $table->pushContent($caption);
270         }
271         if (!empty($thead->_content)) {
272             $table->pushContent($thead);
273         }
274         if (!empty($tbody->_content)) {
275             $table->pushContent($tbody);
276         }
277         if (!empty($table->_content)) {
278             return $table;
279         } else {
280             return HTML::raw('');
281         }
282     }
283 }
284
285 // Local Variables:
286 // mode: php
287 // tab-width: 8
288 // c-basic-offset: 4
289 // c-hanging-comment-ender-p: nil
290 // indent-tabs-mode: nil
291 // End: