4 * Copyright (C) 2003 Sameer D. Sahasrabuddhe
5 * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
6 * Copyright (C) 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
8 * This file is part of PhpWiki.
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.
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.
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
26 * Standard Alcatel-Lucent disclaimer for contributing to open source
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
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
48 * MediawikiTablePlugin
49 * A PhpWiki plugin that allows insertion of tables using a Mediawiki-like
52 class WikiPlugin_MediawikiTable
56 return _("MediawikiTable");
59 function getDescription() {
60 return _("Layout tables using a Mediawiki-like markup style.");
63 function getDefaultArguments() {
67 function getVersion() {
68 return preg_replace("/[Revision: $]/", '',
72 function run($dbi, $argstr, &$request, $basepage) {
73 include_once("lib/BlockParser.php");
74 // MediawikiTablePlugin markup is new.
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);
83 $lines = preg_split('/\n/', $argstr);
84 $table = HTML::table();
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"));
91 if (substr($lines[0],0,2) == "{|") {
93 $lines[0] = substr($lines[0],2);
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);
108 if (count($lines) == 1) { // empty table, we only have closing "|}" line
109 return HTML::raw('');
112 foreach ($lines as $line){
113 if (substr($line,0,2) == "|}") {
117 if (substr($line,0,2) == "|-") {
120 if (isset($content)) {
121 if (is_numeric(trim($content))) {
122 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
124 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
128 $row->pushContent($cell);
132 $thead->pushContent($row);
133 $table->pushContent($thead);
135 $tbody = HTML::tbody();
137 $tbody->pushContent($row);
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);
152 if (substr($line,0,2) == "|=") {
153 $line = substr($line,2);
154 $table->setAttr("summary", trim($line));
158 if (substr($line,0,2) == "|+") {
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",
169 $caption->setAttr($key, $value);
172 $line=substr($line, $pospipe+1);
175 $caption->pushContent(trim($line));
176 $table->pushContent($caption);
179 if (((substr($line,0,1) == "|") or (substr($line,0,1) == "!")) and isset($row)) {
181 if (isset ($content)) {
182 if (is_numeric(trim($content))) {
183 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
185 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
189 $row->pushContent($cell);
191 if (substr($line,0,1) == "!") {
192 $cell = HTML::th(); // Header
193 $thead = HTML::thead();
196 if (!isset($tbody)) $tbody = HTML::tbody();
198 $line = substr($line, 1);
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 {{{}}}
205 // The following cases must work:
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);
225 $line=substr($line, $pospipe+1);
226 if (is_numeric(trim($line))) {
227 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($line)));
229 $cell->pushContent(TransformText(trim($line), $markup, $basepage));
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";
243 if (isset($content)) {
244 if (is_numeric(trim($content))) {
245 $cell->pushContent(HTML::p(array('style' => "text-align:right"), trim($content)));
247 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
251 $row->pushContent($cell);
253 $tbody->pushContent($row);
254 $table->pushContent($tbody);
265 // c-hanging-comment-ender-p: nil
266 // indent-tabs-mode: nil