* Add a patch from Davor Ocelic.
[kdebindings.git] / kalyptus / kdocLib.pm
blob6eac4dff53c384026a64392346ca2460d0784507
2 =head1 kdocLib
4 Writes out a library file.
6 NOTES ON THE NEW FORMAT
8 Stores: class name, members, hierarchy
9 node types are not stored
12 File Format Spec
13 ----------------
15 header
16 zero or more members, each of
17 method
18 member
19 class, each of
20 inheritance
21 zero or more members
25 Unrecognized lines ignored.
27 Sample
28 ------
30 <! KDOC Library HTML Reference File>
31 <VERSION="2.0">
32 <BASE URL="http://www.kde.org/API/kdecore/">
34 <C NAME="KApplication" REF="KApplication.html">
35 <IN NAME="QObject">
36 <ME NAME="getConfig" REF="KApplication.html#getConfig">
37 <M NAME="" REF="">
38 </C>
40 =cut
42 package kdocLib;
43 use strict;
45 use Carp;
46 use File::Path;
47 use File::Basename;
49 use Ast;
50 use kdocAstUtil;
51 use kdocUtil;
54 use vars qw/ $exe $lib $root $plang $outputdir $docpath $url $compress /;
56 BEGIN {
57 $exe = basename $0;
60 sub writeDoc
62 ( $lib, $root, $plang, $outputdir, $docpath, $url,
63 $compress ) = @_;
64 my $outfile = "$outputdir/$lib.kalyptus";
65 $url = $docpath unless defined $url;
67 mkpath( $outputdir ) unless -f $outputdir;
69 if( $compress ) {
70 open( LIB, "| gzip -9 > \"$outfile.gz\"" )
71 || die "$exe: couldn't write to $outfile.gz\n";
74 else {
75 open( LIB, ">$outfile" )
76 || die "$exe: couldn't write to $outfile\n";
79 my $libdesc = "";
80 if ( defined $root->{LibDoc} ) {
81 $libdesc="<LIBDESC>".$root->{LibDoc}->{astNodeName}."</LIBDESC>";
84 print LIB<<LTEXT;
85 <! KDOC Library HTML Reference File>
86 <VERSION="$main::Version">
87 <BASE URL="$url">
88 <PLANG="$plang">
89 <LIBNAME>$lib</LIBNAME>
90 $libdesc
92 LTEXT
94 writeNode( $root, "" );
95 close LIB;
98 sub writeNode
100 my ( $n, $prefix ) = @_;
101 return if !exists $n->{Compound};
102 return if exists $n->{Forward} && !exists $n->{KidAccess};
104 if( $n != $root ) {
105 $prefix .= $n->{astNodeName};
106 print LIB "<C NAME=\"", $n->{astNodeName},
107 "\" REF=\"$prefix.html\">\n";
109 else {
110 print LIB "<STATS>\n";
111 my $stats = $root->{Stats};
112 foreach my $stat ( keys %$stats ) {
113 print LIB "<STAT NAME=\"$stat\">",
114 $stats->{$stat},"</STAT>\n";
116 print LIB "</STATS>\n";
119 if( exists $n->{Ancestors} ) {
120 my $in;
121 foreach $in ( @{$n->{Ancestors}} ) {
122 $in =~ s/\s+//g;
123 print LIB "<IN NAME=\"",$in,"\">\n";
127 return if !exists $n->{Kids};
128 my $kid;
129 my $type;
131 foreach $kid ( @{$n->{Kids}} ) {
132 next if exists $kid->{ExtSource}
133 || $kid->{Access} eq "private";
135 if ( exists $kid->{Compound} ) {
136 if( $n != $root ) {
137 writeNode( $kid, $prefix."::" );
139 else {
140 writeNode( $kid, "" );
142 next;
145 $type = $kid->{NodeType} eq "method" ?
146 "ME" : "M";
148 print LIB "<$type NAME=\"", $kid->{astNodeName},
149 "\" REF=\"$prefix.html#", $kid->{astNodeName}, "\">\n";
152 if( $n != $root ) {
153 print LIB "</C>\n";
157 sub readLibrary
159 my( $rootsub, $name, $path, $relurl ) = @_;
160 $path = "." unless defined $path;
161 my $real = $path."/".$name.".kalyptus";
162 my $url = ".";
163 my @stack = ();
164 my $version = "2.0";
165 my $new;
166 my $root = undef;
167 my $n = undef;
168 my $havecomp = -r "$real.gz";
169 my $haveuncomp = -r "$real";
171 if ( $haveuncomp ) {
172 open( LIB, "$real" ) || die "Can't read lib $real\n";
175 if( $havecomp ) {
176 if ( $haveuncomp ) {
177 warn "$exe: two libs exist: $real and $real.gz. "
178 ."Using $real\n";
180 else {
181 open( LIB, "gunzip < \"$real.gz\"|" )
182 || die "Can't read pipe gunzip < \"$real.gz\": $?\n";
186 while( <LIB> ) {
187 next if /^\s*$/;
188 if ( !/^\s*</ ) {
189 close LIB;
190 #readOldLibrary( $root, $name, $path );
191 return;
194 if( /<VER\w+\s+([\d\.]+)>/ ) {
195 # TODO: what do we do with the version number?
196 $version = $1;
198 elsif ( /<BASE\s*URL\s*=\s*"(.*?)"/ ) {
199 $url = $1;
200 $url .= "/" unless $url =~ m:/$:;
202 my $test = kdocUtil::makeRelativePath( $relurl, $url );
203 $url = $test;
205 elsif( /<PLANG\s*=\s*"(.*?)">/ ) {
206 $root = $rootsub->( $1 );
207 $n = $root;
209 elsif ( /<C\s*NAME="(.*?)"\s*REF="(.*?)"\s*>/ ) {
210 # class
211 $new = Ast::New( $1 );
212 $new->AddProp( "NodeType", "class" );
213 $new->AddProp( "Compound", 1 );
214 $new->AddProp( "ExtSource", $name );
216 # already escaped at this point!
217 $new->AddProp( "Ref", $url.$2 );
219 $root = $n = $rootsub->( "CXX" ) unless defined $root;
220 kdocAstUtil::attachChild( $n, $new );
221 push @stack, $n;
222 $n = $new;
224 elsif ( m#<IN\s*NAME\s*=\s*"(.*?)"\s*># ) {
225 # ancestor
226 kdocAstUtil::newInherit( $n, $1 );
228 elsif ( m#</C># ) {
229 # end class
230 $n = pop @stack;
232 elsif ( m#<(M\w*)\s+NAME="(.*?)"\s+REF="(.*?)"\s*># ) {
233 # member
234 $new = Ast::New( $2 );
235 $new->AddProp( "NodeType", $1 eq "ME" ? "method" : "var" );
236 $new->AddProp( "ExtSource", $name );
237 $new->AddProp( "Flags", "" );
238 $new->AddProp( "Ref", $url.$3 );
240 kdocAstUtil::attachChild( $n, $new );