ca93ba4e88ba239a221b09075596f287b3cbb06f
[atutor.git] / mods / wiki / plugins / markup / asciitbl.php
1 <?php
2
3 /*
4    ASCII-Art tables can be used in Wiki pages, if you load this plugin (it
5    internally converts them into standard tables).
6    Such tables usually look like:
7
8    +----------+--------+----+
9    | dl       | 0x0A   | 1  |
10    +----------+--------+----+
11    | ibbo,    | 0x12   | 2  |
12    | nna      |        |    |
13    +----------+--------+----+
14    | nf,      | 0xFF   | 3  |
15    +----------+--------+----+
16
17    It's essentially a list, which rows are separated by horizontal bars, so
18    one can have multiple lines making up one cell. If you don't import such 
19    tables from an app (mysql outputs such tables), you could shorten writing
20    them into:
21
22    --------
23    | cell1   | cell2 |
24    ------
25    | row2, col1 |  col2/cell4  |
26    | still row2 |  ...  |
27    +-----
28    | row 3   | ... |
29    -------
30
31    Instead of only using minus signs, you could have some plus signs in it
32    (or even a complete line of them).
33 */
34
35
36
37 $ewiki_plugins["format_source"][] = "ewiki_formatsrc_ascii_art_tables";
38
39
40
41 function ewiki_formatsrc_ascii_art_tables(&$src) {
42    $src = preg_replace('/^([+-]{5,}\n\|[^\n]+\n((\|[^\n]+|[+-]+)\n)+)/mse', 'ewiki_formatsrc_asciitbl_cells(stripslashes("\\1"))', $src);
43 }
44
45
46 function ewiki_formatsrc_asciitbl_cells($str) {
47    $rows = preg_split('/^[+-]+\n/m', $str);
48    $str = "";
49    foreach ($rows as $row) {
50       if (empty($row)) {
51          continue;
52       }
53       $cells = array();
54       $lines = explode("\n", $row);
55       foreach ($lines as $l=>$line) {
56          $add = explode("|", trim($line, "|"));
57          if (empty($cells)) {
58             $cells = $add;
59          }
60          else {
61             foreach ($add as $i=>$text) {
62                if (!trim($text) && ($l+1<count($lines))) { 
63                   $text = "<br /><br />";
64                }
65                $cells[$i] .= " $text";
66             }
67          }
68       }
69       $str .= "|" . implode("|", $cells) . "|\n";
70    }
71    $str = preg_replace('/(<br />\s*)+\|/', "|", $str);
72    return($str);
73 }
74
75
76 ?>