[exdoc] Support Doxygen syntax.
[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+)\s+(.*)/) {
185                 push @parameters, { name => $1, description => $2 };
186             } elsif (/\s*\\deprecated +(.*)/) {
187                 $deprecated = $1;
188             } elsif (/\s*(\w+):(.*)/) {
189                 warn "$file_path:$.: Old-style monodoc notation \@param used\n"
190                     if $WARNINGS;
191                 if ($1 eq 'deprecated') {
192                     $deprecated = $2;
193                 } else {
194                     push @parameters, { name => $1, description => $2 };
195                 }
196             } else {
197                 $body = "\t$_\n";
198                 $section = $BODY_SECTION;
199             }
200         } elsif ($section == $BODY_SECTION) {
201             if (s/(Returns?:|\\returns? )//) {
202                 $returns = "\t$_\n";
203                 $section = $RETURN_SECTION;
204             } else {
205                 $body .= "\n\t$_";
206             }
207         } elsif ($section == $RETURN_SECTION) {
208             $returns .= "\n\t$_";
209         } else {
210             die "Invalid section $section\n";
211         }
212     }
213 }
214
215 #
216 # Substitute formatting within documentation text.
217 #
218 sub process_formatting {
219     my ($content, $file_path, $current_line) = @_;
220     $_ = $$content;
221
222     # Constants
223     s{NULL}{<code>NULL</code>}g;
224     s{TRUE}{<code>TRUE</code>}g;
225     s{FALSE}{<code>FALSE</code>}g;
226
227     # Parameters
228     warn "$file_path:$current_line: Old-style monodoc notation \@param used\n"
229         if s{@(\w+)}{<i>$1</i>}g && $WARNINGS;
230     s{\\p +(\w+)}{<i>$1</i>}g;
231
232     # Code
233     warn "$file_path:$current_line: Old-style monodoc notation #code used\n"
234         if s{#(\w+)}{<code>$1</code>}g && $WARNINGS;
235     warn "$file_path:$current_line: Old-style monodoc notation `code` used\n"
236         if s{\`([:.\w\*]+)\`}{<code>$1</code>}g && $WARNINGS;
237     s{\\c +([\w\.]+)}{<code>$1</code>}g;
238
239     $$content = $_;
240 }
241
242 #
243 # Merge templates with stylesheet and documentation extracted from sources.
244 #
245 sub merge {
246     my ($docs, $templates, $stylesheet) = @_;
247     my $last = '';
248     for my $name (keys %$templates) {
249         open (my $output_file, '>', "$TARGET_DIR/html/$name")
250           or die "Could not create $TARGET_DIR/html/$name";
251         print "Merging: $name\n";
252         print $output_file <<EOF;
253 <?xml version="1.0" encoding="utf-8"?>
254 <html xmlns="http://www.w3.org/1999/xhtml">
255 <head>
256     <title>$name</title>
257     <style type="text/css">
258 $stylesheet
259    </style>
260 </head>
261 <body>
262 <div class="mapi-docs">
263 EOF
264         my @a = split (/\n/, $templates->{$name}->{contents});
265         my $strike = '';
266         my $strikeextra = '';
267         my $api_shown = 0;
268         for (my $ai = 0; $ai < $#a; $ai++) {
269             my $line = $a[$ai];
270             if (my ($api, $caption) = ($line =~ /<h4><a name=\"api:(\w+)\">(\w+)<\/a><\/h4>/)) {
271                 if ($api_shown == 1) {
272                     print $output_file "</div> <!-- class=mapi -->\n\n";
273                     if ($docs->{deprecated}->{$api}) {
274                         $strike = "mapi-strike";
275                         $strikeextra = "</div><br><div class='mapi-deprecated'><b>Deprecated:</b> " . $docs->{deprecated}->{$api};
276                     } else {
277                         $strike = "";
278                         $strikeextra = "";
279                     }
280                 }
281                 $api_shown = 1;
282                 my $proto = $docs->{prototype}->{$api} // $api;
283
284                 print $output_file <<EOF;
285 <a name="api:$api"></a>
286 <div class="mapi">
287     <div class="mapi-entry $strike"><code>$api$strikeextra</code></div>
288     <div class="mapi-height-container">
289         <div class="mapi-ptr-container"></div>
290         <div class="mapi-description">
291             <div class="mapi-ptr"></div>
292
293             <div class="mapi-declaration mapi-section">Syntax</div>
294             <div class="mapi-prototype">$proto</div>
295             <p>
296 EOF
297                 if (exists ($docs->{parameters}->{$api})) {
298                     my $ppars = $docs->{parameters}->{$api};
299                     if (@$ppars) {
300                         print $output_file
301                           "            <div class=\"mapi-section\">Parameters</div>\n",
302                           "            <table class=\"mapi-parameters\"><tbody>",
303                           render_parameters($ppars),
304                           "</tbody></table>";
305                     }
306                 }
307
308                 opt_print ($output_file, "Return value", $docs->{return}->{$api});
309                 opt_print ($output_file, "Description", $docs->{body}->{$api});
310                 print $output_file "        </div><!--mapi-description-->\n    </div><!--height container-->\n";
311             } else {
312                 if ($line =~ /\@API_IDX\@/) {
313                     my $apis_toc = create_toc ($docs, $templates->{$name}->{api});
314                     $line =~ s/\@API_IDX\@/$apis_toc/;
315                 }
316                 if ($line =~ /^<h4/) {
317                     print $output_file "</div>\n";
318                     $api_shown = 0;
319                 }
320                 if ($line =~ /`/) {
321                 }
322                 print $output_file "$line\n";
323             }
324         }
325         print $output_file
326           "   </div>",
327           "</body>",
328           "</html>";
329         close $output_file;
330         system ("$ENV{runtimedir}/mono-wrapper convert.exe $TARGET_DIR/html/$name $TARGET_DIR/html/x-$name");
331
332         # Clean up the mess that AgilityPack makes (it CDATAs our CSS).
333         open (my $hack_input, '<', "$TARGET_DIR/html/x-$name")
334           or die "Could not open $TARGET_DIR/html/x-$name";
335         open (my $hack_output, '>', "$TARGET_DIR/deploy/$name")
336           or die "Could not open output";
337
338         my $line = 0;
339         my $doprint = 0;
340         while (<$hack_input>) {
341             print $hack_output $last if ($doprint);
342             $line++;
343             s/^\/\/<!\[CDATA\[//;
344             s/^\/\/\]\]>\/\///;
345
346             # Remove the junk <span> wrapper generated by AgilityPack.
347             if ($line==1) {
348                 s/<span>//;
349             }
350             if (/<style type/) {
351                 # Replace the CSS in the XHTML output with the original CSS.
352                 print $hack_output $_;
353                 print $hack_output $$stylesheet;
354                 while (<$hack_input>) {
355                     last if (/<\/style>/);
356                 }
357             }
358             $last = $_;
359             $doprint = 1;
360         }
361         if (!($last =~ /span/)) {
362             print $hack_output $last;
363         }
364         # system ("cp.exe $TARGET_DIR/html/$name $TARGET_DIR/deploy/$name");
365     }
366 }
367
368 sub create_toc {
369     my ($docs, $apis_listed) = @_;
370     my $type_size = 0;
371     my $name_size = 0;
372     my ($ret, $xname, $args);
373     my $apis_toc = "";
374
375     # Try to align things; compute type size, method size, and arguments.
376     foreach my $line (split /\n/, $apis_listed) {
377         if (exists ($docs->{prototype}->{$line})) {
378             my $p = $docs->{prototype}->{$line};
379             if (my ($ret, $xname, $args) = ($p =~ /(.*)\n(\w+)[ \t](.*)/)) {
380                 my $tl = length ($ret);
381                 my $pl = length ($xname);
382                 $type_size = $tl if ($tl > $type_size);
383                 $name_size = $pl if ($pl > $name_size);
384             }
385         }
386     }
387
388     $type_size++;
389     $name_size++;
390
391     foreach my $line (split /\n/, $apis_listed) {
392         chomp($line);
393         if (exists($docs->{prototype}->{$line})) {
394             my $p = $docs->{prototype}->{$line};
395             if (my ($ret, $xname, $args) = ($p =~ /(.*)\n(\w+)[ \t](.*)/)) {
396                 $xname = $line if $xname eq "";
397                 my $rspace = " " x ($type_size - length ($ret));
398                 my $nspace = " " x ($name_size - length ($xname));
399                 $args = wrap ($args, length ($ret . $rspace . $xname . $nspace), 60);
400                 $apis_toc .= "$ret$rspace<a href=\"\#api:$line\">$xname</a>$nspace$args\n";
401             }
402         }
403     }
404     return $apis_toc;
405 }
406
407 sub wrap {
408     my ($args, $size, $limit) = @_;
409     my $sret = "";
410
411     # return $args if ((length (args) + size) < $limit);
412     
413     my $remain = $limit - $size;
414     my @sa = split /,/, $args;
415     my $linelen = $size;
416     foreach my $arg (@sa) {
417         if ($sret eq "") {
418             $sret = $arg . ", ";
419             $linelen += length ($sret);
420         } else {
421             if ($linelen + length ($arg) < $limit) {
422                 $sret .= "FITS" . $arg . ", ";
423             } else {
424                 my $newline = " " x ($size) . $arg . ", ";
425                 my $linelen = length ($newline);
426                 $sret .= "\n" . $newline;
427             }
428         }
429     }
430     $sret =~ s/, $/;/;
431     return $sret;
432 }
433
434 #
435 # Print a section if non-empty.
436 #
437 sub opt_print {
438     my ($output, $caption, $opttext) = @_;
439     if (defined($opttext) && $opttext ne '' && $opttext !~ /^[ \t]+$/) {
440         print $output
441           "             <div class=\"mapi-section\">$caption</div>\n",
442           "             <div>$opttext</div>\n";
443     }
444 }
445
446 #
447 # Render parameter information as table.
448 #
449 sub render_parameters {
450     my ($parameters) = @_;
451     my $result = '';
452     for my $parameter (@$parameters) {
453         $result .= "<tr><td><i>$parameter->{name}</i></td><td>$parameter->{description}</td></tr>";
454     }
455     return $result;
456 }
457
458 __END__
459
460 =head1 NAME
461
462 exdoc - Compiles API docs from Mono sources and HTML templates.
463
464 =head1 SYNOPSIS
465
466     exdoc [OPTIONS] [FILE...]
467
468 =head1 OPTIONS
469
470 =over 4
471
472 =item B<--help>
473
474 Print this help message.
475
476 =item B<--html> I<DIR>, B<-h> I<DIR>
477
478 Use I<DIR> as the input path for HTML sources.
479
480 =item B<--target> I<DIR>, B<-t> I<DIR>
481
482 Use I<DIR> as the target path for output.
483
484 =item B<--warnings>, B<-W>
485
486 Enable warnings about documentation errors.
487
488 =back
489
490 =head1 DESCRIPTION
491
492 Reads HTML templates and C sources, extracting documentation from the sources and splicing it into the templates.
493
494 =cut