2005-03-23 Martin Baulig <martin@ximian.com>
[mono.git] / mono / docscripts / exdoc
1 #!/usr/bin/perl
2
3 if ($ARGV[0] eq "-h"){
4     $dir = $ARGV[1];
5     $html = 1;
6     shift @ARGV;
7     shift @ARGV;
8 }
9
10 if ($html){
11     opendir (D, "$dir/sources/") || die "Can not open $dir";
12     while ($n = readdir (D)){
13         if ($n =~ /mono-api-.*\.html$/){
14             open (IN, "$dir/sources/$n") || die "Can not open $n";
15             $files[$filecount] = $n;
16             while (<IN>){
17                 @files_content[$filecount] .= $_;
18             }
19             $filecount++;
20             close IN;
21         }
22     }
23 }
24
25 while (<ARGV>){
26         if (/\/\*\* *\n/){
27                 &process_doc;
28         } else {
29                 #print "IGNORING: $_";
30         }
31 }
32
33 if ($html){
34     for ($f = 0; $f < $filecount; $f++){
35         $name = $files[$f];
36         open (OUT, "> $dir/html/$name") || die "Can not create $dir/html/$name";
37         print "Merging: $name\n";
38         print OUT<<EOF;
39 <?xml version="1.0" encoding="utf-8"?>
40 <html xmlns="http://www.w3.org/1999/xhtml">
41 <head>
42    <title>$name</title>
43 </head>
44 <body>
45 EOF
46         @a = split (/\n/, $files_content[$f]);
47
48         for ($ai = 0; $ai < $#a; $ai++){
49             $line = $a[$ai];
50
51             ($api,$caption) = $line =~  /<h4><a name=\"api:(\w+)\">(\w+)<\/a><\/h4>/;
52             if ($api ne ""){
53                 $proto = $prototype{$api};
54                 if ($proto eq ""){
55                     $proto = "Prototype: $api";
56                 }
57
58 print OUT<<EOF;
59 <blockquote>
60     <a name="api:$api"></a>
61                 <table summary="" class="HeaderTable" width="100%" cellpadding="5">
62                         <tr bgcolor="#b0c4de"><td>
63                         <h3 class="api">$api</h3>
64                         </td></tr>
65                 </table>
66
67     <blockquote>
68         <table summary="" class="SignatureTable" bgcolor="#c0c0c0" cellspacing="0" width="100%">
69                 <tr><td>
70                         <table summary="" class="InnerSignatureTalbe" cellpadding="10" cellspacing="0" width="100%">
71                         <tr bgcolor="#f2f2f2"><td>
72                         <h4>$proto</h4>
73                         </td></tr>
74                         </table>
75                 </td></tr>
76         </table>
77     </blockquote>
78 <p>
79 EOF
80                     if ($arguments{$api} ne ""){
81                         print OUT "<h4>Parameters</h4>";
82                         print OUT "<blockquote><dl>";
83
84                         print OUT "$arguments{$api}\n";
85                         print OUT "</dl></blockquote>";
86                     }
87                     if ($returns{$api} ne ""){
88                         print OUT "<h4>Returns</h4>";
89                         print OUT "<blockquote>";
90                         print OUT "$returns{$api}\n";
91                         print OUT "</blockquote>";
92                     }
93                     if ($bodies{$api} ne ""){
94                         print OUT "<h4>Remarks</h4>";
95                         print OUT "<blockquote>";
96                         print OUT "$bodies{$api}\n";
97                         print OUT "</blockquote>";
98                     }
99                     print OUT "\n";
100                     print OUT "</blockquote>";
101             } else {
102                 print OUT "$line\n";
103             }
104         }
105         print OUT<<EOF;
106 </body>
107 </html>
108 EOF
109         close OUT;
110         system ("mono convert.exe $dir/html/$name $dir/deploy/$name");
111     }
112 }
113
114 sub process_doc {
115         $doc = "";
116         $func = <>;
117         chop $func;
118         $func =~ s/^ \* //;
119         $func =~ s/:$//;
120         print "Function: $func\n" if (!$html);
121         $args = "";
122         $inbody = 0;
123         $returns = "";
124         $body = "";
125         $functions[$fn++] = $func;
126
127         # Process arguments
128         while (<>){
129                 if (/^ \*\*?\//){
130                     $body =~ s/[@#](\w+)/<i>\1<\/i>/g;
131                     $returns =~ s/[@#](\w+)/<i>\1<\/i>/g;
132
133                     $args =~ s/@(\w+)/<i>\1<\/i>/g;
134                     $bodies{$func} = $body;
135                     $arguments{$func} = $args;
136                     $returns{$func} = $returns;
137                     $proto = "";
138                     while (<>){
139                         $proto .= $_;
140                         last if (/\{/);
141                     }
142                     $proto =~ s/{//;
143                     $prototype{$func} = $proto;
144                     return;
145                 }
146                 chop;
147                 s/^\ \*//;
148                 $_ = "\n<p>" if (/^\s+$/);
149                                 
150                 if ($inbody == 0){
151                     if (/\s*(\w+):(.*)/){
152                         $args .= "<dt><i>$1:</i></dt><dd>$2</dd>";
153                     } else {
154                         
155                         $body = "\t$_\n";
156                         $inbody = 1;
157                     }
158                 } elsif ($inbody == 1) {
159                     if (/Returns:/){
160                         s/Returns://;
161                         $returns = "\t$_\n";
162                         $inbody = 2;
163                     } else {
164                         $body .= "\n\t$_";
165                     }
166                 } else {
167                     $returns .= "\n\t$_";
168                 }
169                    
170         }
171 }