final move of files
[web.mtrack] / MTrack / Wiki.php
1 <?php
2
3 require_once 'Wiki/Parser.php';
4 require_once 'Wiki/HTMLFormatter.php';
5 require_once 'Wiki/OneLinerFormatter.php';
6 require_once 'Wiki/Item.php';
7
8
9
10
11
12 class MTrack_Wiki {
13   static $macros = array();
14   static $processors = array();
15
16   static function format_to_html($text) {
17     $f = new MTrack_Wiki_HTMLFormatter;
18     $f->format($text);
19     $html = $f->out;
20     if (false) { /* saveHTML messes with the encoding */
21       /* tidy it up */
22       @$d = DOMDocument::loadHTML($html);
23       if ($d) {
24         $d->formatOutput = true;
25         $d->substituteEntities = false;
26         $html = $d->saveHTML();
27         $html = preg_replace("/^.*<body>/sm", '', $html);
28         $html = preg_replace(",</body>.*,sm", '', $html);
29       }
30     }
31     return $html;
32   }
33
34   static function format_to_oneliner($text) {
35     $f = new MTrack_Wiki_OneLinerFormatter;
36     $f->format($text);
37     return $f->out;
38   }
39   static function format_wiki_page($name) {
40     $d = MTrack_Wiki_Item::loadByPageName($name);
41     if ($d) {
42       return self::format_to_html($d->content);
43     }
44     return null;
45   }
46
47   static function register_macro($name, $callback) {
48     self::$macros[$name] = $callback;
49   }
50
51   static function register_processor($name, $callback) {
52     self::$processors[$name] = $callback;
53   }
54
55   static function macro_IncludeWiki($pagename) {
56     return self::format_wiki_page($pagename);
57   }
58   static function macro_IncludeHelp($pagename) {
59     return self::format_to_html(file_get_contents(
60       dirname(__FILE__) . '/../defaults/help/' . basename($pagename)));
61   }
62   static function macro_comment() {
63     return '';
64   }
65   static function processor_comment($name, $content) {
66     return '';
67   }
68   static function processor_html($name, $content) {
69     return join("\n", $content);
70   }
71   static function processor_dataset($name, $content) {
72     $res = '<table class="report wiki dataset">';
73     while (count($content)) {
74       $row = array_shift($content);
75       $next_row = array_shift($content);
76       $cols = preg_split("/\s*\|\s*/", $row);
77       if ($next_row[0] == '-') {
78         // it's a header
79         $res .= '<thead><tr>';
80         foreach ($cols as $c) {
81           $res .= "<th>" . htmlentities($c, ENT_QUOTES, 'utf-8') . "</th>\n";
82         }
83         $res .= "</tr></thead><tbody>";
84       } else {
85         if (is_string($next_row)) {
86           array_unshift($content, $next_row);
87         }
88         // regular row
89         $res .= "<tr>";
90         foreach ($cols as $c) {
91           $res .= "<td>" . htmlentities($c, ENT_QUOTES, 'utf-8') . "</td>\n";
92         }
93         $res .= "</tr>\n";
94       }
95     }
96     $res .= "</tbody></table>\n";
97     return $res;
98   }
99 }