]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/OldStyleTable.php
renamed global $Theme to $WikiTheme (gforge nameclash)
[SourceForge/phpwiki.git] / lib / plugin / OldStyleTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id: OldStyleTable.php,v 1.10 2004-06-14 11:31:39 rurban Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * OldStyleTable: Layout tables using the old table style.
25  *
26  * Usage:
27  * <pre>
28  *  <?plugin OldStyleTable border||=0 summary=""
29  *  ||  __Name__               |v __Cost__   |v __Notes__
30  *  | __First__   | __Last__
31  *  |> Jeff       |< Dairiki   |^  Cheap     |< Not worth it
32  *  |> Marco      |< Polo      | Cheaper     |< Not available
33  *  ?>
34  * </pre>
35  *
36  * Note that multiple <code>|</code>'s lead to spanned columns,
37  * and <code>v</code>'s can be used to span rows.  A <code>&gt;</code>
38  * generates a right justified column, <code>&lt;</code> a left
39  * justified column and <code>^</code> a centered column
40  * (which is the default.)
41  *
42  * @author Geoffrey T. Dairiki
43  */
44
45 class WikiPlugin_OldStyleTable
46 extends WikiPlugin
47 {
48     function getName() {
49         return _("OldStyleTable");
50     }
51
52     function getDescription() {
53       return _("Layout tables using the old markup style.");
54     }
55
56     function getVersion() {
57         return preg_replace("/[Revision: $]/", '',
58                             "\$Revision: 1.10 $");
59     }
60
61     function getDefaultArguments() {
62         return array(
63                      'caption'     => '',
64                      'cellpadding' => '1',
65                      'cellspacing' => '1',
66                      'border'      => '1',
67                      'summary'     => '',
68                      );
69     }
70
71     function handle_plugin_args_cruft($argstr, $args) {
72         return;
73     }
74
75     function run($dbi, $argstr, &$request, $basepage) {
76         global $WikiTheme;
77         include_once('lib/InlineParser.php');
78
79         $args = $this->getArgs($argstr, $request);
80         $default = $this->getDefaultArguments();
81         foreach (array('cellpadding','cellspacing','border') as $arg) {
82             if (!is_numeric($args[$arg])) {
83                 $args[$arg] = $default[$arg];
84             }
85         }
86         $lines = preg_split('/\s*?\n\s*/', $argstr);
87         $table_args = array();
88         $default_args = array_keys($default);
89         foreach ($default_args as $arg) { 
90             if ($args[$arg] == '' and $default[$arg] == '')  
91                 continue;                       // ignore '' arguments
92             if ($arg == 'caption')
93                 $caption = $args[$arg];
94             else
95                 $table_args[$arg] = $args[$arg];
96         }
97         $table = HTML::table($table_args);
98         if (!empty($caption))
99             $table->pushContent(HTML::caption(array('valign'=>'top'),$caption));
100         foreach ($lines as $line) {
101             if (!$line)
102                 continue;
103             if (strstr($line,"=")) {
104                 $tmp = explode("=",$line);
105                 if (in_array(trim($tmp[0]),$default_args))
106                     continue;
107             }
108             if ($line[0] != '|')
109                 return $this->error(fmt("Line does not begin with a '|'."));
110             $table->pushContent($this->_parse_row($line, $basepage));
111         }
112
113         return $table;
114     }
115
116     function _parse_row ($line, $basepage) {
117         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
118         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
119         
120         preg_match_all("/(\\|+) (v*) ([<>^]?) \s* ($cell_content) \s* (?=\\||\$)/x",
121                        $line, $matches, PREG_SET_ORDER);
122
123         $row = HTML::tr();
124
125         foreach ($matches as $m) {
126             $attr = array();
127
128             if (strlen($m[1]) > 1)
129                 $attr['colspan'] = strlen($m[1]);
130             if (strlen($m[2]) > 0)
131                 $attr['rowspan'] = strlen($m[2]) + 1;
132
133             if ($m[3] == '^')
134                 $attr['align'] = 'center';
135             else if ($m[3] == '>')
136                 $attr['align'] = 'right';
137             else
138                 $attr['align'] = 'left';
139
140             // Assume new-style inline markup.
141             $content = TransformInline($m[4], 2.0, $basepage);
142
143             $row->pushContent(HTML::td($attr, HTML::raw('&nbsp;'),
144                                        $content, HTML::raw('&nbsp;')));
145         }
146         return $row;
147     }
148 };
149
150 // $Log: not supported by cvs2svn $
151 // Revision 1.9  2004/02/17 12:11:36  rurban
152 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
153 //
154 // Revision 1.8  2004/01/24 23:37:08  rurban
155 // Support more options: caption (seperate tag), border, summary, cellpadding,
156 // cellspacing
157 // Fixes some errors from the version from the mailinglist.
158 //
159 // Revision 1.7  2003/02/21 23:00:35  dairiki
160 // Fix SF bug #676309.
161 //
162 // Also fix new bugs introduced with cached markup changes.
163 //
164 // Revision 1.6  2003/02/21 04:12:06  dairiki
165 // Minor fixes for new cached markup.
166 //
167 // Revision 1.5  2003/01/18 21:48:59  carstenklapp
168 // Code cleanup:
169 // Reformatting & tabs to spaces;
170 // Added copyleft, getVersion, getDescription, rcs_id.
171 //
172
173 // (c-file-style: "gnu")
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End:
181 ?>