2 rcs_id('$Id: MediawikiTable.php,v 1.2 2008-04-04 18:13:49 vargenau Exp $');
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 $argstr = str_replace("||", "\n|", $argstr);
82 $lines = preg_split('/\n/', $argstr);
83 $table = HTML::table();
84 $tbody = HTML::tbody();
86 if ($lines[0][0] != '|') {
87 $line = array_shift($lines);
88 $attrs = $this->_parse_attr($line);
89 foreach ($attrs as $key => $value) {
90 if (in_array ($key, array("id", "class", "title", "style",
91 "bgcolor", "frame", "rules", "border",
92 "cellspacing", "cellpadding",
93 "summary", "align", "width"))) {
94 $table->setAttr($key, $value);
99 foreach ($lines as $line){
100 if (substr($line,0,2) == "|-") {
103 if (isset($content)) {
104 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
107 $row->pushContent($cell);
110 $tbody->pushContent($row);
113 $attrs = $this->_parse_attr(substr($line,2));
114 foreach ($attrs as $key => $value) {
115 if (in_array ($key, array("id", "class", "title", "style",
116 "bgcolor", "align", "valign"))) {
117 $row->setAttr($key, $value);
124 if (substr($line,0,2) == "|+") {
126 $caption = HTML::caption();
127 $line = substr($line,2);
128 $pospipe = strpos($line, "|");
129 $posbracket = strpos($line, "[");
130 if (($pospipe) && ((!$posbracket) || ($posbracket > $pospipe))) {
131 $attrs = $this->_parse_attr(substr($line, 0, $pospipe));
132 foreach ($attrs as $key => $value) {
133 if (in_array ($key, array("id", "class", "title", "style",
135 $caption->setAttr($key, $value);
138 $line=substr($line, $pospipe+1);
141 $caption->pushContent(trim($line));
142 $table->pushContent($caption);
145 if (substr($line,0,1) == "|" and isset($row)) {
147 if (isset ($content)) {
148 $cell->pushContent(TransformText(trim($content), $markup, $basepage));
151 $row->pushContent($cell);
154 $line = substr($line, 1);
156 // If there is a "|" in the line, the start of line
157 // (before the "|") is made of attributes.
158 // The end of the line (after the "|") is the cell content
159 // This is not true if the pipe is inside []
161 // The following cases must work:
164 // | class="xxx" | foo
165 // | class="xxx" | [foo|bar]
166 $pospipe = strpos($line, "|");
167 $posbracket = strpos($line, "[");
168 if (($pospipe) && ((!$posbracket) || ($posbracket > $pospipe))) {
169 $attrs = $this->_parse_attr(substr($line, 0, $pospipe));
170 foreach ($attrs as $key => $value) {
171 if (in_array ($key, array("id", "class", "title", "style",
172 "colspan", "rowspan", "width", "height",
173 "bgcolor", "align", "valign"))) {
174 $cell->setAttr($key, $value);
177 $line=substr($line, $pospipe+1);
178 $cell->pushContent(TransformText(trim($line), $markup, $basepage));
182 if (isset($row) and isset($cell)) {
183 $line = str_replace("?\>", "?>", $line);
184 $line = str_replace("\~", "~", $line);
185 if (empty($content)) $content = '';
186 $content .= $line . "\n";
192 $cell->pushContent(TransformText(trim($content)));
193 $row->pushContent($cell);
195 $tbody->pushContent($row);
196 $table->pushContent($tbody);
201 function _parse_attr($line) {
202 // We allow attributes with or without quotes (")
203 // border=1, cellpadding="5"
204 // style="font-family: sans-serif; border-top:1px solid #dddddd;"
205 // What will not work is style with comma inside, e. g.
206 // style="font-family: Verdana, Arial, Helvetica, sans-serif"
207 $attr_chunks = preg_split("/\s*,\s*/", strtolower($line));
209 foreach ($attr_chunks as $attr_pair) {
210 if (empty($attr_pair)) continue;
211 $key_val = preg_split("/\s*=\s*/", $attr_pair);
212 if (!empty($key_val[1]))
213 $options[trim($key_val[0])] = trim(str_replace("\"", "", $key_val[1]));
219 // $Log: not supported by cvs2svn $
220 // Revision 1.1 2008/01/31 20:40:10 vargenau
221 // Implemented Mediawiki-like syntax for tables
230 // c-hanging-comment-ender-p: nil
231 // indent-tabs-mode: nil