]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/MediawikiTable.php
Implemented Mediawiki-like syntax for tables
[SourceForge/phpwiki.git] / lib / plugin / MediawikiTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id: MediawikiTable.php,v 1.1 2008-01-31 20:40:10 vargenau Exp $');
3 /**
4   MediawikiTablePlugin
5   A PhpWiki plugin that allows insertion of tables using a Mediawiki-like
6   syntax.
7 */
8 /*
9  * Copyright (C) 2003 Sameer D. Sahasrabuddhe
10  * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
11  * Copyright (C) 2008 Alcatel-Lucent
12  *
13  * This file is part of PhpWiki.
14  *
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.
19  *
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.
24  *
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
28 */
29
30 /*
31  * Standard Alcatel-Lucent disclaimer for contributing to open source
32  *
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 
38  * Contribution.
39  *
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 
49  * ALONE BASIS."
50  */
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: 1.1 $");
70     }
71
72     function run($dbi, $argstr, &$request, $basepage) {
73         global $WikiTheme;
74         include_once("lib/BlockParser.php");
75         // MediawikiTablePlugin markup is new.
76         $markup = 2.0;
77
78         // We allow the compact Mediawiki syntax with
79         // multiple cells on the same line (separated by "||").
80         $argstr = str_replace("||", "\n|", $argstr);
81
82         $lines = preg_split('/\n/', $argstr);
83         $table = HTML::table();
84
85         if ($lines[0][0] != '|') {
86             $line = array_shift($lines);
87             $attrs = $this->_parse_attr($line);
88             foreach ($attrs as $key => $value) {
89                 if (in_array ($key, array("id", "class", "title", "style",
90                                           "bgcolor", "frame", "rules", "border",
91                                           "cellspacing", "cellpadding",
92                                           "summary", "align", "width"))) {
93                     $table->setAttr($key, $value);
94                 }
95             }
96         }
97
98         foreach ($lines as $line){
99             if (substr($line,0,2) == "|-") {
100                 if (isset($row)) {
101                     if (isset($cell)) {
102                         if (isset($content)) {
103                             $cell->pushContent(TransformText(trim($content), $markup, $basepage));
104                             unset($content);
105                         }
106                         $row->pushContent($cell);
107                         unset($cell);
108                     }
109                     $table->pushContent($row);
110                 }
111                 $row = HTML::tr();
112                 $attrs = $this->_parse_attr(substr($line,2));
113                 foreach ($attrs as $key => $value) {
114                     if (in_array ($key, array("id", "class", "title", "style",
115                                               "bgcolor", "align", "valign"))) {
116                         $row->setAttr($key, $value);
117                     }
118                 }
119                 continue;
120             }
121
122             // Table caption
123             if (substr($line,0,2) == "|+") {
124
125                 $caption = HTML::caption();
126                 $line = substr($line,2);
127                 $pospipe = strpos($line, "|");
128                 $posbracket = strpos($line, "[");
129                 if (($pospipe) && ((!$posbracket) || ($posbracket > $pospipe))) {
130                     $attrs = $this->_parse_attr(substr($line, 0, $pospipe));
131                     foreach ($attrs as $key => $value) {
132                         if (in_array ($key, array("id", "class", "title", "style",
133                                                   "align", "lang"))) {
134                             $caption->setAttr($key, $value);
135                         }
136                     }
137                     $line=substr($line, $pospipe+1);
138                 }
139
140                 $caption->pushContent(trim($line));
141                 $table->pushContent($caption);
142             }
143
144             if (substr($line,0,1) == "|" and isset($row)) {
145                 if (isset($cell)) {
146                     if (isset ($content)) {
147                         $cell->pushContent(TransformText(trim($content), $markup, $basepage));
148                         unset($content);
149                     }
150                     $row->pushContent($cell);
151                 }
152                 $cell = HTML::td();
153                 $line = substr($line, 1);
154
155                 // If there is a "|" in the line, the start of line
156                 // (before the "|") is made of attributes.
157                 // The end of the line (after the "|") is the cell content
158                 // This is not true if the pipe is inside []
159                 // | [foo|bar] 
160                 // The following cases must work:
161                 // | foo    
162                 // | [foo|bar]
163                 // | class="xxx" | foo
164                 // | class="xxx" | [foo|bar]
165                 $pospipe = strpos($line, "|");
166                 $posbracket = strpos($line, "[");
167                 if (($pospipe) && ((!$posbracket) || ($posbracket > $pospipe))) {
168                     $attrs = $this->_parse_attr(substr($line, 0, $pospipe));
169                     foreach ($attrs as $key => $value) {
170                         if (in_array ($key, array("id", "class", "title", "style",
171                                                   "colspan", "rowspan", "width", "height",
172                                                   "bgcolor", "align", "valign"))) {
173                             $cell->setAttr($key, $value);
174                         }
175                     }
176                     $line=substr($line, $pospipe+1);
177                     $cell->pushContent(TransformText(trim($line), $markup, $basepage));
178                     continue;
179                 }
180             }
181             if (isset($row) and isset($cell)) {
182                 $line = str_replace("?\>", "?>", $line);
183                 $line = str_replace("\~", "~", $line);
184                 if (empty($content)) $content = '';
185                 $content .= $line . "\n";
186             }
187         }
188         if (isset($row)) {
189             if (isset($cell)) {
190                 if (isset($content))
191                     $cell->pushContent(TransformText(trim($content)));
192                 $row->pushContent($cell);
193             }
194             $table->pushContent($row);
195         }
196         return $table;
197     }
198
199     function _parse_attr($line) {
200         // We allow attributes with or without quotes (")
201         // border=1, cellpadding="5"
202         // style="font-family: sans-serif; border-top:1px solid #dddddd;"
203         // What will not work is style with comma inside, e. g.
204         // style="font-family: Verdana, Arial, Helvetica, sans-serif"
205         $attr_chunks = preg_split("/\s*,\s*/", strtolower($line));
206         $options = array();
207         foreach ($attr_chunks as $attr_pair) {
208             if (empty($attr_pair)) continue;
209             $key_val = preg_split("/\s*=\s*/", $attr_pair);
210             if (!empty($key_val[1]))
211                 $options[trim($key_val[0])] = trim(str_replace("\"", "", $key_val[1]));
212         }
213         return $options;
214     }
215 }
216
217 // $Log: not supported by cvs2svn $
218 //
219
220 // For emacs users
221 // Local Variables:
222 // mode: php
223 // tab-width: 8
224 // c-basic-offset: 4
225 // c-hanging-comment-ender-p: nil
226 // indent-tabs-mode: nil
227 // End:
228 ?>