#!/usr/bin/perl

######################################################################
#
# A PseudoPod to DocBook converter script that uses
# Pod::PseudoPod::DocBook.
#
# usage:
#
# $ ppod2html [-i bookid] [-n include.xml] filename1.pod filename2.pod
#
# It produces a single XML file 'book.xml' that contains all chapters
# passed to it. 
#
# If the -i option is passed in, with a one-word identifier for the
# book, the formatter uses that identifier as part of all autogenerated
# ID tags in the book (on chapters, sections, tables, figures,
# footnotes, and index items).
#
# If the -n option is passed in, with the full path to a partial XML
# file, the contents of that file are included raw in the generated XML
# file (at the beginning of the file where the book <title> and
# <bookinfo> tags usually appear).
#
######################################################################

use strict;
use lib 'lib';
use Pod::PseudoPod::DocBook;
use File::Basename;
use Getopt::Std;


# Optionally, you can set a book ID for autogenerated ID tags, and an
# xml file of bookinfo to include in the book.
my %opts;
getopts('i:n:', \%opts);
my $bookid = $opts{'i'};


# DocBook output goes to the current working directory.
open XMLOUT, ">book.xml" or die "Can't write to book.xml: $!";

# Prepend appropriate header information for a DocBook XML document.
print XMLOUT <<'EOHEAD';
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<book>
<!--  bookinfo gets included here  -->
EOHEAD

# If a partial XML file was provided to include, output it into the
# generated XML file.
if ( $opts{'n'} && open INCLUDE, $opts{'n'}) {
        my @include = <INCLUDE>;
	print XMLOUT @include;
	close INCLUDE;
}

my ($book_xml, $index_count, $preface_num);
foreach my $file (@ARGV) {
	my $parser = Pod::PseudoPod::DocBook->new();
	$parser->index_count($index_count);
	$parser->book_id($bookid);

	# Figures out the type of chapter and chapter number by the
	# filename. This assumes the standard naming convention of
	# ch01.pod (for the first chapter), appa.pod (for the first
	# appendix), and colophon.pod for the colophon. The preface may
	# be either ch00.pod, or preface.pod. For any other chapter
	# naming convention, you'll need to modify this code to
	# correctly identify the appropriate chapter information, so it
	# can generate the right DocBook tags.

	my $filename = fileparse( $file, qr{\..*} );
	if ($filename =~ /preface/) {
		$parser->chapter_type('preface');
		$parser->chapter_num(++$preface_num);
        } elsif ($filename =~ /colophon/) {
		$parser->chapter_type('colophon');
        } elsif ($filename =~ /^app(\w)/) {
		$parser->chapter_type('appendix');
		$parser->chapter_num(uc($1));
        }

	if ($filename =~ /(\d+)/) {
		$parser->chapter_num(int($1));
		if ($1 == 0) {
			$parser->chapter_type('preface');
			$parser->chapter_num(++$preface_num);
		}
        }

	$parser->output_string($book_xml);
	$parser->no_errata_section(1); # don't put errors in doc output
	$parser->complain_stderr(1);   # output errors on STDERR instead

	if (-e $file) {
		$parser->parse_file($file);
	} else {
		die "Unable to open file\n";
	}
	$index_count = $parser->index_count();
}
print XMLOUT $book_xml;

print XMLOUT "\n</book>";
close XMLOUT;

exit;
