]> mj.ucw.cz Git - libucw.git/blob - build/extract-doc.pl
4b700c14488a3c0bea9c5ef0dc013bf59dd4cbe3
[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         print OUT "..................\n";
30         print OUT "$head\n";
31         print OUT "..................\n\n";
32         print OUT "$comment\n\n";
33 }
34
35 sub process( $ ) {
36         my $file = shift;
37         open FILE, $file or die "Could nod read $file ($!)\n";
38         my $line;
39         my $active;
40         my $verbatim;
41         my $buff;
42         my $head;
43         my $struct;
44         while( defined( $line = <FILE> ) ) {
45                 chomp $line;
46                 if( $struct ) {
47                         $head .= "\n".$line;
48                         if( $line =~ /}/ ) {
49                                 formatNote( $head, $buff );
50                                 $struct = 0;
51                                 $buff = undef;
52                                 $head = undef;
53                         }
54                 } elsif( $verbatim ) {
55                         if( $line =~ /\*\// ) {
56                                 $verbatim = 0;
57                                 print OUT "\n";
58                         } else {
59                                 $line =~ s/^\s*\* ?//;
60                                 print OUT "$line\n";
61                         }
62                 } elsif( $active ) {
63                         if( $line =~ /\*\// ) {
64                                 $active = 0;
65                         } else {
66                                 $line =~ s/^\s*\* ?//;
67                                 $buff .= "$line\n";
68                         }
69                 } else {
70                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
71                                 if( $line =~ /\(/ || $line !~ /{/ ) {
72                                         $_ = $line;
73                                         s/^\s*\s?//;
74                                         s/\/\/.*//;
75                                         s/\/\*.*?\*\///gs;
76                                         s/[;{].*//;
77                                         formatNote( $_, $buff );
78                                         $head = undef;
79                                         $buff = undef;
80                                 } else {
81                                         $head = $line;
82                                         $struct = 1;
83                                 }
84                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
85                                 $buff =~ s/\s?//;
86                                 print OUT "$buff\n\n";
87                                 $buff = undef;
88                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
89                                 $buff =~ s/^\s*//;
90                                 $buff =~ s/\s*$//;
91                                 if( $head =~ /\(/ || $head !~ /{/ ) {
92                                         $head =~ s/^\s*//;
93                                         $head =~ s/\/\*.*?\*\///gs;
94                                         $head =~ s/[;{].*//;
95                                         formatNote( $head, $buff );
96                                         $head = undef;
97                                         $buff = undef;
98                                 } else {
99                                         $struct = 1;
100                                 }
101                         } elsif( $line =~ /\/\*\*\*/ ) {
102                                 $verbatim = 1;
103                         } elsif( $line =~ /\/\*\*/ ) {
104                                 $active = 1;
105                         }
106                 }
107         }
108         close FILE;
109 }
110
111 my $line;
112 while( defined( $line = <IN> ) ) {
113         chomp $line;
114         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
115                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
116                 process( $fname );
117                 print DEP " $fname" if( $hasdep );
118         } else {
119                 print OUT "$line\n";
120         }
121 }
122
123 print DEP "\n" if( $hasdep );
124
125 close IN;
126 close OUT;
127 close DEP;