| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Getopt::Std; | ||
| 7 | use Pod::Usage; | ||
| 8 | use IO::File; | ||
| 9 | use XML::SAX::Expat; | ||
| 10 | use XML::SAX::Writer; | ||
| 11 | use XML::Handler::Dtd2Html; | ||
| 12 | |||
| 13 | my %opts; | ||
| 14 | getopts('bCDdfHhMvl:p:s:t:o:x:Z', \%opts) | ||
| 15 | or pod2usage(-verbose => 0); | ||
| 16 | |||
| 17 | if ($opts{h}) { | ||
| 18 | pod2usage(-verbose => 0); | ||
| 19 | } | ||
| 20 | |||
| 21 | if ($opts{v}) { | ||
| 22 | print "$0\n"; | ||
| 23 | print "XML::Handler::Dtd2Html Version $XML::Handler::Dtd2Html::VERSION\n"; | ||
| 24 | exit(0); | ||
| 25 | } | ||
| 26 | |||
| 27 | my $file = $ARGV[0]; | ||
| 28 | die "No input file\n" | ||
| 29 | unless (defined $file); | ||
| 30 | warn "Don't use directly a DTD file (see the embedded pod or the README).\n" | ||
| 31 | if ($file =~ /\.dtd$/i); | ||
| 32 | my $io = IO::File->new($file,'r'); | ||
| 33 | die "Can't open $file ($!)\n" | ||
| 34 | unless (defined $io); | ||
| 35 | |||
| 36 | if (exists $opts{d}) { | ||
| 37 | if (exists $opts{o}) { | ||
| 38 | my $outfile = $opts{o}; | ||
| 39 | open STDOUT, '>', $outfile | ||
| 40 | or die "can't open $outfile ($!).\n"; | ||
| 41 | } | ||
| 42 | my $handler = XML::SAX::Writer->new(Writer => 'DtdWriter', Output => \*STDOUT); | ||
| 43 | my $parser = XML::SAX::Expat->new(Handler => $handler); | ||
| 44 | $parser->set_feature('http://xml.org/sax/features/external-general-entities', 1); | ||
| 45 | $parser->parse(Source => {ByteStream => $io}); | ||
| 46 | } else { | ||
| 47 | my $handler = XML::Handler::Dtd2Html->new(); | ||
| 48 | my $parser = XML::SAX::Expat->new(Handler => $handler); | ||
| 49 | $parser->set_feature('http://xml.org/sax/features/external-general-entities', 1); | ||
| 50 | my $doc = $parser->parse(Source => {ByteStream => $io}); | ||
| 51 | |||
| 52 | my $outfile; | ||
| 53 | if (exists $opts{o}) { | ||
| 54 | $outfile = $opts{o}; | ||
| 55 | } else { | ||
| 56 | my $root = $doc->{root_name}; | ||
| 57 | $root =~ s/[:\.\-]/_/g; | ||
| 58 | $outfile = 'dtd_' . $root; | ||
| 59 | } | ||
| 60 | |||
| 61 | my @examples = (); | ||
| 62 | @examples = split /\s+/, $opts{x} if (exists $opts{x}); | ||
| 63 | |||
| 64 | if ($opts{b}) { | ||
| 65 | bless($doc, 'XML::Handler::Dtd2Html::DocumentBook'); | ||
| 66 | } elsif ($opts{f}) { | ||
| 67 | bless($doc, 'XML::Handler::Dtd2Html::DocumentFrame'); | ||
| 68 | } | ||
| 69 | |||
| 70 | $doc->GenerateHTML( | ||
| 71 | outfile => $outfile, | ||
| 72 | title => $opts{t}, | ||
| 73 | css => $opts{s}, | ||
| 74 | examples => \@examples, | ||
| 75 | flag_comment => !exists($opts{C}), | ||
| 76 | flag_date => !exists($opts{D}), | ||
| 77 | flag_href => exists($opts{H}), | ||
| 78 | flag_multi => exists($opts{M}), | ||
| 79 | flag_zombi => exists($opts{Z}), | ||
| 80 | language => $opts{l}, | ||
| 81 | path_tmpl => $opts{p} | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | |||
| 85 | package DtdWriter; | ||
| 86 | |||
| 87 | use base qw(XML::SAX::Writer::XML); | ||
| 88 | |||
| 89 | sub start_element {} | ||
| 90 | sub end_element {} | ||
| 91 | sub characters {} | ||
| 92 | sub processing_instruction {} | ||
| 93 | sub ignorable_whitespace {} | ||
| 94 | sub comment {} | ||
| 95 | sub start_cdata {} | ||
| 96 | sub end_cdata {} | ||
| 97 | sub start_entity {} | ||
| 98 | sub end_entity {} | ||
| 99 | sub xml_decl {} | ||
| 100 | |||
| 101 | sub start_dtd { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | $self->{BufferDTD} = ''; | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub end_dtd { | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | my $dtd = $self->{BufferDTD}; | ||
| 112 | $dtd = $self->{Encoder}->convert($dtd); | ||
| 113 | $self->{Consumer}->output($dtd); | ||
| 114 | $self->{BufferDTD} = ''; | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | __END__ |