3 Returns current page as man(1) document (nroff/troff). This script
4 includes a separate formatting kernel specifically for that case,
5 which of course strips out many things (CSS and markup extensions).
6 It is regex based and therefore not all too fast.
10 $ewiki_plugins["action"]["man"] = "ewiki_action_manpage";
11 $ewiki_config["action_links"]["view"]["man"] = "man(1)";
14 #-- return page in man(1) format to client
15 function ewiki_action_manpage($id, &$data, $action) {
17 // aaargh! shouldn't that be in text/ ?!
18 header("Content-Type: application/x-troff-man");
19 $fn = urlencode($id) . ".1";
20 header("Content-Disposition: inline; filename=\"$fn\"");
23 die(ewiki_format_nroff($data["content"]));
27 function ewiki_format_nroff(&$source)
29 global $ewiki_id, $ewiki_config, $mancat;
31 $mancat = "1"; // alternatively use "w" or "wiki"
33 $source = trim($source);
34 if (substr($source, 0, 2) == "#!") {
35 $source = substr($source, strpos($source, "\n") + 1);
38 #-- wiki2man line by line
39 foreach (explode("\n", $source) as $line) {
42 $line = preg_replace("/^[.]/", "\n .", $line);
45 $line = preg_replace("/^[!]+/", "\n.SH ", $line);
48 $line = preg_replace("/^:(.+?):/", "\n.TP 5\n.B $1\n", $line);
51 $line = preg_replace("/^( [ ]+)/e", '"\n.TP ".strlen("$1")."\n"', $line);
52 // $line = ltrim($line, " \t");
55 $line = preg_replace("/^\*/", "\n\n*", $line);
56 $line = preg_replace("/^#/", "\n\n#", $line);
59 $line = preg_replace("/__(.+?)__/", "\n.B $1\n", $line);
60 $line = preg_replace("/\*\*(.+?)\*\*/", "\n.B $1\n", $line);
61 $line = preg_replace("/'''(.+?)'''/", "\n.B $1\n", $line);
62 $line = preg_replace("/''(.+?)''/", "\n.I $1\n", $line);
63 $line = preg_replace("/'''''(.+?)'''''/", "\n.BI $1\n", $line);
65 #-- rip out some things
66 $line = preg_replace("/@@[^\s]+/", "", $line);
74 $o .= addslashes($line) . " ";
78 ewiki_scan_wikiwords($source, $GLOBALS["ewiki_man_links"]);
79 $o = preg_replace_callback($ewiki_config["wiki_link_regex"], "ewiki_format_man_linkrxcb", $o);
82 $o = preg_replace("/\n\s*\n+/", "\n\n", $o);
83 $o = preg_replace("/^ ([^\s])/m", "$1", $o);
86 $monthyear = strftime("%B %Y", time());
88 $o = ".\\\" this man page was converted by ewiki,\n"
89 . ".\\\" many apologies if it doesn't work\n"
91 . ".TH \"$ewiki_id\" $mancat \"$monthyear\" \"$name\" \"$name\"\n"
92 . (!preg_match("/^\s*\.SH/", $o) ? "\n.SH $ewiki_id\n" : "")
100 function ewiki_format_man_linkrxcb($uu) {
101 global $ewiki_man_links, $mancat;
104 if (($str[0]=="!") or ($str[0] == "~") or ($str[0] == "\\")) {
105 return substr($str, 1);
108 $str = substr($str, 1, strlen($str)-2);
111 if (strpos($str, '"')
112 and (preg_match('/^\s*"(1:.+?)"\s*(2:.+?)\s*$/', $str, $uu)
113 or preg_match('/^\s*(2:.+?)\s*"(1:.+?)"\s*$/', $str, $uu))) {
114 list($uu2, $title, $href) = $uu;
116 elseif (strpos($str, '"')) {
117 list($title, $href) = explode("|", $str, 2);
121 if (strpos($href, "://") && strpos($href, " ")) {
122 $href = strtok($str, " ");
123 $title = strtok("]");
125 elseif (!$ewiki_man_links[$href]) {
131 if (strpos($title, "://")) {
134 elseif (strpos($title, ":")) {
135 $href = ewiki_interwiki($href, $uu, $uu);
139 return "\n.BI \"" . addslashes($title) . "\""
140 . ($href ? ($href=="?" ? "?" : " \"(" . addslashes($href) . ")\"") : "")