]> mj.ucw.cz Git - libucw.git/blob - build/extract-doc.pl
Created documentation system.
[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 process( $ ) {
25         my $file = shift;
26         open FILE, $file or die "Could nod read $file ($!)\n";
27         my $line;
28         my $active;
29         my $verbatim;
30         my $buff;
31         my $head;
32         while( defined( $line = <FILE> ) ) {
33                 chomp $line;
34                 if( $verbatim ) {
35                         if( $line =~ /\*\// ) {
36                                 $verbatim = 0;
37                         } else {
38                                 $line =~ s/^\s*\* ?//;
39                                 print OUT "$line\n";
40                         }
41                 } elsif( $active ) {
42                         if( $line =~ /END\*\// ) {
43                                 print OUT "$buff\n";
44                         } elsif( $line =~ /\*\// ) {
45                                 $active = 0;
46                         } else {
47                                 $line =~ s/^\s*\* ?//;
48                                 $line =~ s/^\s*$/+/;
49                                 $buff .= "$line\n";
50                         }
51                 } else {
52                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
53                                 chomp $line;
54                                 $line =~ s/^\s*//;
55                                 $line =~ s/\/\/.*//;
56                                 $head .= "\n$line";
57                                 if( $head =~ /;/ ) {
58                                         $head =~ s/\/\*.*?\*\///gs;
59                                         $head =~ s/\s+/ /g;
60                                         $head =~ s/;.*/;/;
61                                         print OUT "- + +++$head+++ +\n+\n$buff\n";
62                                         $head = undef;
63                                         $buff = undef;
64                                 }
65                         } elsif( $line =~ /\/\*VERBATIM/ ) {
66                                 $verbatim = 1;
67                         } elsif( $line =~ /\/\*DOC/ ) {
68                                 $active = 1;
69                         }
70                 }
71         }
72         close FILE;
73 }
74
75 my $line;
76 while( defined( $line = <IN> ) ) {
77         chomp $line;
78         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
79                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
80                 process( $fname );
81                 print DEP " $fname" if( $hasdep );
82         } else {
83                 print OUT "$line\n";
84         }
85 }
86
87 print DEP "\n" if( $hasdep );
88
89 close IN;
90 close OUT;
91 close DEP;