5 A PhpWiki plugin that allows insertion of tables using a Mediawiki-like
9 * Copyright (C) 2003 Sameer D. Sahasrabuddhe
10 * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
11 * Copyright (C) 2008 Alcatel-Lucent
13 * This file is part of PhpWiki.
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.
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.
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
31 * Standard Alcatel-Lucent disclaimer for contributing to open source
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
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
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) {
74 include_once("lib/BlockParser.php");
75 // MediawikiTablePlugin markup is new.
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);
84 $lines = preg_split('/\n/', $argstr);
85 $table = HTML::table();
87 if (substr($lines[0],0,2) == "{|") {
89 $lines[0] = substr($lines[0],2);
91 if (($lines[0][0] != '|') and ($lines[0][0] != '!')) {
92 $line = array_shift($lines);
93 $attrs = $this->_parse_attr($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);
104 foreach ($lines as $line){
105 if (substr($line,0,2) == "|}") {
109 if (substr($line,0,2) == "|-") {
112 if (isset($content)) {
113 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
116 $row->pushContent($cell);
120 $thead->pushContent($row);
121 $table->pushContent($thead);
123 $tbody = HTML::tbody();
125 $tbody->pushContent($row);
129 $attrs = $this->_parse_attr(substr($line,2));
130 foreach ($attrs as $key => $value) {
131 if (in_array ($key, array("id", "class", "title", "style",
132 "bgcolor", "align", "valign"))) {
133 $row->setAttr($key, $value);
140 if (substr($line,0,2) == "|+") {
142 $caption = HTML::caption();
143 $line = substr($line,2);
144 $pospipe = strpos($line, "|");
145 $posbracket = strpos($line, "[");
146 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
147 $attrs = $this->_parse_attr(substr($line, 0, $pospipe));
148 foreach ($attrs as $key => $value) {
149 if (in_array ($key, array("id", "class", "title", "style",
151 $caption->setAttr($key, $value);
154 $line=substr($line, $pospipe+1);
157 $caption->pushContent(trim($line));
158 $table->pushContent($caption);
161 if (((substr($line,0,1) == "|") or (substr($line,0,1) == "!")) and isset($row)) {
163 if (isset ($content)) {
164 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
167 $row->pushContent($cell);
169 if (substr($line,0,1) == "!") {
170 $cell = HTML::th(); // Header
171 $thead = HTML::thead();
174 if (!isset($tbody)) $tbody = HTML::tbody();
176 $line = substr($line, 1);
178 // If there is a "|" in the line, the start of line
179 // (before the "|") is made of attributes.
180 // The end of the line (after the "|") is the cell content
181 // This is not true if the pipe is inside []
183 // The following cases must work:
186 // | class="xxx" | foo
187 // | class="xxx" | [foo|bar]
188 $pospipe = strpos($line, "|");
189 $posbracket = strpos($line, "[");
190 if (($pospipe !== false) && (($posbracket === false) || ($posbracket > $pospipe))) {
191 $attrs = $this->_parse_attr(substr($line, 0, $pospipe));
192 foreach ($attrs as $key => $value) {
193 if (in_array ($key, array("id", "class", "title", "style",
194 "colspan", "rowspan", "width", "height",
195 "bgcolor", "align", "valign"))) {
196 $cell->setAttr($key, $value);
199 $line=substr($line, $pospipe+1);
200 $cell->pushContent(TransformText(trim($line), $markup, $basepage));
204 if (isset($row) and isset($cell)) {
205 $line = str_replace("?\>", "?>", $line);
206 $line = str_replace("\~", "~", $line);
207 if (empty($content)) $content = '';
208 $content .= $line . "\n";
214 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
215 $row->pushContent($cell);
217 $tbody->pushContent($row);
218 $table->pushContent($tbody);
223 function _parse_attr($line) {
224 // We allow attributes with or without quotes (")
225 // border=1, cellpadding="5"
226 // style="font-family: sans-serif; border-top:1px solid #dddddd;"
227 // What will not work is style with comma inside, e. g.
228 // style="font-family: Verdana, Arial, Helvetica, sans-serif"
229 $attr_chunks = preg_split("/\s*,\s*/", strtolower($line));
231 foreach ($attr_chunks as $attr_pair) {
232 if (empty($attr_pair)) continue;
233 $key_val = preg_split("/\s*=\s*/", $attr_pair);
234 if (!empty($key_val[1]))
235 $options[trim($key_val[0])] = trim(str_replace("\"", "", $key_val[1]));
246 // c-hanging-comment-ender-p: nil
247 // indent-tabs-mode: nil