Merge pull request #2542 from akoeplinger/remove-changelog
[mono.git] / docs / exdoc
1 #!/usr/bin/perl
2
3 if ($ARGV[0] eq "-h"){
4     $sourcedir = $ARGV[1];
5     $dir = $sourcedir;
6     $html = 1;
7     shift @ARGV;
8     shift @ARGV;
9 }
10
11 if ($ARGV[0] eq "-t"){
12     $dir = $ARGV[1];
13     shift @ARGV;
14 }
15
16 if ($html){
17     opendir (D, "$sourcedir/sources/") || die "Can not open $dir";
18     while ($n = readdir (D)){
19         if ($n =~ /mono-api-.*\.html$/){
20             open (IN, "$sourcedir/sources/$n") || die "Can not open $n";
21             $files[$filecount] = $n;
22             while (<IN>){
23                 @files_content[$filecount] .= $_;
24                 if (/name="api:(.*?)"/){
25                     $_ =~ s/.*name="api:(\w+?)".*/\1/;
26                     $apis[$filecount] .= "$_";
27                 }
28             }
29             $filecount++;
30             close IN;
31         }
32     }
33 }
34
35 while (<ARGV>){
36         if (/\/\*\* *\n/){
37                 &process_doc;
38         } else {
39                 #print "IGNORING: $_";
40         }
41 }
42
43 if ($html){
44     for ($f = 0; $f < $filecount; $f++){
45         $name = $files[$f];
46         open (OUT, "> $dir/html/$name") || die "Can not create $dir/html/$name";
47         print "Merging: $name\n";
48         print OUT<<EOF;
49 <?xml version="1.0" encoding="utf-8"?>
50 <html xmlns="http://www.w3.org/1999/xhtml">
51 <head>
52    <title>$name</title>
53    <style type="text/css">
54    h3 { 
55        font-size: 18px;
56        padding-bottom: 4pt;
57        border-bottom: 2px solid #dddddd;
58    }
59        
60    .api {
61      padding: 10pt;
62      margin: 10pt;
63      border: 3px solid #ddd;
64    } 
65
66    .api-entry { 
67        border-bottom: none;
68        font-size: 150%;
69        font-family: "Consolas", "Courier", monospace;
70        padding-bottom: .5em;
71    }
72
73    .prototype {
74      border: 3px solid #ecf0f1;
75      border-radius: 6px;
76      padding: .5em;
77      margin-top: 5pt;
78      margin-bottom: 5pt;
79      font-family: "Consolas", "Courier", monospace;
80      display: block;
81      overflow: auto;
82      background-color: #f9f9f9;
83    } 
84
85    .header {
86      
87      padding: 0 0 5pt 5pt;
88      margin: 10pt;
89      white-space: pre;
90        font-family: monospace;
91    }
92     
93    .code {
94      border: 1px solid;
95      padding: 0 0 5pt 5pt;
96      margin: 10pt;
97      white-space: pre;
98        font-family: monospace;
99    }
100
101    .strike {
102       text-decoration: line-through;
103    }
104         .deprecated {
105             color: red;
106         }
107    </style>
108 </head>
109 <body>
110 EOF
111         @a = split (/\n/, $files_content[$f]);
112         $strikeextra = "";
113         for ($ai = 0; $ai < $#a; $ai++){
114             $line = $a[$ai];
115             
116             ($api,$caption) = $line =~  /<h4><a name=\"api:(\w+)\">(\w+)<\/a><\/h4>/;
117             if ($api ne ""){
118                 if ($api_shown == 1){
119                     print OUT "</div>";
120                     if ($deprecated{$api}){
121                         $strike = "strike";
122                         $strikeextra = "</div><br><div class='deprecated'><b>Deprecated:</b> " . $deprecated{$api};
123                     } else {
124                         $strike = "";
125                         $strikeextra = "";
126                     }
127                 }
128                 $api_shown = 1;
129                 $proto = $prototype{$api};
130                 if ($proto eq ""){
131                     $proto = "Prototype: $api";
132                 }
133
134 print OUT<<EOF;
135  <a name="api:$api"></a>
136  <div class="api">
137     <div class="api-entry $strike">$api$strikeextra</div>
138     <div><b>Syntax</b></div>
139     <div class="prototype">$proto</div>
140 <p>
141 EOF
142                     &opt_print ("Parameters", $arguments{$api}, 1);
143                     &opt_print ("Returns", $returns{$api}, 1);
144                     &opt_print ("Description", $bodies{$api}, 0);
145                     print OUT "\n";
146             } else {
147                 if ($line =~ /@API_IDX@/){
148                     $apis_toc = &create_toc ($apis[$f]);
149                     $line =~ s/\@API_IDX\@/$apis_toc/;
150                 }
151                 if ($line =~ /^<h/){
152                     print OUT "</div>";
153                     $api_shown = 0;
154                 }
155
156                 print OUT "$line\n";
157             }
158         }
159         print OUT<<EOF;
160 </body>
161 </html>
162 EOF
163         close OUT;
164         system ("$ENV{runtimedir}/mono-wrapper convert.exe $dir/html/$name $dir/html/x-$name");
165
166         # clean up the mess that AgilityPack does, it CDATAs our CSS
167         open HACK, "$dir/html/x-$name" || die "Could not open $dir/html/x-$name";
168         open HACKOUT, ">$dir/deploy/$name" || die "Could not open output";
169
170         while (<HACK>){
171             s/^\/\/<!\[CDATA\[//;
172             s/^\/\/\]\]>\/\///;
173             print HACKOUT $_;
174         }
175         #system ("cp.exe $dir/html/$name $dir/deploy/$name");
176     }
177 }
178
179 sub process_doc {
180         $doc = "";
181         $func = <>;
182         chop $func;
183         $func =~ s/^ \* //;
184         $func =~ s/:$//;
185         print "Function: $func\n" if (!$html);
186         $args = "";
187         $inbody = 0;
188         $returns = "";
189         $body = "";
190         $functions[$fn++] = $func;
191         $deprecated = 0;
192         
193         # Process arguments
194         while (<>){
195                 if (/^ \*\*?\//){
196                     $body =~ s/[@#](\w+)/<i>\1<\/i>/g;
197                     $returns =~ s/[@#](\w+)/<i>\1<\/i>/g;
198
199                     $args =~ s/@(\w+)/<i>\1<\/i>/g;
200                     $body =~ s/\n/ /;
201                     $bodies{$func} = $body;
202                     $arguments{$func} = $args;
203                     $deprecated{$func} = $deprecated;
204                     $returns{$func} = $returns;
205                     $proto = "";
206                     while (<>){
207                         $proto .= $_;
208                         last if (/\{/);
209                     }
210                     $proto =~ s/{//;
211                     # clean it up a little, remove newlines, empty space at end
212                     $proto =~ s/ +$//;
213                     # Turn "Type * xxx" into "Type* xxx"
214                     $proto =~ s/^(\w+)\W+\*/\1\*/;
215                     $prototype{$func} = $proto;
216                     return;
217                 }
218                 chop;
219                 s/^\ \*//;
220                 $_ = "\n<p>" if (/^\s+$/);
221                                 
222                 if ($inbody == 0){
223                     if (/\s*(\w+):(.*)/){
224                         if ($1 eq "deprecated"){
225                             $deprecated = $2;
226                         } else {
227                             $args .= "<dt><i>$1:</i></dt><dd>$2</dd>";
228                         }
229                     } else {
230                         
231                         $body = "\t$_\n";
232                         $inbody = 1;
233                     }
234                 } elsif ($inbody == 1) {
235                     if (/Returns?:/){
236                         s/Returns?://;
237                         $returns = "\t$_\n";
238                         $inbody = 2;
239                     } else {
240                         $body .= "\n\t$_";
241                     }
242                 } else {
243                     $returns .= "\n\t$_";
244                 }
245                    
246         }
247 }
248
249 sub create_toc {
250     my ($apis_listed) = @_;
251     my $type_size = 0;
252     my $name_size = 0;
253     my $ret, $xname, $args, $line;
254     $apis_toc = "";
255
256
257     # Try to align things, so compute type size, method size, and arguments
258     foreach $line (split /\n/, $apis_listed){
259         $p = $prototype{$line};
260         ($ret, $xname, $args) = $p =~ /(.*)\n(\w+)[ \t](.*)/;
261         $tl = length ($ret);
262         $pl = length ($xname);
263
264         $type_size = $tl if ($tl > $type_size);
265         $name_size = $pl if ($pl > $name_size);
266     }
267
268     $type_size++;
269     $name_size++;
270
271     foreach $line (split /\n/, $apis_listed){
272         chop;
273         $p = $prototype{$line};
274         ($ret, $xname, $args) = $p =~ /(.*)\n(\w+)[ \t](.*)/;
275         
276         $rspace = " " x ($type_size - length ($ret));
277         $nspace = " " x ($name_size - length ($xname));
278         $args = &format ($args, length ($ret . $rspace . $xname . $nspace), 60);
279         $apis_toc .= "$ret$rspace<a href=\"\#api:$line\">$xname</a>$nspace$args\n";
280     }
281     return $apis_toc;
282 }
283
284 #
285 # Formats the rest of the arguments in a way that will fit in N columns
286 #
287 sub format {
288     my ($args, $size, $limit) = @_;
289     my $sret = "";
290
291 #    return $args if ((length (args) + size) < $limit);
292     
293     $remain = $limit - $size;
294     @sa = split /,/, $args;
295     $linelen = $size;
296     foreach $arg (@sa){
297         if ($sret eq ""){
298             $sret = $arg . ", ";
299             $linelen += length ($sret);
300         } else {
301             if ($linelen + length ($arg) < $limit){
302                 $sret .= "FITS" . $arg . ", ";
303             } else {
304                 $newline = " " x ($size) . $arg . ", ";
305                 $linelen = length ($newline);
306                 $sret .= "\n" . $newline;
307             }
308         }
309     }
310     $sret =~ s/, $/;/;
311     return $sret;
312 }
313
314 sub opt_print {
315     my ($caption, $opttext, $quote) = @_;
316
317     if ($opttext ne "" && (!($opttext =~ /^[ \t]+$/))){
318         print OUT "<b>$caption</b>\n";
319         if ($quote == 1){
320             print OUT "<blockquote>$opttext</blockquote>\n";
321         } else {
322             print OUT "<p>$opttext\n";
323         }
324     }
325 }