]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/RichTable.php
Activated Id substitution for Subversion
[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  *
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 // error_reporting (E_ALL & ~E_NOTICE);
31
32 class WikiPlugin_RichTable
33 extends WikiPlugin
34 {
35     function getName() {
36         return _("RichTable");
37     }
38
39     function getDescription() {
40       return _("Layout tables using a very rich markup style.");
41     }
42
43     function getDefaultArguments() {
44         return array();
45     }
46
47     function getVersion() {
48         return preg_replace("/[Revision: $]/", '',
49                             "\$Revision: 1.8 $");
50     }
51
52     function run($dbi, $argstr, &$request, $basepage) {
53         global $WikiTheme;
54         include_once("lib/BlockParser.php");
55         // RichTablePlugin markup is new.
56         $markup = 2.0; 
57
58         $lines = preg_split('/\n/', $argstr);
59         $table = HTML::table();
60  
61         if ($lines[0][0] == '*') {
62             $line = substr(array_shift($lines),1);
63             $attrs = $this->_parse_attr($line);
64             foreach ($attrs as $key => $value) {
65                 if (in_array ($key, array("id", "class", "title", "style",
66                                           "bgcolor", "frame", "rules", "border",
67                                           "cellspacing", "cellpadding",
68                                           "summary", "align", "width"))) {
69                     $table->setAttr($key, $value);
70                 }
71             }
72         }
73         
74         foreach ($lines as $line){
75             if (substr($line,0,1) == "-") {
76                 if (isset($row)) {
77                     if (isset($cell)) {
78                         if (isset($content)) {
79                             $cell->pushContent(TransformText($content, $markup, $basepage));
80                             unset($content);
81                         }
82                         $row->pushContent($cell);
83                         unset($cell);
84                     }
85                     $table->pushContent($row);
86                 }       
87                 $row = HTML::tr();
88                 $attrs = $this->_parse_attr(substr($line,1));
89                 foreach ($attrs as $key => $value) {
90                     if (in_array ($key, array("id", "class", "title", "style",
91                                               "bgcolor", "align", "valign"))) {
92                         $row->setAttr($key, $value);
93                     }
94                 }
95                 continue;
96             }
97             if (substr($line,0,1) == "|" and isset($row)) {
98                 if (isset($cell)) {
99                     if (isset ($content)) {
100                         $cell->pushContent(TransformText($content, $markup, $basepage));
101                         unset($content);
102                     }
103                     $row->pushContent($cell);
104                 }
105                 $cell = HTML::td();
106                 $line = substr($line, 1);
107                 if ($line[0] == "*" ) {
108                     $attrs = $this->_parse_attr(substr($line,1));
109                     foreach ($attrs as $key => $value) {
110                         if (in_array ($key, array("id", "class", "title", "style",
111                                                   "colspan", "rowspan", "width", "height",
112                                                   "bgcolor", "align", "valign"))) {
113                             $cell->setAttr($key, $value);
114                         }
115                     }
116                     continue;
117                 } 
118             } 
119             if (isset($row) and isset($cell)) {
120                 $line = str_replace("?\>", "?>", $line);
121                 $line = str_replace("\~", "~", $line);
122                 if (empty($content)) $content = '';
123                 $content .= $line . "\n";
124             }
125         }
126         if (isset($row)) {
127             if (isset($cell)) {
128                 if (isset($content))
129                     $cell->pushContent(TransformText($content));
130                 $row->pushContent($cell);
131             }
132             $table->pushContent($row);
133         }
134         return $table;
135     }
136
137     function _parse_attr($line) {
138         // We allow attributes with or without quotes (")
139         // border=1, cellpadding="5"
140         // style="font-family: sans-serif; border-top:1px solid #dddddd;
141         // What will not work is style with comma inside, e. g.
142         // style="font-family: Verdana, Arial, Helvetica, sans-serif"
143         $attr_chunks = preg_split("/\s*,\s*/", strtolower($line));
144         $options = array();
145         foreach ($attr_chunks as $attr_pair) {
146             if (empty($attr_pair)) continue;
147             $key_val = preg_split("/\s*=\s*/", $attr_pair);
148             if (!empty($key_val[1]))
149                 $options[trim($key_val[0])] = trim(str_replace("\"", "", $key_val[1]));
150         }
151         return $options;
152     }
153 }
154
155 // $Log: not supported by cvs2svn $
156 // Revision 1.7  2005/05/06 17:44:24  rurban
157 // silence undefined offset 1 msg
158 //
159 // Revision 1.6  2005/04/09 08:16:00  rurban
160 // fix RichTablePlugin embedded plugin invocation. Bug #1044245
161 //
162 // Revision 1.5  2004/06/14 11:31:39  rurban
163 // renamed global $Theme to $WikiTheme (gforge nameclash)
164 // inherit PageList default options from PageList
165 //   default sortby=pagename
166 // use options in PageList_Selectable (limit, sortby, ...)
167 // added action revert, with button at action=diff
168 // added option regex to WikiAdminSearchReplace
169 //
170 // Revision 1.4  2004/03/09 13:08:40  rurban
171 // fix undefined TransformText error: load BlockParser,
172 // get rid of warnings
173 //
174
175 // For emacs users
176 // Local Variables:
177 // mode: php
178 // tab-width: 8
179 // c-basic-offset: 4
180 // c-hanging-comment-ender-p: nil
181 // indent-tabs-mode: nil
182 // End:
183 ?>