#!/usr/bin/perl
# vim: set ts=4 sw=4 tw=78 et si ft=perl:
#
# make-epub-content-opf
#
use 5.010;
use strict;
use warnings;

use App::MakeEPUB;
use File::Spec;
use Getopt::Long;
use Pod::Usage;

use version; our $VERSION = qv('v0.3.2');

my %opt = (
    tocdepth    => 1,
    identifier  => sprintf('%x',time),
    language    => 'en',
    title       => 'You forgot to set a title',
);

my %guidetitle = (
    cover   => 'Cover',
);

GetOptions( \%opt,
    'cover=s',
    'creator=s',
    'identifier=s',
    'language=s',
    'level2=s',
    'publisher=s',
    'rights=s',
    'spine=s',
    'tocdepth=i',
    'title=s',
    'output=s',
    'help|?', 'manual')
    or pod2usage(2);

pod2usage(-exitval => 0, -verbose => 1, -input => \*DATA) if ($opt{help});
pod2usage(-exitval => 0, -verbose => 2, -input => \*DATA) if ($opt{manual});

my $epubdir = shift;

pod2usage(-exitval => 1, -verbose => 1, -input => \*DATA) unless ($epubdir);

my $epub = App::MakeEPUB->new({ epubdir     => $epubdir,
                                level2      => $opt{level2},
                                spine_order => $opt{spine},
                              });

$epub->add_metadata(\%opt);
$epub->write_epub($opt{output});

exit 0;

__END__

=head1 NAME

make-epub - create an EPUB ebook from a directory

=head1 SYNOPSIS

 make-epub [options] path/to/epubdir

Create the metadata for an EPUB book from the files I<path/to/epubdir> and
write the metadata and files into an EPUB file.

=head1 OPTIONS

=over 8

=item B<< -help >>

Print a brief help message and exit.

=item B<< -manual >>

Print the manual page and exit.

=item B<< -output filename >>

This option sets the name of the EPUB file the program will create.

If it is missing the suffix I<< .epub >> is added to I<< path/to/epubdir >>
and used as the name of the EPUB file.

=item B<< -creator name_of_creator >>

This option sets the dc:creator attribute of the metadata.
You may use this for information about the author.

=item B<< -cover coverfile >>

This option marks the file containing the cover for the OPF guide.

=item B<< -identifier >>

Set the I<dc:identifier> for the metadata.

=item B<< -language >>

Set the I<dc:language> for the metadata. Defaults to I<en>.

=item B<< -level2 >>

This option takes a string containing instructions on how to find the text
and id for the level 2 navPoints in the file I<toc.ncf>.

The content for the navPoint is taken from the first C<< <a> >> tag inside
each HTML element found containing an attribute I<id>.
The text for the navPoint is taken from the whole text inside each
HTML element.

If the argument I<level2> is missing, '_tag:span,class:h2' is taken as default.
This will take the navPoints from all spans looking roughly like

  <span class="h2"><a id="navid">some text</a></span>

and translates them into something like

  <navPoint id="navpoint-id" playOrder="order">
    <navLabel><text>some text</text></navLabel>
    <content src="filename#navid" />
  </navPoint>

=item B<< -publisher name_of_publisher >>

This option sets the dc:publisher attribute of the metadata.
You may use this for information about the publisher.

=item B<< -rights the_copyright >>

This options sets the dc:rights attribute of the metadata.
Use this for copyright information.

=item B<< -title title >>

Set the I<dc:title> for the metadata.

=item B<< -spine order >>

This option determines the order of the HTML pages in the EPUB. The argument
for this options is a comma separated list of basenames (without ending) of
the HTML files in the EPUB book.

For instance if you have two HTML files with path I<OEBPS/part1.html> and
I<OEBPS/part2.html> you would give option C<-spine> like this:

 make-epub-content-opf -spine=part1,part2 ...

=item B<< -tocdepth level >>

This option allows to set the maximum level of the table of contents.

The default is 1, maximum is 2.

Level 1 is taken from the C<< <title> >> tag of each HTML file in the ebook.

At the moment level 2 works only with HTML RFCs that have their headings
organized like C<< <span class="h2"><a id="some-id">...</span> >>. The attr
I<id> from the C<< <a> >> tag is taken as url extension into the file and the
text inside the C<< <span> >> tags is taken as label.

=back

=head1 DESCRIPTION

This program will generate the metadata files for an EPUB book.

It determines the name and path of the I<content.opf> and the I<toc.ncx>
file and writes the metadata gathered from the command line and the files in
the given directory into them.

=head1 AUTHOR

Mathias Weidner
=cut
container.xml:<?xml version="1.0"?>
container.xml:<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
container.xml: <rootfiles>
container.xml:  <rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
container.xml: </rootfiles>
container.xml:</container>
content.opf:<?xml version="1.0"?>
content.opf:<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="uid" version="2.0">
content.opf:
content.opf: <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opf="http://www.idpf.org/2007/opf">
content.opf:%%METADATA%%
content.opf: </metadata>
content.opf:
content.opf: <manifest>
content.opf:%%MANIFEST%%
content.opf: </manifest>
content.opf:
content.opf: <spine toc="ncx">
content.opf:%%SPINE%%
content.opf: </spine>
content.opf:
content.opf:%%GUIDE%%
content.opf:</package>
toc.ncx:<?xml version="1.0"?>
toc.ncx:<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
toc.ncx:<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
toc.ncx:
toc.ncx: <head>
toc.ncx:%%TOCNCXHEAD%%
toc.ncx: </head>
toc.ncx:
toc.ncx: <docTitle>
toc.ncx:%%TOCNCXDOCTITLE%%
toc.ncx: </docTitle>
toc.ncx:
toc.ncx: <navMap>
toc.ncx:%%TOCNCXNAVMAP%%
toc.ncx: </navMap>
toc.ncx:
toc.ncx:</ncx>
