[Documentation] Document some of Mono's embedding API, improve the style
[mono-project.git] / docs / exdoc
blob4e60e1b90c744e7ae5a730d5b669cf47b2d4c6f3
1 #!/usr/bin/perl
3 if ($ARGV[0] eq "-h"){
4 $sourcedir = $ARGV[1];
5 $dir = $sourcedir;
6 $html = 1;
7 shift @ARGV;
8 shift @ARGV;
11 if ($ARGV[0] eq "-t"){
12 $dir = $ARGV[1];
13 shift @ARGV;
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] .= "$_";
29 $filecount++;
30 close IN;
35 while (<ARGV>){
36 if (/\/\*\* *\n/){
37 &process_doc;
38 } else {
39 #print "IGNORING: $_";
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;
60 .api {
61 padding: 10pt;
62 margin: 10pt;
65 .api-entry {
66 border-bottom: none;
67 font-size: 120%;
70 .prototype {
71 border: 3px solid #ecf0f1;
72 border-radius: 6px;
73 padding: 1em 1.2em;
74 margin-top: 5pt;
75 margin-bottom: 5pt;
76 font-family: "Consolas", "Courier", monospace;
77 display: block;
78 overflow: auto;
81 .header {
83 padding: 0 0 5pt 5pt;
84 margin: 10pt;
85 white-space: pre;
86 font-family: monospace;
89 .code {
90 border: 1px solid;
91 padding: 0 0 5pt 5pt;
92 margin: 10pt;
93 white-space: pre;
94 font-family: monospace;
96 </style>
97 </head>
98 <body>
99 EOF
100 @a = split (/\n/, $files_content[$f]);
102 for ($ai = 0; $ai < $#a; $ai++){
103 $line = $a[$ai];
105 ($api,$caption) = $line =~ /<h4><a name=\"api:(\w+)\">(\w+)<\/a><\/h4>/;
106 if ($api ne ""){
107 if ($api_shown == 1){
108 print OUT "</div>";
109 if ($deprecated{$api}){
110 print OUT "<p><b>DEPRECATED ${deprecated{$api}}</b>";
113 $api_shown = 1;
114 $proto = $prototype{$api};
115 if ($proto eq ""){
116 $proto = "Prototype: $api";
119 print OUT<<EOF;
120 <a name="api:$api"></a>
121 <div class="api">
122 <div class="api-entry">$api</div>
124 <div class="prototype">$proto</div>
127 &opt_print ("Parameters", $arguments{$api}, 1);
128 &opt_print ("Returns", $returns{$api}, 1);
129 &opt_print ("Description", $bodies{$api}, 0);
130 print OUT "\n";
131 } else {
132 if ($line =~ /@API_IDX@/){
133 $apis_toc = &create_toc ($apis[$f]);
134 $line =~ s/\@API_IDX\@/$apis_toc/;
136 if ($line =~ /^<h/){
137 print OUT "</div>";
138 $api_shown = 0;
141 print OUT "$line\n";
144 print OUT<<EOF;
145 </body>
146 </html>
148 close OUT;
149 system ("$ENV{runtimedir}/mono-wrapper convert.exe $dir/html/$name $dir/html/x-$name");
151 # clean up the mess that AgilityPack does, it CDATAs our CSS
152 open HACK, "$dir/html/x-$name" || die "Could not open $dir/html/x-$name";
153 open HACKOUT, ">$dir/deploy/$name" || die "Could not open output";
155 while (<HACK>){
156 s/^\/\/<!\[CDATA\[//;
157 s/^\/\/\]\]>\/\///;
158 print HACKOUT $_;
160 #system ("cp.exe $dir/html/$name $dir/deploy/$name");
164 sub process_doc {
165 $doc = "";
166 $func = <>;
167 chop $func;
168 $func =~ s/^ \* //;
169 $func =~ s/:$//;
170 print "Function: $func\n" if (!$html);
171 $args = "";
172 $inbody = 0;
173 $returns = "";
174 $body = "";
175 $functions[$fn++] = $func;
176 $deprecated = 0;
178 # Process arguments
179 while (<>){
180 if (/^ \*\*?\//){
181 $body =~ s/[@#](\w+)/<i>\1<\/i>/g;
182 $returns =~ s/[@#](\w+)/<i>\1<\/i>/g;
184 $args =~ s/@(\w+)/<i>\1<\/i>/g;
185 $body =~ s/\n/ /;
186 $bodies{$func} = $body;
187 $arguments{$func} = $args;
188 $deprecated{$func} = $deprecated;
189 $returns{$func} = $returns;
190 $proto = "";
191 while (<>){
192 $proto .= $_;
193 last if (/\{/);
195 $proto =~ s/{//;
196 # clean it up a little, remove newlines, empty space at end
197 $proto =~ s/ +$//;
198 # Turn "Type * xxx" into "Type* xxx"
199 $proto =~ s/^(\w+)\W+\*/\1\*/;
200 $prototype{$func} = $proto;
201 return;
203 chop;
204 s/^\ \*//;
205 $_ = "\n<p>" if (/^\s+$/);
207 if ($inbody == 0){
208 if (/\s*(\w+):(.*)/){
209 if ($1 eq "deprecated"){
210 $deprecated = $2;
211 } else {
212 $args .= "<dt><i>$1:</i></dt><dd>$2</dd>";
214 } else {
216 $body = "\t$_\n";
217 $inbody = 1;
219 } elsif ($inbody == 1) {
220 if (/Returns?:/){
221 s/Returns?://;
222 $returns = "\t$_\n";
223 $inbody = 2;
224 } else {
225 $body .= "\n\t$_";
227 } else {
228 $returns .= "\n\t$_";
234 sub create_toc {
235 my ($apis_listed) = @_;
236 my $type_size = 0;
237 my $name_size = 0;
238 my $ret, $xname, $args, $line;
239 $apis_toc = "";
242 # Try to align things, so compute type size, method size, and arguments
243 foreach $line (split /\n/, $apis_listed){
244 $p = $prototype{$line};
245 ($ret, $xname, $args) = $p =~ /(.*)\n(\w+)[ \t](.*)/;
246 $tl = length ($ret);
247 $pl = length ($xname);
249 $type_size = $tl if ($tl > $type_size);
250 $name_size = $pl if ($pl > $name_size);
253 $type_size++;
254 $name_size++;
256 foreach $line (split /\n/, $apis_listed){
257 chop;
258 $p = $prototype{$line};
259 ($ret, $xname, $args) = $p =~ /(.*)\n(\w+)[ \t](.*)/;
261 $rspace = " " x ($type_size - length ($ret));
262 $nspace = " " x ($name_size - length ($xname));
263 $args = &format ($args, length ($ret . $rspace . $xname . $nspace), 60);
264 $apis_toc .= "$ret$rspace<a href=\"\#api:$line\">$xname</a>$nspace$args\n";
266 return $apis_toc;
270 # Formats the rest of the arguments in a way that will fit in N columns
272 sub format {
273 my ($args, $size, $limit) = @_;
274 my $sret = "";
276 # return $args if ((length (args) + size) < $limit);
278 $remain = $limit - $size;
279 @sa = split /,/, $args;
280 $linelen = $size;
281 foreach $arg (@sa){
282 if ($sret eq ""){
283 $sret = $arg . ", ";
284 $linelen += length ($sret);
285 } else {
286 if ($linelen + length ($arg) < $limit){
287 $sret .= "FITS" . $arg . ", ";
288 } else {
289 $newline = " " x ($size) . $arg . ", ";
290 $linelen = length ($newline);
291 $sret .= "\n" . $newline;
295 $sret =~ s/, $/;/;
296 return $sret;
299 sub opt_print {
300 my ($caption, $opttext, $quote) = @_;
302 if ($opttext ne "" && (!($opttext =~ /^[ \t]+$/))){
303 print OUT "<b>$caption</b>\n";
304 if ($quote == 1){
305 print OUT "<blockquote>$opttext</blockquote>\n";
306 } else {
307 print OUT "<p>$opttext\n";