| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
###################################################################### |
| 4 |
# |
| 5 |
# A PseudoPod to DocBook converter script that uses |
| 6 |
# Pod::PseudoPod::DocBook. |
| 7 |
# |
| 8 |
# usage: |
| 9 |
# |
| 10 |
# $ ppod2html [-i bookid] [-n include.xml] filename1.pod filename2.pod |
| 11 |
# |
| 12 |
# It produces a single XML file 'book.xml' that contains all chapters |
| 13 |
# passed to it. |
| 14 |
# |
| 15 |
# If the -i option is passed in, with a one-word identifier for the |
| 16 |
# book, the formatter uses that identifier as part of all autogenerated |
| 17 |
# ID tags in the book (on chapters, sections, tables, figures, |
| 18 |
# footnotes, and index items). |
| 19 |
# |
| 20 |
# If the -n option is passed in, with the full path to a partial XML |
| 21 |
# file, the contents of that file are included raw in the generated XML |
| 22 |
# file (at the beginning of the file where the book <title> and |
| 23 |
# <bookinfo> tags usually appear). |
| 24 |
# |
| 25 |
###################################################################### |
| 26 |
|
| 27 |
use strict; |
| 28 |
use lib 'lib'; |
| 29 |
use Pod::PseudoPod::DocBook; |
| 30 |
use File::Basename; |
| 31 |
use Getopt::Std; |
| 32 |
|
| 33 |
|
| 34 |
# Optionally, you can set a book ID for autogenerated ID tags, and an |
| 35 |
# xml file of bookinfo to include in the book. |
| 36 |
my %opts; |
| 37 |
getopts('i:n:', \%opts); |
| 38 |
my $bookid = $opts{'i'}; |
| 39 |
|
| 40 |
|
| 41 |
# DocBook output goes to the current working directory. |
| 42 |
open XMLOUT, ">book.xml" or die "Can't write to book.xml: $!"; |
| 43 |
|
| 44 |
# Prepend appropriate header information for a DocBook XML document. |
| 45 |
print XMLOUT <<'EOHEAD'; |
| 46 |
<?xml version="1.0" encoding="UTF-8"?> |
| 47 |
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> |
| 48 |
<book> |
| 49 |
<!-- bookinfo gets included here --> |
| 50 |
EOHEAD |
| 51 |
|
| 52 |
# If a partial XML file was provided to include, output it into the |
| 53 |
# generated XML file. |
| 54 |
if ( $opts{'n'} && open INCLUDE, $opts{'n'}) { |
| 55 |
my @include = <INCLUDE>; |
| 56 |
print XMLOUT @include; |
| 57 |
close INCLUDE; |
| 58 |
} |
| 59 |
|
| 60 |
my ($book_xml, $index_count, $preface_num); |
| 61 |
foreach my $file (@ARGV) { |
| 62 |
my $parser = Pod::PseudoPod::DocBook->new(); |
| 63 |
$parser->index_count($index_count); |
| 64 |
$parser->book_id($bookid); |
| 65 |
|
| 66 |
# Figures out the type of chapter and chapter number by the |
| 67 |
# filename. This assumes the standard naming convention of |
| 68 |
# ch01.pod (for the first chapter), appa.pod (for the first |
| 69 |
# appendix), and colophon.pod for the colophon. The preface may |
| 70 |
# be either ch00.pod, or preface.pod. For any other chapter |
| 71 |
# naming convention, you'll need to modify this code to |
| 72 |
# correctly identify the appropriate chapter information, so it |
| 73 |
# can generate the right DocBook tags. |
| 74 |
|
| 75 |
my $filename = fileparse( $file, qr{\..*} ); |
| 76 |
if ($filename =~ /preface/) { |
| 77 |
$parser->chapter_type('preface'); |
| 78 |
$parser->chapter_num(++$preface_num); |
| 79 |
} elsif ($filename =~ /colophon/) { |
| 80 |
$parser->chapter_type('colophon'); |
| 81 |
} elsif ($filename =~ /^app(\w)/) { |
| 82 |
$parser->chapter_type('appendix'); |
| 83 |
$parser->chapter_num(uc($1)); |
| 84 |
} |
| 85 |
|
| 86 |
if ($filename =~ /(\d+)/) { |
| 87 |
$parser->chapter_num(int($1)); |
| 88 |
if ($1 == 0) { |
| 89 |
$parser->chapter_type('preface'); |
| 90 |
$parser->chapter_num(++$preface_num); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
$parser->output_string($book_xml); |
| 95 |
$parser->no_errata_section(1); # don't put errors in doc output |
| 96 |
$parser->complain_stderr(1); # output errors on STDERR instead |
| 97 |
|
| 98 |
if (-e $file) { |
| 99 |
$parser->parse_file($file); |
| 100 |
} else { |
| 101 |
die "Unable to open file\n"; |
| 102 |
} |
| 103 |
$index_count = $parser->index_count(); |
| 104 |
} |
| 105 |
print XMLOUT $book_xml; |
| 106 |
|
| 107 |
print XMLOUT "\n</book>"; |
| 108 |
close XMLOUT; |
| 109 |
|
| 110 |
exit; |