Debian Codegraph @

example/xmllibxmldocs.pl

raw
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use XML::LibXML;
6 use IO::File;
7
8 # ------------------------------------------------------------------------- #
9 # (c) 2003 christian p. glahn
10 # ------------------------------------------------------------------------- #
11
12 # ------------------------------------------------------------------------- #
13 # This is an example how to use the DOM interface of XML::LibXML The
14 # script reads a XML File with a module specification. If the module
15 # contains several classes, the script fetches them and stores the
16 # data into different POD Files.
17 #
18 # Note this is just an example, to demonstrate how XML::LibXML works.
19 # The code works for the XML::LibXML documentation, but may not work
20 # for any other docbook file.
21 #
22 # If you are interested what the results are, check the README and the POD
23 # files shipped with XML::LibXML.
24 # ------------------------------------------------------------------------- #
25
26 # ------------------------------------------------------------------------- #
27 # SYNOPSIS:
28 # xmllibxmldocs.pl $dokbook_file $targetdir
29 #
30 my $srcfile = shift @ARGV;
31 my $targetdir = shift @ARGV;
32
33 unless ( $targetdir =~ /\/$/ ) {
34 $targetdir .= "/";
35 }
36
37 # ------------------------------------------------------------------------- #
38 #
39 # ------------------------------------------------------------------------- #
40 # init the parser
41 my $parser = XML::LibXML->new();
42 $parser->load_ext_dtd(0);
43 $parser->keep_blanks(0);
44 # ------------------------------------------------------------------------- #
45 #
46 # ------------------------------------------------------------------------- #
47 # load the document into memory.
48 my $doc = $parser->parse_file( $srcfile );
49 # ------------------------------------------------------------------------- #
50 #
51 # ------------------------------------------------------------------------- #
52 # good implementations would use XSLT to convert a docbook to any other
53 # text format. Since the module does not presume libxslt installed, we
54 # have to do the dirty job.
55 my $ch = ChapterHandler->new($targetdir);
56
57 # ------------------------------------------------------------------------- #
58 # init the common parts in all pods
59 my ( $bookinfo ) = $doc->findnodes( "//bookinfo" );
60 $ch->set_general_info( $bookinfo );
61 # ------------------------------------------------------------------------- #
62
63 # ------------------------------------------------------------------------- #
64 # then process each chapter of the XML::LibXML book
65 my @chapters = $doc->findnodes( "//chapter" );
66 foreach my $chap ( @chapters ) {
67 $ch->handle( $chap );
68 }
69 # ------------------------------------------------------------------------- #
70 # ------------------------------------------------------------------------- #
71
72 # ------------------------------------------------------------------------- #
73 # the class to process our dokbook file
74 # ------------------------------------------------------------------------- #
75 package ChapterHandler;
76 use XML::LibXML;
77
78 # ------------------------------------------------------------------------- #
79 # the constructor
80 # ------------------------------------------------------------------------- #
81 sub new{
82 my $class = shift;
83 my $dir = shift;
84 my $self = bless {directory => $dir}, $class;
85
86 return $self;
87 }
88 # ------------------------------------------------------------------------- #
89
90 # ------------------------------------------------------------------------- #
91 # set_general_info
92 # ------------------------------------------------------------------------- #
93 # processes the bookinfo tag of XML::LibXML to extract common information such
94 # as version or copyright information
95 sub set_general_info {
96 my $self = shift;
97 my $infonode = shift;
98 return unless defined $infonode;
99
100 my $infostr = "=head1 AUTHORS\n\n";
101 my @authors = $infonode->findnodes( "authorgroup/author" );
102 foreach my $author ( @authors ) {
103 my ( $node_fn ) = $author->getChildrenByTagName( "firstname" );
104 my ( $node_sn ) = $author->getChildrenByTagName( "surname" );
105 if ( defined $node_fn ) {
106 $infostr .= $node_fn->string_value();
107 }
108 if ( defined $node_sn ) {
109 $infostr .= " ". $node_sn->string_value();
110 }
111 if ( defined $author->nextSibling() ) {
112 $infostr .= ", \n";
113 }
114 else {
115 $infostr .= "\n\n";
116 }
117 }
118
119 my ( $version ) = $infonode->findnodes( "edition" );
120 if ( defined $version ) {
121 $infostr .= "\n=head1 VERSION\n\n" . $version->string_value() . "\n\n";
122 }
123
124 my @copyright = $infonode->findnodes( "copyright" );
125 if ( @copyright ) {
126 $infostr .= "=head1 COPYRIGHT\n\n";
127 foreach my $copyright (@copyright) {
128 my $node_y = $copyright->getChildrenByTagName( "year" );
129 my $node_h = $copyright->getChildrenByTagName( "holder" );
130 if ( defined $node_y ) {
131 $infostr .= $node_y->string_value() . ", ";
132 }
133 if ( defined $node_h ) {
134 $infostr .= $node_h->string_value();
135 }
136 $infostr .= ".\n\n";
137 }
138 $infostr .= "=cut\n";
139
140 $infostr .= "\n\n".<<'EOF';
141 =head1 LICENSE
142
143 This program is free software; you can redistribute it and/or modify it under
144 the same terms as Perl itself.
145
146 EOF
147 }
148
149 $self->{infoblock} = $infostr;
150 }
151
152 # ------------------------------------------------------------------------- #
153 # handle
154 # ------------------------------------------------------------------------- #
155 # This function opens the output file and decides how the chapter is
156 # processed
157 sub handle {
158 my $self = shift;
159 my $chapter = shift;
160
161 my ( $abbr ) = $chapter->findnodes( "titleabbrev" );
162 if ( defined $abbr ) {
163 # create a new file.
164 my $filename = $abbr->string_value();
165 $filename =~ s/^\s*|\s*$//g;
166 my $dir = $self->{directory};
167
168 $filename =~ s/XML\:\:LibXML//g;
169 $filename =~ s/^-|^\:\://g; # remove the first colon or minus.
170 $filename =~ s/\:\:/\//g; # transform remaining colons to paths.
171 # the previous statement should work for existing modules. This could be
172 # dangerous for nested modules, which do not exist at the time of writing
173 # this code.
174
175 unless ( length $filename ) {
176 $dir = "";
177 $filename = "LibXML";
178 }
179
180 if ( $filename ne "README" and $filename ne "LICENSE" ) {
181 $filename .= ".pod";
182 }
183 else {
184 $dir = "";
185 }
186
187 $self->{OFILE} = IO::File->new();
188 $self->{OFILE}->open(">".$dir.$filename);
189
190 if ( $abbr->string_value() eq "README"
191 or $abbr->string_value() eq "LICENSE" ) {
192
193 # Text only chapters in the documentation
194 $self->dump_text( $chapter );
195 }
196 else {
197 # print header
198 # print synopsis
199 # process the information itself
200 # dump the info block
201 $self->dump_pod( $chapter );
202 $self->{OFILE}->print( $self->{infoblock} );
203 }
204 # close the file
205 $self->{OFILE}->close();
206
207 # Strip trailing space.
208 my $text = _slurp($dir.$filename);
209 $text =~ s/[ \t]+$//gms;
210
211 open my $out, '>', $dir.$filename
212 or die "Cannot open $dir$filename for writing.";
213 print {$out} $text;
214 close ($out);
215
216 }
217 }
218
219 sub _slurp
220 {
221 my $filename = shift;
222
223 open my $in, '<', $filename
224 or die "Cannot open '$filename' for slurping - $!";
225
226 local $/;
227 my $contents = <$in>;
228
229 close($in);
230
231 return $contents;
232 }
233
234 # ------------------------------------------------------------------------- #
235 # dump_text
236 # ------------------------------------------------------------------------- #
237 # convert the chapter into a textfile, such as README.
238 sub dump_text {
239 my $self = shift;
240 my $chap = shift;
241
242 if ( $chap->nodeName() eq "chapter" ) {
243 my ( $title ) = $chap->getChildrenByTagName( "title" );
244 my $str = $title->string_value();
245 my $len = length $str;
246 $self->{OFILE}->print( uc($str) . "\n" );
247 $self->{OFILE}->print( "=" x $len );
248 $self->{OFILE}->print( "\n\n" );
249 }
250
251 foreach my $node ( $chap->childNodes() ) {
252 if ( $node->nodeName() eq "para" ) {
253 # we split at the last whitespace before 80 chars
254 my $string = $node->string_value();
255 my $os = "";
256 my @words = split /\s+/, $string;
257 foreach my $word ( @words ) {
258 if ( (length( $os ) + length( $word ) + 1) < 80 ) {
259 if ( length $os ) { $os .= " "; }
260 $os .= $word;
261 }
262 else {
263 $self->{OFILE}->print( $os . "\n" );
264 $os = $word;
265 }
266 }
267 $self->{OFILE}->print( $os );
268 $self->{OFILE}->print( "\n\n" );
269 }
270 elsif ( $node->nodeName() eq "sect1" ) {
271 my ( $title ) = $node->getChildrenByTagName( "title" );
272 my $str = $title->string_value();
273 my $len = length $str;
274
275 $self->{OFILE}->print( "\n" . uc($str) . "\n" );
276 $self->{OFILE}->print( "=" x $len );
277 $self->{OFILE}->print( "\n\n" );
278 $self->dump_text( $node );
279 }
280 elsif ( $node->nodeName() eq "sect2" ) {
281 my ( $title ) = $node->getChildrenByTagName( "title" );
282 my $str = $title->string_value();
283 my $len = length $str;
284
285 $self->{OFILE}->print( "\n" . $str . "\n" );
286 $self->{OFILE}->print( "=" x $len );
287 $self->{OFILE}->print( "\n\n" );
288 $self->dump_text( $node );
289 }
290 elsif ( $node->nodeName() eq "itemizedlist" ) {
291 my @items = $node->findnodes( "listitem" );
292 my $sp= " ";
293 foreach my $item ( @items ) {
294 $self->{OFILE}->print( "$sp o " );
295 my $str = $item->string_value();
296 $str =~ s/^\s*|\s*$//g;
297 $self->{OFILE}->print( $str );
298 $self->{OFILE}->print( "\n" );
299 }
300 $self->{OFILE}->print( "\n" );
301 }
302 elsif ( $node->nodeName() eq "orderedlist" ) {
303 my @items = $node->findnodes( "listitem" );
304 my $i = 0;
305 my $sp= " ";
306 foreach my $item ( @items ) {
307 $i++;
308 $self->{OFILE}->print( "$sp $i " );
309 my $str = $item->string_value();
310 $str =~ s/^\s*|\s*$//g;
311 $self->{OFILE}->print( $str );
312 $self->{OFILE}->print( "\n" );
313 }
314 $self->{OFILE}->print( "\n" );
315 }
316 elsif ( $node->nodeName() eq "programlisting" ) {
317 my $str = $node->string_value();
318 $str =~ s/\n/\n> /g;
319 $self->{OFILE}->print( "> ". $str );
320 $self->{OFILE}->print( "\n\n" );
321 }
322 }
323 }
324
325 # ------------------------------------------------------------------------- #
326 # dump_pod
327 # ------------------------------------------------------------------------- #
328 # This method is used to create the real POD files for XML::LibXML. It is not
329 # too sophisticated, but it already does quite a good job.
330 sub dump_pod {
331 my $self = shift;
332 my $chap = shift;
333
334 if ( $chap->nodeName() eq "chapter" ) {
335 my ( $title ) = $chap->getChildrenByTagName( "title" );
336 my ( $ttlabbr ) = $chap->getChildrenByTagName( "titleabbrev" );
337 my $str = $ttlabbr->string_value() . " - ".$title->string_value();
338 $str=~s/^\s+|\s+$//g;
339 $self->{OFILE}->print( "=head1 NAME\n\n$str\n" );
340 my ($synopsis) = $chap->findnodes( "sect1[title='Synopsis']" );
341 my @funcs = $chap->findnodes( ".//funcsynopsis" );
342 if ($synopsis or scalar @funcs) {
343 $self->{OFILE}->print( "\n=head1 SYNOPSIS\n\n" )
344 }
345 if ($synopsis) {
346 $self->dump_pod( $synopsis );
347 }
348 if ( scalar @funcs ) {
349 foreach my $s ( @funcs ) {
350 $self->dump_pod( $s );
351 }
352 # $self->{OFILE}->print( "\n\n=head1 DESCRIPTION\n\n" );
353 }
354 }
355
356 foreach my $node ( $chap->childNodes() ) {
357 if ( $node->nodeType == XML_TEXT_NODE ||
358 $node->nodeType == XML_CDATA_SECTION_NODE ) {
359 # we split at the last whitespace before 80 chars
360 my $prev_inline =
361 ($node->previousSibling and
362 $node->previousSibling->nodeName !~
363 /^(?:itemizedlist|orderedlist|variablelist|programlisting|funcsynopsis)/)
364 ? 1 : 0;
365 my $str = $node->data();
366 $str=~s/(^|\n)[ \t]+($|\n)/$1$2/g;
367 if ($str=~/\S/) {
368 my $string = $str;
369 my $space_before = ($string =~ s/^\s+//g) ? $prev_inline : 0;
370 my $space_after = ($string =~ s/\s+$//g) ? 1 : 0;
371 $self->{OFILE}->print( " " ) if $space_before;
372 my $os = "";
373 my @words = split /\s+/, $string;
374 foreach my $word ( @words ) {
375 if ( (length( $os ) + length( $word ) + 1) < 80 ) {
376 if ( length $os ) { $os .= " "; }
377 $os .= $word;
378 }
379 else {
380 $self->{OFILE}->print( $os . "\n" );
381 $os = $word;
382 }
383 }
384 $os.=" " if $space_after;
385 $self->{OFILE}->print( $os );
386 }
387 } elsif ( $node->nodeName() eq "para" ) {
388 $self->dump_pod( $node );
389 $self->{OFILE}->print( "\n\n" );
390 } elsif ( $node->nodeName() eq "sect1" ) {
391 my ( $title ) = $node->getChildrenByTagName( "title" );
392 my $str = $title->string_value();
393 unless ($chap->nodeName eq "chapter" and $str eq 'Synopsis') {
394 $self->{OFILE}->print( "\n=head1 " . uc($str) );
395 $self->{OFILE}->print( "\n\n" );
396 $self->dump_pod( $node );
397 }
398 }
399 elsif ( $node->nodeName() eq "sect2" ) {
400 my ( $title ) = $node->getChildrenByTagName( "title" );
401 my $str = $title->string_value();
402 my $len = length $str;
403
404 $self->{OFILE}->print( "\n=head2 " . $str . "\n\n" );
405
406 $self->dump_pod( $node );
407 }
408 elsif ( $node->nodeName() eq "sect3" ) {
409 my ( $title ) = $node->getChildrenByTagName( "title" );
410 my $str = $title->string_value();
411 my $len = length $str;
412
413 $self->{OFILE}->print( "\n=head3 " . $str . "\n\n" );
414
415 $self->dump_pod( $node );
416 }
417 elsif ( $node->nodeName() eq "itemizedlist" ) {
418 my @items = $node->findnodes( "listitem" );
419 $self->{OFILE}->print( "\n=over 4\n\n" );
420 foreach my $item ( @items ) {
421 $self->{OFILE}->print( "=item *\n\n" );
422 $self->dump_pod( $item );
423 $self->{OFILE}->print( "\n\n" );
424 }
425 $self->{OFILE}->print( "=back\n\n" );
426 }
427 elsif ( $node->nodeName() eq "orderedlist" ) {
428 my @items = $node->findnodes( "listitem" );
429 my $i = 0;
430 $self->{OFILE}->print( "\n=over 4\n\n" );
431
432 foreach my $item ( @items ) {
433 $i++;
434 $self->{OFILE}->print( "=item $i.\n\n" );
435 $self->dump_pod($item);
436 $self->{OFILE}->print( "\n\n" );
437 }
438 $self->{OFILE}->print( "=back\n\n" );
439 }
440 elsif ( $node->nodeName() eq "variablelist" ) {
441 $self->{OFILE}->print( "=over 4\n\n" );
442 my @nodes = $node->findnodes( "varlistentry" );
443 $self->dump_pod( $node );
444 $self->{OFILE}->print( "\n=back\n\n" );
445 }
446 elsif ( $node->nodeName() eq "varlistentry" ) {
447 my ( $term ) = $node->findnodes( "term" );
448 $self->{OFILE}->print( "=item " );
449 if ( defined $term ) {
450 $self->dump_pod( $term );
451 }
452 $self->{OFILE}->print( "\n\n" );
453 my @nodes =$node->findnodes( "listitem" );
454 foreach my $it ( @nodes ) {
455 $self->dump_pod( $it );
456 }
457 $self->{OFILE}->print( "\n" );
458 }
459 elsif ( $node->nodeName() eq "programlisting" ) {
460 my $str = $node->string_value();
461 $str =~ s/^\s+|\s+$//g;
462 $str =~ s/\n/\n /g;
463 $str=~s/(^|\n)[ \t]+($|\n)/$1$2/g;
464 $self->{OFILE}->print( "\n\n" );
465 $self->{OFILE}->print( " ". $str );
466 $self->{OFILE}->print( "\n\n" );
467 }
468 elsif ( $node->nodeName() eq "funcsynopsis") {
469 if (($node->getAttribute('role')||'') ne 'synopsis') {
470 $self->dump_pod($node);
471 $self->{OFILE}->print( "\n" );
472 }
473 }
474 elsif( $node->nodeName() eq "funcsynopsisinfo" ) {
475 my $str = $node->string_value() ;
476 $str =~ s/\n/\n /g;
477 $self->{OFILE}->print( " $str\n" );
478 } elsif( $node->nodeName() eq "title" or
479 $node->nodeName() eq "titleabbrev"
480 ) {
481 # IGNORE
482 } elsif( $node->nodeName() eq "emphasis" ) {
483 my $str = $node->string_value() ;
484 $str =~ s/\n/ /g;
485 $str = pod_escape($str);
486 $self->{OFILE}->print( "I<<<<<< $str >>>>>>" );
487 } elsif( $node->nodeName() eq "function" or
488 $node->nodeName() eq "email" or
489 $node->nodeName() eq "literal"
490 ) {
491 my $str = $node->string_value() ;
492 $str =~ s/\n/ /g;
493 $str = pod_escape($str);
494 $self->{OFILE}->print( "C<<<<<< $str >>>>>>" );
495 } elsif( $node->nodeName() eq "ulink" ) {
496 my $str = $node->string_value() ;
497 my $url = $node->getAttribute('url');
498 $str =~ s/\n/ /g;
499 if ($str eq $url) {
500 $self->{OFILE}->print( "L<<<<<< $url >>>>>>" );
501 } else {
502 $self->{OFILE}->print( "$str (L<<<<<< $url >>>>>>)" );
503 }
504 } elsif( $node->nodeName() eq "xref" ) {
505 my $linkend = $node->getAttribute('linkend');
506 my ($target) = $node->findnodes(qq(//*[\@id="$linkend"]/titleabbrev));
507 ($target) = $node->findnodes(qq(//*[\@id="$linkend"]/title)) unless $target;
508 if ($target) {
509 my $str = $target->string_value() ;
510 $str =~ s/\n/ /g;
511 $str = pod_escape($str);
512 $self->{OFILE}->print( "L<<<<<< $str >>>>>>" );
513 } else {
514 warn "WARNING: Didn't find any section with id='$linkend'\n";
515 $self->{OFILE}->print( "$linkend" );
516 }
517 } elsif( $node->nodeName() eq "olink" ) {
518 my $str = pod_escape($node->string_value());
519 my $url = $node->getAttribute('targetdoc');
520 if (!defined $url) {
521 warn $node->toString(1),"\n";
522 }
523 $str =~ s/\n/ /g;
524 if ($str eq $url) {
525 $self->{OFILE}->print( "L<<<<<< $url >>>>>>" );
526 } else {
527 $self->{OFILE}->print( "$str (L<<<<<< $url >>>>>>)" );
528 }
529 } else {
530 print STDERR "Ignoring ",$node->nodeName(),"\n";
531 $self->dump_pod($node);
532 }
533 }
534 }
535
536 sub pod_escape {
537 my ($str) = @_;
538
539 my %escapes = (
540 '>' => 'gt',
541 '<' => 'lt',
542 );
543
544 my $re = join('|', keys %escapes);
545
546 $str =~ s/($re)/E<$escapes{$1}>/g;
547
548 return $str;
549 }
550
551 1;