299677a623997d929e0168dff8b79aa6d5a71432
[atutor.git] / mods / wiki / plugins / markup / htmltable.php
1 <?php
2
3 /*
4    You can use the ordinary html <table>, <tr> and <td> tags in all wiki
5    pages, if you activate this plugin. Standard attributes are allowed
6    (bgcolor, width, class, style, align, ...). It provides only limited 
7    tag correction support, but you can often leave out </tr> and </td>:
8
9    <table width="100%">
10      <tr> <td> cell1
11           <td> cell2
12      <tr> <td> row2
13           <td> cell4
14    </table>
15 */
16
17
18 $ewiki_plugins["format_block"]["htmltable"][] = "ewiki_markup_fblock_htmltable";
19 $ewiki_config["format_block"]["htmltable"] = array("&lt;table", "&lt;/table&gt;", false, 0x0027);
20
21
22 function ewiki_markup_fblock_htmltable(&$c, &$in, &$ooo, &$s) {
23
24    if (($p = strpos($c, "&gt;")) !== false) {
25
26
27       // clean <table> start and </table> end tag
28       $c = "<table " . ewiki_markup_htmltable_attrs(substr($c, 0, $p))
29          . ">" . substr($c, $p + 4) . "</table>";
30       
31       // clean <td> and <tr> tags
32       $c = preg_replace('#&lt;(/?td|/?tr)(.*?)&gt;#e',
33             '"<\\1" . ewiki_markup_htmltable_attrs("\\2") . ">"', $c);
34
35       // insert missing </tr> and </td>
36       $c = preg_replace('#(?<!</t[dr]>)\s*<(t[dr])#', '</\\1><\\1', $c);
37
38       // add last closing </td> and </tr> tags
39       $c = preg_replace('#(?<!</tr>)(\s*</table>)#', '</tr>\\1', $c);
40       $c = preg_replace('#(?<!</td>)(\s*</tr>\s*</table>)#', '</td>\\1', $c);
41
42       // remove redundant closing tags
43       $c = preg_replace('#(<table[^>]+?>\s*)\s*(</t[dr]>)#', '\\1', $c);
44       $c = preg_replace('#(</?tr>\s*)(\s*</t[dr]>)+#', '\\1', $c);
45       $c = preg_replace('#(</td>\s*)(\s*</td>)*#', '\\1', $c);
46    }
47 }
48
49
50 function ewiki_markup_htmltable_attrs($str) {
51    if (preg_match_all('/(\s+(class|style|width|height|align|bgcolor|valign|border|colspan|rowspan|cellspacing|cellpadding)=(\w+|"[^"]+"))/', $str, $uu)) {
52       $str = implode("", $uu[1]);
53    }
54    return($str);
55 }
56
57
58 ?>