]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/OldStyleTable.php
Replace tabs by spaces; remove EOL spaces
[SourceForge/phpwiki.git] / lib / plugin / OldStyleTable.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /**
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with PhpWiki; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * OldStyleTable: Layout tables using the old table style.
26  *
27  * Usage:
28  * <pre>
29  *  <?plugin OldStyleTable border||=0 summary=""
30  *  ||  __Name__               |v __Cost__   |v __Notes__
31  *  | __First__   | __Last__
32  *  |> Jeff       |< Dairiki   |^  Cheap     |< Not worth it
33  *  |> Marco      |< Polo      | Cheaper     |< Not available
34  *  ?>
35  * </pre>
36  *
37  * Note that multiple <code>|</code>'s lead to spanned columns,
38  * and <code>v</code>'s can be used to span rows.  A <code>&gt;</code>
39  * generates a right justified column, <code>&lt;</code> a left
40  * justified column and <code>^</code> a centered column
41  * (which is the default.)
42  *
43  * @author Geoffrey T. Dairiki
44  */
45
46 class WikiPlugin_OldStyleTable
47 extends WikiPlugin
48 {
49     function getName() {
50         return _("OldStyleTable");
51     }
52
53     function getDescription() {
54       return _("Layout tables using the old markup style.");
55     }
56
57     function getVersion() {
58         return preg_replace("/[Revision: $]/", '',
59                             "\$Revision$");
60     }
61
62     function getDefaultArguments() {
63         return array(
64                      'caption'     => '',
65                      'cellpadding' => '1',
66                      'cellspacing' => '1',
67                      'border'      => '1',
68                      'summary'     => '',
69                      );
70     }
71
72     function handle_plugin_args_cruft($argstr, $args) {
73         return;
74     }
75
76     function run($dbi, $argstr, &$request, $basepage) {
77         global $WikiTheme;
78         include_once('lib/InlineParser.php');
79
80         $args = $this->getArgs($argstr, $request);
81         $default = $this->getDefaultArguments();
82         foreach (array('cellpadding','cellspacing','border') as $arg) {
83             if (!is_numeric($args[$arg])) {
84                 $args[$arg] = $default[$arg];
85             }
86         }
87         $lines = preg_split('/\s*?\n\s*/', $argstr);
88         $table_args = array();
89         $default_args = array_keys($default);
90         foreach ($default_args as $arg) {
91             if ($args[$arg] == '' and $default[$arg] == '')
92                 continue;                        // ignore '' arguments
93             if ($arg == 'caption')
94                 $caption = $args[$arg];
95             else
96                 $table_args[$arg] = $args[$arg];
97         }
98         $table = HTML::table($table_args);
99         if (!empty($caption))
100             $table->pushContent(HTML::caption($caption));
101         if (preg_match("/^\s*(cellpadding|cellspacing|border|caption|summary)/", $lines[0]))
102             $lines[0] = '';
103         foreach ($lines as $line) {
104             if (!$line)
105                 continue;
106             if (strstr($line,"=")) {
107                     $tmp = explode("=",$line);
108                     if (in_array(trim($tmp[0]),$default_args))
109                     continue;
110             }
111             if ($line[0] != '|') {
112                     // bogus error if argument
113                 trigger_error(sprintf(_("Line %s does not begin with a '|'."), $line), E_USER_WARNING);
114             } else {
115                 $table->pushContent($this->_parse_row($line, $basepage));
116             }
117         }
118
119         return $table;
120     }
121
122     function _parse_row ($line, $basepage) {
123         $brkt_link = "\\[ .*? [^]\s] .*? \\]";
124         $cell_content  = "(?: [^[] | ".ESCAPE_CHAR."\\[ | $brkt_link )*?";
125
126         preg_match_all("/(\\|+) (v*) ([<>^]?) \s* ($cell_content) \s* (?=\\||\$)/x",
127                        $line, $matches, PREG_SET_ORDER);
128
129         $row = HTML::tr();
130
131         foreach ($matches as $m) {
132             $attr = array();
133
134             if (strlen($m[1]) > 1)
135                 $attr['colspan'] = strlen($m[1]);
136             if (strlen($m[2]) > 0)
137                 $attr['rowspan'] = strlen($m[2]) + 1;
138
139             if ($m[3] == '^')
140                 $attr['align'] = 'center';
141             else if ($m[3] == '>')
142                 $attr['align'] = 'right';
143             else
144                 $attr['align'] = 'left';
145
146             // Assume new-style inline markup.
147             $content = TransformInline($m[4], 2.0, $basepage);
148
149             $row->pushContent(HTML::td($attr, HTML::raw('&nbsp;'),
150                                        $content, HTML::raw('&nbsp;')));
151         }
152         return $row;
153     }
154 };
155
156 // (c-file-style: "gnu")
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 ?>