Debian Codegraph @

doc/doxydown/doxydown.pl

raw
1 #!/usr/bin/env perl
2
3 $VERSION = "0.1.4";
4
5 use strict;
6 use warnings;
7 use Data::Dumper;
8 use Digest::MD5 qw(md5_hex);
9
10 my @modules;
11 my %options = ();
12 my $cur_module;
13 my $example_language = "lua";
14
15 my %languages = (
16 c => {
17 start => qr/^\s*\/\*\*\*(?:\s*|(\s+\S.+\s*))$/,
18 end => qr/^\s*\*+\/\s*$/,
19 filter => qr/^(?:\s*\*+\s?)?(\s*\S.+)\s*$/,
20 },
21 lua => {
22 start => qr/^\s*\--(?:\[\[\[+|-+)\s*$/,
23 end => qr/^\s*--(:?\]\]+|-+)\s*$/,
24 filter => qr/^(?:\s*--!?\s?)?(\s*\S?.+)\s*$/,
25 },
26 sql => {
27 start => qr/^\s*\--(?:\[\[+|-+)\s*$/,
28 end => qr/^\s*--(:?\]\]+|-+)\s*$/,
29 filter => qr/^(?:\s*--\s?)?(\s*\S.+)\s*$/,
30 },
31 pl => {
32 start => qr/^\s*\##+\s*$/,
33 end => qr/^\s*##+\s*/,
34 filter => qr/^(?:\s*#+\s?)(\s*\S.+)\s*$/,
35 },
36 );
37
38 my $function_re = qr/^\s*\@(function|fn|method)\s*(\S.+)$/oi;
39 my $struct_re = qr/^\s*\@(table|struct)\s*(\S.+)$/oi;
40 my $module_re = qr/^\s*\@(?:module|file)\s*(\S.+)$/oi;
41 my $language;
42
43 # /function print_module_markdown
44 sub print_module_markdown {
45 my ( $mname, $m ) = @_;
46 my $idline = $options{g} ? "" : " {#$m->{'id'}}";
47
48 print <<EOD;
49 ## Module `$mname`$idline
50
51 $m->{'data'}
52 EOD
53 if ( $m->{'example'} ) {
54 print <<EOD;
55
56 ### Example:
57
58 ~~~$m->{'example_language'}
59 $m->{'example'}
60 ~~~
61 EOD
62 }
63
64 sub print_func {
65 my ($f) = @_;
66
67 my $name = $f->{'name'};
68 my $id = $f->{'id'};
69
70 if ($f->{'brief'}) {
71 print "[`$name`](#$id) | ". $f->{'brief'} . "\n";
72 } else {
73 print "[`$name`](#$id) | No description\n";
74 }
75 }
76
77 sub print_table {
78 my ($f) = @_;
79
80 my $name = $f->{'name'};
81 my $id = $f->{'id'};
82
83 if ($f->{'brief'}) {
84 print "> [`$name`](#$id): ". $f->{'brief'} . "\n\n";
85 } else {
86 print "> [`$name`](#$id)\n\n";
87 }
88 }
89
90 print "\n### Brief content:\n\n";
91
92 if ($m->{'functions'}) {
93 if (scalar(@{ $m->{'functions'} }) > 0) {
94 print "**Functions**:\n\n";
95
96 print " Function | Description\n";
97 print "----------|------------\n";
98 foreach ( @{ $m->{'functions'} } ) {
99 print_func($_);
100 }
101 }
102 }
103
104 if ($m->{'methods'}) {
105 if (scalar(@{ $m->{'methods'} }) > 0) {
106 print "\n\n**Methods**:\n\n";
107 print " Method | Description\n";
108 print "----------|------------\n";
109 foreach ( @{ $m->{'methods'} } ) {
110 print_func($_);
111 }
112 }
113 }
114
115 if ($m->{'tables'}) {
116 if (scalar(@{ $m->{'tables'} }) > 0) {
117 print "\n\n**Tables**:\n\n";
118
119 foreach ( @{ $m->{'tables'} } ) {
120 print_table($_);
121 }
122 }
123 }
124
125 if ($m->{'structs'}) {
126 if (scalar(@{ $m->{'structs'} }) > 0) {
127 print "\n\n**Structs**:\n\n";
128
129 foreach ( @{ $m->{'structs'} } ) {
130 print_table($_);
131 }
132 }
133 }
134 }
135
136 # /function print_function_markdown
137 sub print_function_markdown {
138 my ( $type, $fname, $f ) = @_;
139
140 my $idline = $options{g} ? "" : " {#$f->{'id'}}";
141 print <<EOD;
142 ### $type `$fname`$idline
143
144 $f->{'data'}
145 EOD
146 print "\n**Parameters:**\n\n";
147
148 if ( $f->{'params'} && scalar @{ $f->{'params'} } > 0 ) {
149 foreach ( @{ $f->{'params'} } ) {
150 if ( $_->{'type'} ) {
151 print
152 "- `$_->{'name'} \{$_->{'type'}\}`: $_->{'description'}\n";
153 } else {
154 print "- `$_->{'name'}`: $_->{'description'}\n";
155 }
156 }
157 } else {
158 print "No parameters\n";
159 }
160
161 print "\n**Returns:**\n\n";
162
163 if ( $f->{'returns'} && scalar @{ $f->{'returns'} } > 0 ) {
164 foreach ( @{ $f->{'returns'} } ) {
165 if ( $_->{'type'} ) {
166 print "- `\{$_->{'type'}\}`: $_->{'description'}\n";
167 } else {
168 print "- $_->{'description'}\n";
169 }
170 }
171 } else {
172 print "No return\n";
173 }
174
175 if ( $f->{'available'} ) {
176 printf "\n**Available in:** %s\n", $f->{'available'};
177 }
178
179 if ( $f->{'example'} ) {
180 print <<EOD;
181
182 ### Example:
183
184 ~~~$f->{'example_language'}
185 $f->{'example'}
186 ~~~
187 EOD
188 }
189 }
190
191 # /function print_struct_markdown
192 sub print_struct_markdown {
193 my ( $type, $fname, $f ) = @_;
194
195 my $idline = $options{g} ? "" : " {#$f->{'id'}}";
196 print <<EOD;
197 ### $type `$fname`$idline
198
199 $f->{'data'}
200 EOD
201 print "\n**Elements:**\n\n";
202
203 if ( $f->{'params'} && scalar @{ $f->{'params'} } > 0 ) {
204 foreach ( @{ $f->{'params'} } ) {
205 if ( $_->{'type'} ) {
206 print
207 "- `$_->{'name'} \{$_->{'type'}\}`: $_->{'description'}\n";
208 } else {
209 print "- `$_->{'name'}`: $_->{'description'}\n";
210 }
211 }
212 } else {
213 print "No elements\n";
214 }
215
216 if ( $f->{'example'} ) {
217 print <<EOD;
218
219 ### Example:
220
221 ~~~$f->{'example_language'}
222 $f->{'example'}
223 ~~~
224 EOD
225 }
226 }
227
228 # /function print_markdown
229 sub print_markdown {
230 for my $m (@modules) {
231 my $mname = $m->{name};
232
233 print_module_markdown( $mname, $m );
234
235 if ($m->{'functions'}) {
236 if ( scalar(@{ $m->{'functions'} }) > 0 ) {
237 print "\n## Functions\n\nThe module `$mname` defines the following functions.\n\n";
238
239 foreach ( @{ $m->{'functions'} } ) {
240 print_function_markdown( "Function", $_->{'name'}, $_ );
241
242 print "\nBack to [module description](#$m->{'id'}).\n\n";
243
244 }
245 }
246 }
247
248 if ($m->{'methods'}) {
249 if ( scalar(@{ $m->{'methods'} }) > 0 ) {
250 print "\n## Methods\n\nThe module `$mname` defines the following methods.\n\n";
251
252 foreach ( @{ $m->{'methods'} } ) {
253 print_function_markdown( "Method", $_->{'name'}, $_ );
254
255 print "\nBack to [module description](#$m->{'id'}).\n\n";
256
257 }
258 }
259 }
260
261 if ($m->{'tables'}) {
262 if ( scalar(@{ $m->{'tables'} }) > 0 ) {
263 print "\n## Tables\n\nThe module `$mname` defines the following tables.\n\n";
264
265 foreach ( @{ $m->{'tables'} } ) {
266 print_struct_markdown( "Table", $_->{'name'}, $_ );
267
268 print "\nBack to [module description](#$m->{'id'}).\n\n";
269
270 }
271 }
272 }
273
274 if ($m->{'structs'}) {
275 if ( scalar(@{ $m->{'structs'} }) > 0 ) {
276 print "\n## Structs\n\nThe module `$mname` defines the following structs.\n\n";
277
278 foreach ( @{ $m->{'structs'} } ) {
279 print_struct_markdown( "Struct", $_->{'name'}, $_ );
280
281 print "\nBack to [module description](#$m->{'id'}).\n\n";
282
283 }
284 }
285 }
286
287 print "\nBack to [top](#).\n\n";
288 }
289 }
290
291 # /function make_id
292 sub make_id {
293 my ( $name, $prefix ) = @_;
294
295 if ( !$prefix ) {
296 $prefix = "f";
297 }
298
299 if ( !$options{g} ) {
300
301 # Kramdown/pandoc version of ID's
302 $name =~ /^(\S+).*$/;
303
304 return substr( substr( $prefix, 0, 1 ) . md5_hex($1), 0, 6 );
305 } else {
306 my $input = lc $prefix . "-" . $name;
307 my $id = join '-', split /\s+/, $input;
308
309 $id =~ s/[^\w_-]+//g;
310
311 return $id;
312 }
313 }
314
315 # /function substitute_data_keywords
316 sub substitute_data_keywords {
317 my ($line) = @_;
318
319 if ( $line =~ /^.*\@see\s+(\S+)\s*.*$/ ) {
320 my $name = $1;
321 my $id = make_id($name);
322
323 return $line =~ s/\@see\s+\S+/[`$name`](#$id)/r;
324 }
325
326 return $line;
327 }
328
329 # /function parse_function
330 sub parse_function {
331 my ( $func, @data ) = @_;
332
333 my ( $type, $name ) = ( $func =~ $function_re );
334 chomp $name;
335
336 my $f = {
337 name => $name,
338 data => '',
339 example => undef,
340 example_language => $example_language,
341 id => make_id( $name, $type ),
342 };
343 my $example = 0;
344
345 foreach ( @data ) {
346 if ( /^\s*\@param\s*(?:\{([^}]+)\})?\s*(\S+)\s*(.+)?\s*$/ ) {
347 my $p = {
348 name => $2,
349 type => $1 || "no type",
350 description => $3 || "no description"
351 };
352
353 push @{ $f->{'params'} }, $p;
354 } elsif ( /^\s*\@return\s*(?:\{([^}]+)\})?\s*(.+)?\s*$/ ) {
355 my $r = {
356 type => $1,
357 description => $2 || "no description"
358 };
359
360 push @{ $f->{'returns'} }, $r;
361 } elsif ( /^\s*\@brief\s*(\S.+)$/ ) {
362 $f->{'brief'} = $1;
363 } elsif ( /^\s*\@available\s*(\S.+)$/ ) {
364 $f->{'available'} = $1;
365 }
366 elsif ( /^\s*\@example\s*(\S)?\s*$/ ) {
367 $example = 1;
368 if ( $1 ) {
369 $f->{'example_language'} = $1;
370 }
371 } elsif ( $_ ne $func ) {
372 if ( $example ) {
373 $f->{'example'} .= $_;
374 } else {
375 $f->{'data'} .= substitute_data_keywords($_);
376 }
377 }
378 }
379
380 if ( $f->{'data'} ) {
381 chomp $f->{'data'};
382 } elsif ($f->{'brief'}) {
383 chomp $f->{'brief'};
384 $f->{'data'} = $f->{'brief'};
385 }
386
387 if ( $f->{'example'} ) {
388 chomp $f->{'example'};
389 }
390
391 if ( $f->{'available'} ) {
392 chomp $f->{'available'}
393 }
394
395 if ( !$f->{'brief'} && $f->{'data'} ) {
396
397
398 if ( $f->{'data'} =~ /^(.*?)(?:(?:[.:]\s|$)|\n).*/ ) {
399 $f->{'brief'} = "$1";
400 chomp $f->{'brief'};
401
402 if ( $f->{'brief'} !~ /\.$/) {
403 $f->{'brief'} .= ".";
404 }
405 }
406 }
407
408 if ( $type eq "method" ) {
409 push @{ $cur_module->{'methods'} }, $f;
410 } elsif ( $type eq "function" || $type eq "fn") {
411 push @{ $cur_module->{'functions'} }, $f;
412 }
413 }
414
415 # /function parse_struct
416 sub parse_struct {
417 my ( $func, @data ) = @_;
418
419 my ( $type, $name ) = ( $func =~ $struct_re );
420 chomp $name;
421
422 my $f = {
423 name => $name,
424 data => '',
425 example => undef,
426 example_language => $example_language,
427 id => make_id( $name, $type ),
428 };
429 my $example = 0;
430
431 foreach ( @data ) {
432 if ( /^\s*\@param\s*(?:\{([^}]+)\})?\s*(\S+)\s*(.+)?\s*$/ ) {
433 my $p = {
434 name => $2,
435 type => $1,
436 description => $3
437 };
438
439 push @{ $f->{'params'} }, $p;
440 } elsif ( /^\s*\@brief\s*(\S.+)$/ ) {
441 $f->{'brief'} = $1;
442 } elsif ( /^\s*\@example\s*(\S)?\s*$/ ) {
443 $example = 1;
444 if ( $1 ) {
445 $f->{'example_language'} = $1;
446 }
447 } elsif ( $_ ne $func ) {
448 if ( $example ) {
449 $f->{'example'} .= $_;
450 } else {
451 $f->{'data'} .= substitute_data_keywords($_);
452 }
453 }
454 }
455
456 if ( $f->{'data'} ) {
457 chomp $f->{'data'};
458 } elsif ($f->{'brief'}) {
459 chomp $f->{'brief'};
460 $f->{'data'} = $f->{'brief'};
461 }
462
463 if ( $f->{'example'} ) {
464 chomp $f->{'example'};
465 }
466
467 if ( $type eq "table" ) {
468 push @{ $cur_module->{'tables'} }, $f;
469 } elsif ( $type eq "struct" ) {
470 push @{ $cur_module->{'structs'} }, $f;
471 }
472 }
473
474 # /function parse_module
475 sub parse_module {
476 my ( $module, @data ) = @_;
477 my ( $name ) = ( $module =~ $module_re );
478
479 chomp $name;
480
481 my $f = {
482 name => $name,
483 functions => [],
484 methods => [],
485 data => '',
486 example => undef,
487 example_language => $example_language,
488 id => make_id( $name, "module" ),
489 };
490
491 my $example = 0;
492
493 foreach ( @data ) {
494 if ( /^\s*\@example\s*(\S)?\s*$/ ) {
495 $example = 1;
496 if ($1) {
497 $f->{'example_language'} = $1;
498 }
499 } elsif ( /^\s*\@brief\s*(\S.+)$/ ) {
500 $f->{'brief'} = $1;
501 } elsif ( $_ ne $module ) {
502 if ( $example ) {
503 $f->{'example'} .= $_;
504 } else {
505 $f->{'data'} .= substitute_data_keywords($_);
506 }
507 }
508 }
509
510 if ( $f->{'data'} ) {
511 chomp $f->{'data'};
512 } elsif ( $f->{'brief'} ) {
513 chomp $f->{'brief'};
514
515 $f->{'data'} = $f->{'brief'};
516 }
517
518 if ( $f->{'example'} ) {
519 chomp $f->{'example'};
520 }
521
522 $cur_module = $f;
523 push @modules, $f;
524 }
525
526 # /function parse_content
527 sub parse_content {
528 #
529 my @func = grep /$function_re/, @_;
530 if ( scalar @func > 0 ) {
531 parse_function( $func[0], @_ );
532 }
533
534 #
535 my @struct = grep /$struct_re/, @_;
536 if ( scalar @struct > 0 ) {
537 parse_struct( $struct[0], @_ );
538 }
539
540 #
541 my @module = grep /$module_re/, @_;
542 if ( scalar @module > 0 ) {
543 parse_module( $module[0], @_ );
544 }
545 }
546
547 sub HELP_MESSAGE {
548 print STDERR <<EOF;
549 Utility to convert doxygen comments to markdown.
550
551 usage: $0 [-hg] [-l language] < input_source > markdown.md
552
553 -h : this (help) message
554 -e : sets default example language (default: lua)
555 -l : sets input language (default: c)
556 -g : use github flavoured markdown (default: kramdown/pandoc)
557 EOF
558
559 exit;
560 }
561
562 $Getopt::Std::STANDARD_HELP_VERSION = 1;
563 use Getopt::Std;
564
565 getopts( 'he:gl:', \%options );
566
567 HELP_MESSAGE() if $options{h};
568
569 $example_language = $options{e} if $options{e};
570 $language = $languages{ lc $options{l} } if $options{l};
571
572 if ( !$language ) {
573 $language = $languages{c};
574 }
575
576 ## TODO: select language based on file extension
577 ## TODO: change calling structure to allow looping through directory
578
579 use constant {
580 STATE_READ_SKIP => 0,
581 STATE_READ_CONTENT => 1,
582 STATE_READ_ENUM => 2,
583 STATE_READ_STRUCT => 3,
584 };
585
586 my $state = STATE_READ_SKIP;
587 my $content;
588
589 while ( <> ) {
590 if ( $state == STATE_READ_SKIP ) {
591 if ( $_ =~ $language->{start} ) {
592 $state = STATE_READ_CONTENT;
593
594 if (defined($1)) {
595 chomp($content = $1);
596 $content =~ tr/\r//d;
597 $content .= "\n";
598 } else {
599 $content = "";
600 }
601 }
602 } elsif ( $state == STATE_READ_CONTENT ) {
603 if ( $_ =~ $language->{end} ) {
604 $state = STATE_READ_SKIP;
605
606 parse_content( split /^/, $content );
607
608 $content = "";
609 } else {
610 my ($line) = ( $_ =~ $language->{filter} );
611
612 if ( $line ) {
613 $line =~ tr/\r//d;
614 $content .= $line . "\n";
615 } else {
616 # Preserve empty lines
617 $content .= "\n";
618 }
619 }
620 }
621 }
622
623 #print Dumper( \@modules );
624 print_markdown;