]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/OldStyleTable.php
Activated Revision substitution for Subversion
[SourceForge/phpwiki.git] / lib / plugin / OldStyleTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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$");
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         if (preg_match("/^\s*(cellpadding|cellspacing|border|caption|summary)/", $lines[0])) 
101             $lines[0] = '';
102         foreach ($lines as $line) {
103             if (!$line)
104                 continue;
105             if (strstr($line,"=")) {
106                 $tmp = explode("=",$line);
107                 if (in_array(trim($tmp[0]),$default_args))
108                     continue;
109             }
110             if ($line[0] != '|') {
111                 // bogus error if argument
112                 trigger_error(sprintf(_("Line %s does not begin with a '|'."), $line), E_USER_WARNING);
113             } else {
114                 $table->pushContent($this->_parse_row($line, $basepage));
115             }
116         }
117
118         return $table;
119     }
120
121     function _parse_row ($line, $basepage) {
122         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
123         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
124         
125         preg_match_all("/(\\|+) (v*) ([<>^]?) \s* ($cell_content) \s* (?=\\||\$)/x",
126                        $line, $matches, PREG_SET_ORDER);
127
128         $row = HTML::tr();
129
130         foreach ($matches as $m) {
131             $attr = array();
132
133             if (strlen($m[1]) > 1)
134                 $attr['colspan'] = strlen($m[1]);
135             if (strlen($m[2]) > 0)
136                 $attr['rowspan'] = strlen($m[2]) + 1;
137
138             if ($m[3] == '^')
139                 $attr['align'] = 'center';
140             else if ($m[3] == '>')
141                 $attr['align'] = 'right';
142             else
143                 $attr['align'] = 'left';
144
145             // Assume new-style inline markup.
146             $content = TransformInline($m[4], 2.0, $basepage);
147
148             $row->pushContent(HTML::td($attr, HTML::raw('&nbsp;'),
149                                        $content, HTML::raw('&nbsp;')));
150         }
151         return $row;
152     }
153 };
154
155 // $Log: not supported by cvs2svn $
156 // Revision 1.10  2004/06/14 11:31:39  rurban
157 // renamed global $Theme to $WikiTheme (gforge nameclash)
158 // inherit PageList default options from PageList
159 //   default sortby=pagename
160 // use options in PageList_Selectable (limit, sortby, ...)
161 // added action revert, with button at action=diff
162 // added option regex to WikiAdminSearchReplace
163 //
164 // Revision 1.9  2004/02/17 12:11:36  rurban
165 // 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, ...)
166 //
167 // Revision 1.8  2004/01/24 23:37:08  rurban
168 // Support more options: caption (seperate tag), border, summary, cellpadding,
169 // cellspacing
170 // Fixes some errors from the version from the mailinglist.
171 //
172 // Revision 1.7  2003/02/21 23:00:35  dairiki
173 // Fix SF bug #676309.
174 //
175 // Also fix new bugs introduced with cached markup changes.
176 //
177 // Revision 1.6  2003/02/21 04:12:06  dairiki
178 // Minor fixes for new cached markup.
179 //
180 // Revision 1.5  2003/01/18 21:48:59  carstenklapp
181 // Code cleanup:
182 // Reformatting & tabs to spaces;
183 // Added copyleft, getVersion, getDescription, rcs_id.
184 //
185
186 // (c-file-style: "gnu")
187 // Local Variables:
188 // mode: php
189 // tab-width: 8
190 // c-basic-offset: 4
191 // c-hanging-comment-ender-p: nil
192 // indent-tabs-mode: nil
193 // End:
194 ?>