]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/OldStyleTable.php
Merge OldTextFormattingRules into TextFormattingRules; rename underscore plugins
[SourceForge/phpwiki.git] / lib / plugin / OldStyleTable.php
1 <?php
2
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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * OldStyleTable: Layout tables using the old table style.
26  *
27  * Usage:
28  * <pre>
29  *  <<OldStyleTable border||=0
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 getDescription()
50     {
51         return _("Layout tables using the old markup style.");
52     }
53
54     function getDefaultArguments()
55     {
56         return array(
57             'caption' => '',
58             'cellpadding' => '1',
59             'cellspacing' => '1',
60             'border' => '1',
61             'summary' => '',
62         );
63     }
64
65     function handle_plugin_args_cruft($argstr, $args)
66     {
67         return;
68     }
69
70     function run($dbi, $argstr, &$request, $basepage)
71     {
72         include_once 'lib/InlineParser.php';
73
74         $args = $this->getArgs($argstr, $request);
75         $default = $this->getDefaultArguments();
76         foreach (array('cellpadding', 'cellspacing', 'border') as $arg) {
77             if (!is_numeric($args[$arg])) {
78                 $args[$arg] = $default[$arg];
79             }
80         }
81         $lines = preg_split('/\s*?\n\s*/', $argstr);
82         $table_args = array();
83         $default_args = array_keys($default);
84         foreach ($default_args as $arg) {
85             if ($args[$arg] == '' and $default[$arg] == '')
86                 continue; // ignore '' arguments
87             if ($arg == 'caption')
88                 $caption = $args[$arg];
89             else
90                 $table_args[$arg] = $args[$arg];
91         }
92         $table = HTML::table($table_args);
93         if (!empty($caption))
94             $table->pushContent(HTML::caption($caption));
95         if (preg_match("/^\s*(cellpadding|cellspacing|border|caption|summary)/", $lines[0]))
96             $lines[0] = '';
97         foreach ($lines as $line) {
98             if (!$line)
99                 continue;
100             if (strstr($line, "=")) {
101                 $tmp = explode("=", $line);
102                 if (in_array(trim($tmp[0]), $default_args))
103                     continue;
104             }
105             if ($line[0] != '|') {
106                 // bogus error if argument
107                 trigger_error(sprintf(_("Line %s does not begin with a '|'."), $line), E_USER_WARNING);
108             } else {
109                 $table->pushContent($this->parse_row($line, $basepage));
110             }
111         }
112
113         return $table;
114     }
115
116     private function parse_row($line, $basepage)
117     {
118         $bracket_link = "\\[ .*? [^]\s] .*? \\]";
119         $cell_content = "(?: [^[] | " . ESCAPE_CHAR . "\\[ | $bracket_link )*?";
120
121         preg_match_all("/(\\|+) (v*) ([<>^]?) \s* ($cell_content) \s* (?=\\||\$)/x",
122             $line, $matches, PREG_SET_ORDER);
123
124         $row = HTML::tr();
125
126         foreach ($matches as $m) {
127             $attr = array();
128
129             if (strlen($m[1]) > 1)
130                 $attr['colspan'] = strlen($m[1]);
131             if (strlen($m[2]) > 0)
132                 $attr['rowspan'] = strlen($m[2]) + 1;
133
134             if ($m[3] == '^')
135                 $attr['align'] = 'center';
136             else if ($m[3] == '>')
137                 $attr['align'] = 'right';
138             else
139                 $attr['align'] = 'left';
140
141             $content = TransformInline($m[4], $basepage);
142
143             $row->pushContent(HTML::td($attr, HTML::raw('&nbsp;'),
144                 $content, HTML::raw('&nbsp;')));
145         }
146         return $row;
147     }
148 }
149
150 // Local Variables:
151 // mode: php
152 // tab-width: 8
153 // c-basic-offset: 4
154 // c-hanging-comment-ender-p: nil
155 // indent-tabs-mode: nil
156 // End: