EBook-Tools
view release on metacpan or search on metacpan
lib/EBook/Tools/Unpack.pm view on Meta::CPAN
next;
}
if($exth_is_int{$$exth{type}})
{
$data = '0x' . hexstring($$exth{data});
}
else
{
$data = $$exth{data};
}
if($exth_repeats{$$exth{type}})
{
debug(2,"DEBUG: Repeating EXTH ",$type," = '",$data,"'");
my @extharray = ();
my $oldexth = $$self{detected}{$type};
if(ref $oldexth eq 'ARRAY') { @extharray = @$oldexth; }
elsif($oldexth) { push(@extharray,$oldexth); }
push(@extharray,$data);
$$self{detected}{$type} = \@extharray;
}
else
{
debug(2,"DEBUG: Single EXTH ",$type," = '",$data,"'");
$$self{detected}{$type} = $data;
}
}
return 1;
}
=head2 C<gen_opf(%args)>
This generates an OPF file from detected and specified metadata. It
does not honor the C<nosave> flag, and will always write its output.
Normally this is called automatically from inside the C<unpack>
methods, but can be called manually after an unpack if the C<nosave>
flag was set to write an OPF anyway.
Returns the filename of the OPF file.
=head3 Arguments
=over
=item * C<opffile> (optional)
If specified, this overrides the object attribute C<opffile>, and
determines the filename to use for the generated OPF file. If not
specified, and the object attribute C<opffile> has somehow been
cleared (the attribute is set during L</new()>), it will be generated
by looking at the C<textfile> argument. If no value can be found, the
method croaks. If a value was found somewhere other than the object
attribute C<opffile>, then the object attribute is updated to match.
=item * C<textfile> (optional)
The file containing the main text of the document. If specified, the
method will attempt to split metadata out of the file and add whatever
remains to the manifest of the OPF.
=item * C<mediatype> (optional)
The media type (mime type) of the document specified via C<textfile>.
If C<textfile> is not specified, this argument is ignored. If C<textfile> is specified, but
=back
=cut
sub gen_opf :method ## no critic (Always unpack @_ first)
{
my $self = shift;
my (%args) = @_;
my $subname = ( caller(0) )[3];
debug(2,"DEBUG[",$subname,"]");
my %valid_args = (
'opffile' => 1,
'textfile' => 1,
'mediatype' => 1,
);
foreach my $arg (keys %args)
{
croak($subname,"(): invalid argument '",$arg,"'")
if(!$valid_args{$arg});
}
my $ebook = EBook::Tools->new();
my $textfile = $args{textfile};
my $opffile = $args{opffile} || $$self{opffile};
my $mediatype = $args{mediatype};
unless($self->{raw})
{
$opffile = split_metadata($textfile,$opffile) if($textfile);
}
my $detected;
my $code;
my $index;
croak($subname,"(): could not determine OPF filename\n")
unless($opffile);
$$self{opffile} ||= $opffile;
if(-f $opffile)
{
$ebook->init($opffile);
}
else
{
$ebook->init_blank(opffile => $opffile,
title => $$self{title},
author => $$self{author});
}
$ebook->fix_metastructure_oeb12();
$ebook->add_document($textfile,'text-main',$mediatype) if($textfile);
# Set author, title, and opffile from manual overrides
$ebook->set_primary_author(text => $$self{author}) if($$self{author});
$ebook->set_title(text => $$self{title}) if($$self{title});
$ebook->set_opffile($$self{opffile}) if($$self{opffile});
( run in 1.259 second using v1.01-cache-2.11-cpan-71847e10f99 )