changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / mpi / mpi_sparsetable.php
1 <?php
2
3 /*
4    SparseTable allows you to build up a table in a data oriented
5    fashion:
6
7     <?plugin SparseTable columns="id, 2,p, a, b, c,d, e"
8
9        id=identifier
10          2=second
11          p=points
12          a=notes
13          b=another column
14
15        id=row2
16          b=more text
17          d=here is an entry
18
19        id=row3
20          p=...
21          b=...
22     ?>
23
24    You could also assign a list of rows="..." instead of columns= names.
25    In both cases you separate the individual row/column entries with an
26    empty line, and use one of the specified identifiers and a "=" sign or
27    ":" colon to fill the table cells with data.
28    The row/column identifiers itself won't be printed and should therefore
29    be choosen like variables. The first block of assignments then was
30    typically used to make appropriate headings.
31
32    (This plugin resembles a feature as seen in ProWiki, or at least
33    tries to. You could get the ProWiki CDML syntax with an mpi framework
34    extension plugin.)
35 */
36
37
38 $ewiki_plugins["mpi"]["sparsetable"] = "ewiki_mpi_sparsetable";
39 function ewiki_mpi_sparsetable($action, $args, &$iii, &$s) {
40
41    $SEP = "|";
42    $o = "";
43    $i = array();
44    $t = array();
45
46    #-- column/row names
47    $use_rows = isset($args["rows"]);
48    if ($use_rows) {
49       $i = $args["rows"];
50    }
51    else {
52       $i = $args["columns"];
53    }
54    $ind = preg_split("/\s*[:;,|]\s*/", strtolower(trim($i)));
55    if (!$ind) {
56       return("Note: use rows= or columns= for SparseTable");
57    }
58    $ind = array_flip($ind);
59    $empty_line = array();
60    for ($n=0; $n<count($ind); $n++) {
61       $empty_line[$n] = "";
62    }
63
64    #-- input chunks
65    $data = substr($args["_"], strpos($args["_"], "\n"));
66    $data = preg_split("/\n\s*\n/", $data);
67    unset($args);
68
69    #-- walk through rows/cols
70    foreach ($data as $block) {
71       if (!trim($block)) {
72          continue;
73       }
74       $d = preg_split('/^\s*(\w+)\s*[:=]+/m', $block, -1, PREG_SPLIT_DELIM_CAPTURE);
75
76       $l = $empty_line;
77       for ($i=1; $i<count($d); $i++) {
78          $rname = strtolower(strtok(trim($d[$i]), " :=\t"));
79          $val = trim($d[++$i]);
80          $l[ $ind[$rname] ] = $val;
81       }
82       $t[] = $l;
83    }
84
85    #-- generate table
86    foreach ($t as $line) {
87       $o .= "$SEP " . implode(" $SEP ", $line) . " $SEP\n";
88    }
89
90    #-- put output back into $iii
91    $iii[$s["in"]] = array(
92       $o,
93       0x007F,
94       "core",
95    );
96    return($o);
97 }
98
99
100 ?>