]> mj.ucw.cz Git - libucw.git/blob - build/extract-doc.pl
Documentation system: generate anchors/ids for functions
[libucw.git] / build / extract-doc.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my( $inname, $outname, $depname, $basedir ) = @ARGV;
6 if( defined $inname ) {
7         open IN, $inname or die "Could not read $inname ($!)\n";
8 } else {
9         open IN, "<&STDIN" or die "Could not read stdin ($!)\n";
10 }
11 if( defined $outname ) {
12         open OUT, ">$outname" or die "Could not write $outname ($!)\n";
13 } else {
14         open OUT, ">&STDOUT" or die "Could not write to stdout ($!)\n";
15 }
16 my $hasdep;
17 if( defined $depname ) {
18         open DEP, ">>$depname" or die "Could not write $depname ($!)\n";
19         $hasdep = 1;
20 }
21
22 print DEP "$outname:" if( $hasdep );
23
24 sub formatNote( $$ ) {
25         my( $head, $comment ) = @_;
26         $head =~ s/(\S)[ ]+/$1 /g;
27         print OUT "\n";
28         print OUT "''''\n";
29         chomp $head;
30         if($head =~ /\w+\([^()]*\)/ && $head !~ /\n/) {
31                 print OUT "!!f!$head!!!\n\n";
32         } else {
33                 print OUT "..................\n";
34                 print OUT "$head\n";
35                 print OUT "..................\n\n";
36         }
37         print OUT "$comment\n\n";
38 }
39
40 sub process( $ ) {
41         my $file = shift;
42         open FILE, $file or die "Could nod read $file ($!)\n";
43         my $line;
44         my $active;
45         my $verbatim;
46         my $buff;
47         my $head;
48         my $struct;
49         while( defined( $line = <FILE> ) ) {
50                 chomp $line;
51                 if( $struct ) {
52                         $head .= "\n".$line;
53                         if( $line =~ /}/ ) {
54                                 formatNote( $head, $buff );
55                                 $struct = 0;
56                                 $buff = undef;
57                                 $head = undef;
58                         }
59                 } elsif( $verbatim ) {
60                         if( $line =~ /\*\// ) {
61                                 $verbatim = 0;
62                                 print OUT "\n";
63                         } else {
64                                 $line =~ s/^\s*\* ?//;
65                                 print OUT "$line\n";
66                         }
67                 } elsif( $active ) {
68                         if( $line =~ /\*\// ) {
69                                 $active = 0;
70                         } else {
71                                 $line =~ s/^\s*\* ?//;
72                                 $buff .= "$line\n";
73                         }
74                 } else {
75                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
76                                 if( $line =~ /\(/ || $line !~ /{/ ) {
77                                         $_ = $line;
78                                         s/^\s*\s?//;
79                                         s/\/\/.*//;
80                                         s/\/\*.*?\*\///gs;
81                                         s/[;{].*//;
82                                         formatNote( $_, $buff );
83                                         $head = undef;
84                                         $buff = undef;
85                                 } else {
86                                         $head = $line;
87                                         $struct = 1;
88                                 }
89                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
90                                 $buff =~ s/\s?//;
91                                 print OUT "$buff\n\n";
92                                 $buff = undef;
93                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
94                                 $buff =~ s/^\s*//;
95                                 $buff =~ s/\s*$//;
96                                 if( $head =~ /\(/ || $head !~ /{/ ) {
97                                         $head =~ s/^\s*//;
98                                         $head =~ s/\/\*.*?\*\///gs;
99                                         $head =~ s/[;{].*//;
100                                         formatNote( $head, $buff );
101                                         $head = undef;
102                                         $buff = undef;
103                                 } else {
104                                         $struct = 1;
105                                 }
106                         } elsif( $line =~ /\/\*\*\*/ ) {
107                                 $verbatim = 1;
108                         } elsif( $line =~ /\/\*\*/ ) {
109                                 $active = 1;
110                         }
111                 }
112         }
113         close FILE;
114 }
115
116 my $line;
117 while( defined( $line = <IN> ) ) {
118         chomp $line;
119         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
120                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
121                 process( $fname );
122                 print DEP " $fname" if( $hasdep );
123         } else {
124                 print OUT "$line\n";
125         }
126 }
127
128 print DEP "\n" if( $hasdep );
129
130 close IN;
131 close OUT;
132 close DEP;