]> mj.ucw.cz Git - libucw.git/blob - build/extract-doc.pl
Enhanced 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         my $levelMark = '-';
33         my $markDepth;
34         while( defined( $line = <FILE> ) ) {
35                 chomp $line;
36                 if( $verbatim ) {
37                         if( $line =~ /\*\// ) {
38                                 $verbatim = 0;
39                         } else {
40                                 $line =~ s/^\s*\* ?//;
41                                 print OUT "$line\n";
42                         }
43                 } elsif( $active ) {
44                         if( $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/([;{]).*/$1/;
61                                         print OUT $levelMark." + +++$head+++ +\n+\n$buff\n";
62                                         if( $head =~ /\{/ ) {
63                                                 $levelMark = '*' unless( $markDepth ++ );
64                                         }
65                                         $head = undef;
66                                         $buff = undef;
67                                 }
68                         } elsif( my( $head, $comment ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
69                                 $head =~ s/^\s*//;
70                                 $head =~ s/\/\*.*?\*\///gs;
71                                 $head =~ s/\s+/ /g;
72                                 $head =~ s/([;{]).*/$1/;
73                                 $comment =~ s/^\s*//;
74                                 $comment =~ s/\s*$//;
75                                 print OUT $levelMark." + +++$head+++ +\n+\n$comment\n\n";
76                                 if( $head =~ /\{/ ) {
77                                         $levelMark = '*' unless( $markDepth ++ );
78                                 }
79                         } elsif( $line =~ /\}/ && $markDepth ) {
80                                 $levelMark = '-' unless( -- $markDepth );
81                         } elsif( $line =~ /\/\*\*\*/ ) {
82                                 $verbatim = 1;
83                         } elsif( $line =~ /\/\*\*/ ) {
84                                 $active = 1;
85                         }
86                 }
87         }
88         close FILE;
89 }
90
91 my $line;
92 while( defined( $line = <IN> ) ) {
93         chomp $line;
94         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
95                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
96                 process( $fname );
97                 print DEP " $fname" if( $hasdep );
98         } else {
99                 print OUT "$line\n";
100         }
101 }
102
103 print DEP "\n" if( $hasdep );
104
105 close IN;
106 close OUT;
107 close DEP;