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