]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RichTable.php
Link in the last cell of a table was not displayed as a link
[SourceForge/phpwiki.git] / lib / plugin / RichTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4   RichTablePlugin
5   A PhpWiki plugin that allows insertion of tables using a richer syntax.
6   Src: http://www.it.iitb.ac.in/~sameerds/phpwiki/index.php/RichTablePlugin
7   Docs: http://phpwiki.org/RichTablePlugin
8 */
9 /* 
10  * Copyright (C) 2003 Sameer D. Sahasrabuddhe
11  * Copyright (C) 2005 $ThePhpWikiProgrammingTeam
12  * Copyright (C) 2008 Marc-Etienne Vargenau, Alcatel-Lucent
13  *
14  * This file is part of PhpWiki.
15  *
16  * PhpWiki is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  * 
21  * PhpWiki is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * 
26  * You should have received a copy of the GNU General Public License
27  * along with PhpWiki; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 */
30
31 // error_reporting (E_ALL & ~E_NOTICE);
32
33 class WikiPlugin_RichTable
34 extends WikiPlugin
35 {
36     function getName() {
37         return _("RichTable");
38     }
39
40     function getDescription() {
41       return _("Layout tables using a very rich markup style.");
42     }
43
44     function getDefaultArguments() {
45         return array();
46     }
47
48     function getVersion() {
49         return preg_replace("/[Revision: $]/", '',
50                             "\$Revision$");
51     }
52
53     function run($dbi, $argstr, &$request, $basepage) {
54         global $WikiTheme;
55         include_once("lib/BlockParser.php");
56         // RichTablePlugin markup is new.
57         $markup = 2.0; 
58
59         $lines = preg_split('/\n/', $argstr);
60         $table = HTML::table();
61  
62         if ($lines[0][0] == '*') {
63             $line = substr(array_shift($lines),1);
64             $attrs = $this->_parse_attr($line);
65             foreach ($attrs as $key => $value) {
66                 if (in_array ($key, array("id", "class", "title", "style",
67                                           "bgcolor", "frame", "rules", "border",
68                                           "cellspacing", "cellpadding",
69                                           "summary", "align", "width"))) {
70                     $table->setAttr($key, $value);
71                 }
72             }
73         }
74         
75         foreach ($lines as $line){
76             if (substr($line,0,1) == "-") {
77                 if (isset($row)) {
78                     if (isset($cell)) {
79                         if (isset($content)) {
80                             $cell->pushContent(TransformText($content, $markup, $basepage));
81                             unset($content);
82                         }
83                         $row->pushContent($cell);
84                         unset($cell);
85                     }
86                     $table->pushContent($row);
87                 }       
88                 $row = HTML::tr();
89                 $attrs = $this->_parse_attr(substr($line,1));
90                 foreach ($attrs as $key => $value) {
91                     if (in_array ($key, array("id", "class", "title", "style",
92                                               "bgcolor", "align", "valign"))) {
93                         $row->setAttr($key, $value);
94                     }
95                 }
96                 continue;
97             }
98             if (substr($line,0,1) == "|" and isset($row)) {
99                 if (isset($cell)) {
100                     if (isset ($content)) {
101                         $cell->pushContent(TransformText($content, $markup, $basepage));
102                         unset($content);
103                     }
104                     $row->pushContent($cell);
105                 }
106                 $cell = HTML::td();
107                 $line = substr($line, 1);
108                 if ($line[0] == "*" ) {
109                     $attrs = $this->_parse_attr(substr($line,1));
110                     foreach ($attrs as $key => $value) {
111                         if (in_array ($key, array("id", "class", "title", "style",
112                                                   "colspan", "rowspan", "width", "height",
113                                                   "bgcolor", "align", "valign"))) {
114                             $cell->setAttr($key, $value);
115                         }
116                     }
117                     continue;
118                 } 
119             } 
120             if (isset($row) and isset($cell)) {
121                 $line = str_replace("?\>", "?>", $line);
122                 $line = str_replace("\~", "~", $line);
123                 if (empty($content)) $content = '';
124                 $content .= $line . "\n";
125             }
126         }
127         if (isset($row)) {
128             if (isset($cell)) {
129                 if (isset($content))
130                     $cell->pushContent(TransformText($content, $markup, $basepage));
131                 $row->pushContent($cell);
132             }
133             $table->pushContent($row);
134         }
135         return $table;
136     }
137
138     function _parse_attr($line) {
139         // We allow attributes with or without quotes (")
140         // border=1, cellpadding="5"
141         // style="font-family: sans-serif; border-top:1px solid #dddddd;
142         // What will not work is style with comma inside, e. g.
143         // style="font-family: Verdana, Arial, Helvetica, sans-serif"
144         $attr_chunks = preg_split("/\s*,\s*/", strtolower($line));
145         $options = array();
146         foreach ($attr_chunks as $attr_pair) {
147             if (empty($attr_pair)) continue;
148             $key_val = preg_split("/\s*=\s*/", $attr_pair);
149             if (!empty($key_val[1]))
150                 $options[trim($key_val[0])] = trim(str_replace("\"", "", $key_val[1]));
151         }
152         return $options;
153     }
154 }
155
156 // For emacs users
157 // Local Variables:
158 // mode: php
159 // tab-width: 8
160 // c-basic-offset: 4
161 // c-hanging-comment-ender-p: nil
162 // indent-tabs-mode: nil
163 // End:
164 ?>