77c03a313eabbdbcce0e5d861ce29f675766cc7d
[atutor.git] / mods / wiki / plugins / action / manpage.php
1 <?php
2 /*
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.
7 */
8
9
10 $ewiki_plugins["action"]["man"] = "ewiki_action_manpage";
11 $ewiki_config["action_links"]["view"]["man"] = "man(1)";
12
13
14 #-- return page in man(1) format to client
15 function ewiki_action_manpage($id, &$data, $action) {
16
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\"");
21    
22    #-- output
23    die(ewiki_format_nroff($data["content"]));
24 }
25
26
27 function ewiki_format_nroff(&$source)
28 {
29    global $ewiki_id, $ewiki_config, $mancat;
30
31    $mancat = "1";   // alternatively use "w" or "wiki"
32    $o = "";
33    $source = trim($source);
34    if (substr($source, 0, 2) == "#!") {
35       $source = substr($source, strpos($source, "\n") + 1);
36    }
37    
38    #-- wiki2man line by line
39    foreach (explode("\n", $source) as $line) {
40
41       #-- escaping
42       $line = preg_replace("/^[.]/", "\n .", $line);
43       
44       #-- headlines
45       $line = preg_replace("/^[!]+/", "\n.SH ", $line);
46
47       #-- definiton lists
48       $line = preg_replace("/^:(.+?):/", "\n.TP 5\n.B $1\n", $line);
49
50       #-- indented text
51       $line = preg_replace("/^( [ ]+)/e", '"\n.TP ".strlen("$1")."\n"', $line);
52 //      $line = ltrim($line, " \t");
53
54       #-- ordinary lists
55       $line = preg_replace("/^\*/", "\n\n*", $line);
56       $line = preg_replace("/^#/", "\n\n#", $line);
57
58       #-- text style
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);
64
65       #-- rip out some things
66       $line = preg_replace("/@@[^\s]+/", "", $line);
67       
68       #-- paragraphs
69       if (!strlen($line)) {
70          $o .= "\n\n";
71       } 
72
73       #-- ok, out
74       $o .= addslashes($line) . " ";
75    }
76    
77    #-- highlight links
78    ewiki_scan_wikiwords($source, $GLOBALS["ewiki_man_links"]);
79    $o = preg_replace_callback($ewiki_config["wiki_link_regex"], "ewiki_format_man_linkrxcb", $o);
80    
81    #-- post fixes
82    $o = preg_replace("/\n\s*\n+/", "\n\n", $o);
83    $o = preg_replace("/^ ([^\s])/m", "$1", $o);
84
85    #-- prefix output   
86    $monthyear = strftime("%B %Y", time());
87    $name = EWIKI_NAME;
88    $o = ".\\\" this man page was converted by ewiki,\n"
89       . ".\\\" many apologies if it doesn't work\n"
90       . ".\\\"\n"
91       . ".TH \"$ewiki_id\" $mancat \"$monthyear\" \"$name\" \"$name\"\n"
92       . (!preg_match("/^\s*\.SH/", $o) ? "\n.SH $ewiki_id\n" : "")
93       . "\n"
94       . $o;
95    
96    return($o);
97 }
98
99
100 function ewiki_format_man_linkrxcb($uu) {
101    global $ewiki_man_links, $mancat;
102
103    $str = $uu[0];
104    if (($str[0]=="!") or ($str[0] == "~") or ($str[0] == "\\")) {
105       return substr($str, 1);
106    }
107    if ($str[0]=="[") {
108       $str = substr($str, 1, strlen($str)-2);
109    }
110
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;
115    }
116    elseif (strpos($str, '"')) {
117       list($title, $href) = explode("|", $str, 2);
118    }
119    else {
120       $href=$title=$str;
121       if (strpos($href, "://") && strpos($href, " ")) {
122          $href = strtok($str, " ");
123          $title = strtok("]");
124       }
125       elseif (!$ewiki_man_links[$href]) {
126          $href = "?";
127       }
128       else {
129          $href = "$mancat";
130       }
131       if (strpos($title, "://")) {
132          $href = 0;
133       }
134       elseif (strpos($title, ":")) {
135          $href = ewiki_interwiki($href, $uu, $uu);
136       }
137    }
138    
139    return "\n.BI \"" . addslashes($title) . "\""
140        . ($href ? ($href=="?" ? "?" : " \"(" . addslashes($href) . ")\"") : "")
141        . "\n";
142 }
143
144 ?>