]> mj.ucw.cz Git - libucw.git/blob - build/extract-doc.pl
Documentation system: Structures are input verbatim.
[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/[\t ]+/ /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";
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                         } else {
58                                 $line =~ s/^\s*\* ?//;
59                                 print OUT "$line\n";
60                         }
61                 } elsif( $active ) {
62                         if( $line =~ /\*\// ) {
63                                 $active = 0;
64                         } else {
65                                 $line =~ s/^\s*\* ?//;
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( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
85                                 $buff =~ s/^\s*//;
86                                 $buff =~ s/\s*$//;
87                                 if( $head =~ /\(/ || $head !~ /{/ ) {
88                                         $head =~ s/^\s*//;
89                                         $head =~ s/\/\*.*?\*\///gs;
90                                         $head =~ s/([;{]).*/$1/;
91                                         formatNote( $head, $buff );
92                                         $head = undef;
93                                         $buff = undef;
94                                 } else {
95                                         $struct = 1;
96                                 }
97                         } elsif( $line =~ /\/\*\*\*/ ) {
98                                 $verbatim = 1;
99                         } elsif( $line =~ /\/\*\*/ ) {
100                                 $active = 1;
101                         }
102                 }
103         }
104         close FILE;
105 }
106
107 my $line;
108 while( defined( $line = <IN> ) ) {
109         chomp $line;
110         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
111                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
112                 process( $fname );
113                 print DEP " $fname" if( $hasdep );
114         } else {
115                 print OUT "$line\n";
116         }
117 }
118
119 print DEP "\n" if( $hasdep );
120
121 close IN;
122 close OUT;
123 close DEP;