44db88382bcb53392cce4ff53a6cb039cd724546
[mono.git] / docs / exdoc
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Getopt::Long;
7 use Pod::Usage;
8
9 # Options
10 my $HELP = 0;
11 my $SOURCE_DIR = '';
12 my $TARGET_DIR = '';
13 my $WARNINGS = 0;
14
15 GetOptions(
16     "help" => \$HELP,
17     "html|h=s" => \$SOURCE_DIR,
18     "target|t=s" => \$TARGET_DIR,
19     "warnings|W" => \$WARNINGS,
20 ) or pod2usage(1);
21
22 pod2usage(0) if $HELP;
23
24 exdoc();
25
26 #
27 # Main entry point.
28 #
29 sub exdoc {
30     my %templates = ();
31     my %docs = ();
32     my $stylesheet = load_stylesheet($SOURCE_DIR);
33     load_templates($SOURCE_DIR, \%templates);
34     process_source_files(\%docs);
35     merge(\%docs, \%templates, \$stylesheet);
36 }
37
38 #
39 # Load CSS stylesheet.
40 #
41 sub load_stylesheet {
42     my ($dir_path) = @_;
43     my $file_path = "$dir_path/api-style.css";
44     open (my $file, '<', $file_path) or die "Could not open $file_path";
45     local $/;
46     my $contents = <$file>;
47     close $file;
48     return $contents;
49 }
50
51 #
52 # Load HTML templates.
53 #
54 sub load_templates {
55     my ($dir_path, $templates) = @_;
56     opendir (my $dir, "$dir_path/sources/") or die "Could not open $dir_path";
57     while (my $file_name = readdir ($dir)) {
58         next if $file_name !~ /mono-api-.*\.html$/;
59         open (my $file, "$dir_path/sources/$file_name") or die "Could not open $file_name";
60         my $contents = '';
61         my @api = ();
62         while (<$file>) {
63             $contents .= $_;
64             if (/name="api:(.*?)"/) {
65                 s/.*name="api:(\w+?)".*/$1/;
66                 push @api, $_;
67             }
68         }
69         close $file;
70         $templates->{$file_name}->{contents} = $contents;
71         $templates->{$file_name}->{api} = \@api;
72     }
73     closedir $dir;
74 }
75
76 #
77 # Extract documentation from all source files.
78 #
79 sub process_source_files {
80     my ($docs) = @_;
81     for my $file_path (@ARGV) {
82         process_source_file($file_path, $docs);
83     }
84 }
85
86 #
87 # Extract documentation from a single source file.
88 #
89 sub process_source_file {
90     my ($file_path, $docs) = @_;
91     open (my $file, '<', $file_path) or die "Could not open $file_path";
92     while (<$file>) {
93         next if (!/\/\*\* *\n/);
94         process_function($file, $file_path, $docs);
95     }
96     close $file;
97 }
98
99 #
100 # Extract documentation from a single function.
101 #
102 sub process_function {
103
104     my ($file, $file_path, $docs) = @_;
105
106     my $PARAMETER_SECTION = 0;
107     my $BODY_SECTION = 1;
108     my $RETURN_SECTION = 2;
109     my $section = $PARAMETER_SECTION;
110
111     my $name = do {
112         $_ = <$file>;
113         chomp;
114         s/^ \* //;
115         s/:$//;
116         $_
117     };
118
119     # Ignore irrelevant functions, and those with the wrong doc format.
120     return if $name !~ /^mono_\w+$/;
121
122     my $deprecated;
123     my @parameters = ();
124     my $body = '';
125     my $returns = '';
126     my $prototype = '';
127
128     while (<$file>) {
129
130         # We've reached the last line in the documentation block.
131         if (/^ \*\*?\//) {
132
133             # Grab function prototype.
134             while (<$file>) {
135                 $prototype .= $_;
136                 last if /\{/;
137             }
138
139             # Clean up prototype.
140             $prototype = do {
141                 $_ = $prototype;
142                 # Strip braces and trailing whitespace.
143                 s/{//;
144                 s/ +$//;
145                 # Turn "Type * xxx" into "Type* xxx"
146                 s/^(\w+)\W+\*/$1*/;
147                 $_;
148             };
149
150             # Process formatting within sections.
151             for my $parameter (@parameters) {
152                 process_formatting(\$parameter->{description}, $file_path, $.);
153             }
154             process_formatting(\$returns, $file_path, $.);
155             process_formatting(\$body, $file_path, $.);
156             $body =~ s/\n/ /g;
157
158             if (exists($docs->{body}->{$name})) {
159                 my $origin = $docs->{origin}->{$name};
160                 if ($WARNINGS) {
161                     warn
162                       "$file_path:$.: Redundant documentation for $name\n",
163                       "$origin->{file}:$origin->{line}: Previously defined here\n";
164                 }
165             }
166             $docs->{origin}->{$name} = { file => $file_path, line => $. };
167             $docs->{body}->{$name} = $body;
168             $docs->{parameters}->{$name} = \@parameters;
169             $docs->{deprecated}->{$name} = $deprecated if defined $deprecated;
170             $docs->{return}->{$name} = $returns;
171             $docs->{prototype}->{$name} = $prototype;
172             last;
173
174         }
175
176         # Strip newlines and asterisk prefix.
177         chomp;
178         s/^ +\*//;
179
180         # Replace blank lines with paragraph breaks.
181         $_ = '<p>' if /^\s*$/;
182
183         if ($section == $PARAMETER_SECTION) {
184             if (/\s*\\param +(\w+)(.*)/) {
185                 # print "$file_path:$.: warning: Got parameter $1\n";
186                 push @parameters, { name => $1, description => $2 };
187             } elsif (/\s*\\deprecated(.*)/) {
188                 # print "$file_path:$.: warning: Got deprecated annotation\n";
189                 $deprecated = $1;
190             } elsif (/\s*(\w+):(.*)/) {
191                 if ($1 eq 'deprecated') {
192                     warn "$file_path:$.: Old-style monodoc notation 'deprecated:' used\n"
193                         if $WARNINGS;
194                     $deprecated = $2;
195                 } else {
196                     warn "$file_path:$.: Old-style monodoc notation 'param:' used\n"
197                         if $WARNINGS;
198                     push @parameters, { name => $1, description => $2 };
199                 }
200             } else {
201                 # $body = "\t$_\n";
202                 $section = $BODY_SECTION;
203                 redo;
204             }
205         } elsif ($section == $BODY_SECTION) {
206             if (s/(Returns?:\s*|\\returns?\s*)//) {
207                 $returns = "\t$_\n";
208                 $section = $RETURN_SECTION;
209             } else {
210                 $body .= "\n\t$_";
211             }
212         } elsif ($section == $RETURN_SECTION) {
213             $returns .= "\n\t$_";
214         } else {
215             die "Invalid section $section\n";
216         }
217     }
218 }
219
220 #
221 # Substitute formatting within documentation text.
222 #
223 sub process_formatting {
224     my ($content, $file_path, $current_line) = @_;
225     $_ = $$content;
226
227     # Constants
228     s{NULL}{<code>NULL</code>}g;
229     s{TRUE}{<code>TRUE</code>}g;
230     s{FALSE}{<code>FALSE</code>}g;
231
232     # Parameters
233     warn "$file_path:$current_line: Old-style monodoc notation '\@param' used\n"
234         if s{@(\w+)}{<i>$1</i>}g && $WARNINGS;
235     s{\\p +(\w+)}{<i>$1</i>}g;
236
237     # Code
238     warn "$file_path:$current_line: Old-style monodoc notation '#code' used\n"
239         if s{#(\w+)}{<code>$1</code>}g && $WARNINGS;
240     warn "$file_path:$current_line: Old-style monodoc notation '`code`' used\n"
241         if s{\`((?!api:)[:.\w\*]+)\`}{<code>$1</code>}g && $WARNINGS;
242     s{\\c +(\S+(?<![.,:;]))}{<code>$1</code>}g;
243
244     $$content = $_;
245 }
246
247 #
248 # Merge templates with stylesheet and documentation extracted from sources.
249 #
250 sub merge {
251     my ($docs, $templates, $stylesheet) = @_;
252     my $last = '';
253     for my $name (keys %$templates) {
254         open (my $output_file, '>', "$TARGET_DIR/html/$name")
255           or die "Could not create $TARGET_DIR/html/$name";
256         print "Merging: $name\n";
257         print $output_file <<EOF;
258 <?xml version="1.0" encoding="utf-8"?>
259 <html xmlns="http://www.w3.org/1999/xhtml">
260 <head>
261     <title>$name</title>
262     <style type="text/css">
263 $stylesheet
264    </style>
265 </head>
266 <body>
267 <div class="mapi-docs">
268 EOF
269         my @a = split (/\n/, $templates->{$name}->{contents});
270         my $strike = '';
271         my $strikeextra = '';
272         my $api_shown = 0;
273         for (my $ai = 0; $ai < $#a; $ai++) {
274             my $line = $a[$ai];
275             if (my ($api, $caption) = ($line =~ /<h4><a name=\"api:(\w+)\">(\w+)<\/a><\/h4>/)) {
276                 if ($api_shown == 1) {
277                     print $output_file "</div> <!-- class=mapi -->\n\n";
278                     if ($docs->{deprecated}->{$api}) {
279                         $strike = "mapi-strike";
280                         $strikeextra = "</div><br><div class='mapi-deprecated'><b>Deprecated:</b> " . $docs->{deprecated}->{$api};
281                     } else {
282                         $strike = "";
283                         $strikeextra = "";
284                     }
285                 }
286                 $api_shown = 1;
287                 my $proto = $docs->{prototype}->{$api} // $api;
288
289                 print $output_file <<EOF;
290 <a name="api:$api"></a>
291 <div class="mapi">
292     <div class="mapi-entry $strike"><code>$api$strikeextra</code></div>
293     <div class="mapi-height-container">
294         <div class="mapi-ptr-container"></div>
295         <div class="mapi-description">
296             <div class="mapi-ptr"></div>
297
298             <div class="mapi-declaration mapi-section">Syntax</div>
299             <div class="mapi-prototype">$proto</div>
300             <p>
301 EOF
302                 if (exists ($docs->{parameters}->{$api})) {
303                     my $ppars = $docs->{parameters}->{$api};
304                     if (@$ppars) {
305                         print $output_file
306                           "            <div class=\"mapi-section\">Parameters</div>\n",
307                           "            <table class=\"mapi-parameters\"><tbody>",
308                           render_parameters($ppars),
309                           "</tbody></table>";
310                     }
311                 }
312
313                 opt_print ($output_file, "Return value", $docs->{return}->{$api});
314                 opt_print ($output_file, "Description", $docs->{body}->{$api});
315                 print $output_file "        </div><!--mapi-description-->\n    </div><!--height container-->\n";
316             } else {
317                 if ($line =~ /\@API_IDX\@/) {
318                     my $apis_toc = create_toc ($docs, $templates->{$name}->{api});
319                     $line =~ s/\@API_IDX\@/$apis_toc/;
320                 }
321                 if ($line =~ /^<h4/) {
322                     print $output_file "</div>\n";
323                     $api_shown = 0;
324                 }
325                 if ($line =~ /`/) {
326                 }
327                 print $output_file "$line\n";
328             }
329         }
330         print $output_file
331           "   </div>",
332           "</body>",
333           "</html>";
334         close $output_file;
335         system ("$ENV{runtimedir}/mono-wrapper convert.exe $TARGET_DIR/html/$name $TARGET_DIR/html/x-$name");
336
337         # Clean up the mess that AgilityPack makes (it CDATAs our CSS).
338         open (my $hack_input, '<', "$TARGET_DIR/html/x-$name")
339           or die "Could not open $TARGET_DIR/html/x-$name";
340         open (my $hack_output, '>', "$TARGET_DIR/deploy/$name")
341           or die "Could not open output";
342
343         my $line = 0;
344         my $doprint = 0;
345         while (<$hack_input>) {
346             print $hack_output $last if ($doprint);
347             $line++;
348             s/^\/\/<!\[CDATA\[//;
349             s/^\/\/\]\]>\/\///;
350
351             # Remove the junk <span> wrapper generated by AgilityPack.
352             if ($line==1) {
353                 s/<span>//;
354             }
355             if (/<style type/) {
356                 # Replace the CSS in the XHTML output with the original CSS.
357                 print $hack_output $_;
358                 print $hack_output $$stylesheet;
359                 while (<$hack_input>) {
360                     last if (/<\/style>/);
361                 }
362             }
363             $last = $_;
364             $doprint = 1;
365         }
366         if (!($last =~ /span/)) {
367             print $hack_output $last;
368         }
369         # system ("cp.exe $TARGET_DIR/html/$name $TARGET_DIR/deploy/$name");
370     }
371 }
372
373 sub create_toc {
374     my ($docs, $apis_listed) = @_;
375     my $type_size = 0;
376     my $name_size = 0;
377     my ($ret, $xname, $args);
378     my $apis_toc = "";
379
380     # Try to align things; compute type size, method size, and arguments.
381     foreach my $line (split /\n/, $apis_listed) {
382         if (exists ($docs->{prototype}->{$line})) {
383             my $p = $docs->{prototype}->{$line};
384             if (my ($ret, $xname, $args) = ($p =~ /(.*)\n(\w+)[ \t](.*)/)) {
385                 my $tl = length ($ret);
386                 my $pl = length ($xname);
387                 $type_size = $tl if ($tl > $type_size);
388                 $name_size = $pl if ($pl > $name_size);
389             }
390         }
391     }
392
393     $type_size++;
394     $name_size++;
395
396     foreach my $line (split /\n/, $apis_listed) {
397         chomp($line);
398         if (exists($docs->{prototype}->{$line})) {
399             my $p = $docs->{prototype}->{$line};
400             if (my ($ret, $xname, $args) = ($p =~ /(.*)\n(\w+)[ \t](.*)/)) {
401                 $xname = $line if $xname eq "";
402                 my $rspace = " " x ($type_size - length ($ret));
403                 my $nspace = " " x ($name_size - length ($xname));
404                 $args = wrap ($args, length ($ret . $rspace . $xname . $nspace), 60);
405                 $apis_toc .= "$ret$rspace<a href=\"\#api:$line\">$xname</a>$nspace$args\n";
406             }
407         }
408     }
409     return $apis_toc;
410 }
411
412 sub wrap {
413     my ($args, $size, $limit) = @_;
414     my $sret = "";
415
416     # return $args if ((length (args) + size) < $limit);
417     
418     my $remain = $limit - $size;
419     my @sa = split /,/, $args;
420     my $linelen = $size;
421     foreach my $arg (@sa) {
422         if ($sret eq "") {
423             $sret = $arg . ", ";
424             $linelen += length ($sret);
425         } else {
426             if ($linelen + length ($arg) < $limit) {
427                 $sret .= "FITS" . $arg . ", ";
428             } else {
429                 my $newline = " " x ($size) . $arg . ", ";
430                 my $linelen = length ($newline);
431                 $sret .= "\n" . $newline;
432             }
433         }
434     }
435     $sret =~ s/, $/;/;
436     return $sret;
437 }
438
439 #
440 # Print a section if non-empty.
441 #
442 sub opt_print {
443     my ($output, $caption, $opttext) = @_;
444     if (defined($opttext) && $opttext ne '' && $opttext !~ /^[ \t]+$/) {
445         print $output
446           "             <div class=\"mapi-section\">$caption</div>\n",
447           "             <div>$opttext</div>\n";
448     }
449 }
450
451 #
452 # Render parameter information as table.
453 #
454 sub render_parameters {
455     my ($parameters) = @_;
456     my $result = '';
457     for my $parameter (@$parameters) {
458         $result .= "<tr><td><i>$parameter->{name}</i></td><td>$parameter->{description}</td></tr>";
459     }
460     return $result;
461 }
462
463 __END__
464
465 =head1 NAME
466
467 exdoc - Compiles API docs from Mono sources and HTML templates.
468
469 =head1 SYNOPSIS
470
471     exdoc [OPTIONS] [FILE...]
472
473 =head1 OPTIONS
474
475 =over 4
476
477 =item B<--help>
478
479 Print this help message.
480
481 =item B<--html> I<DIR>, B<-h> I<DIR>
482
483 Use I<DIR> as the input path for HTML sources.
484
485 =item B<--target> I<DIR>, B<-t> I<DIR>
486
487 Use I<DIR> as the target path for output.
488
489 =item B<--warnings>, B<-W>
490
491 Enable warnings about documentation errors.
492
493 =back
494
495 =head1 DESCRIPTION
496
497 Reads HTML templates and C sources, extracting documentation from the sources and splicing it into the templates.
498
499 =cut