| 1 | package EBook::Tools::Unpack; | ||
| 2 | 2 | ▶ | use warnings; use strict; use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use English qw( -no_match_vars ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use version 0.74; our $VERSION = qv("0.4.9"); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | # Perl Critic overrides: | ||
| 7 | ## no critic (Package variable) | ||
| 8 | # RequireBriefOpen seems to be way too brief to be useful | ||
| 9 | ## no critic (RequireBriefOpen) | ||
| 10 | # Builtin homonyms are methods and flagged as such | ||
| 11 | ## no critic (ProhibitBuiltinHomonyms) | ||
| 12 | # Double-sigils are needed for lexical filehandles in clear print statements | ||
| 13 | ## no critic (Double-sigil dereference) | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | EBook::Tools::Unpack - Object class for unpacking e-book files into their component parts and metadata | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use EBook::Tools::Unpack; | ||
| 22 | my $unpacker = EBook::Tools::Unpack->new( | ||
| 23 | 'file' => $filename, | ||
| 24 | 'dir' => $dir, | ||
| 25 | 'encoding' => $encoding, | ||
| 26 | 'format' => $format, | ||
| 27 | 'raw' => $raw, | ||
| 28 | 'author' => $author, | ||
| 29 | 'title' => $title, | ||
| 30 | 'opffile' => $opffile, | ||
| 31 | 'tidy' => $tidy, | ||
| 32 | 'nosave' => $nosave, | ||
| 33 | ); | ||
| 34 | $unpacker->unpack; | ||
| 35 | |||
| 36 | or, more simply: | ||
| 37 | |||
| 38 | use EBook::Tools::Unpack; | ||
| 39 | my $unpacker = EBook::Tools::Unpack->new('file' => 'mybook.prc'); | ||
| 40 | $unpacker->unpack; | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | require Exporter; | ||
| 45 | 2 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | |||
| 47 | our @EXPORT_OK; | ||
| 48 | @EXPORT_OK = qw ( | ||
| 49 | ); | ||
| 50 | |||
| 51 | 2 | ▶ | use Archive::Zip qw(:ERROR_CODES); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 52 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 53 | 2 | ▶ | use EBook::Tools qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 54 | use EBook::Tools::EReader qw(:all); | ||
| 55 | use EBook::Tools::IMP qw(:all); | ||
| 56 | use EBook::Tools::Mobipocket qw(:all); | ||
| 57 | use EBook::Tools::MSReader qw(:all); | ||
| 58 | use EBook::Tools::PalmDoc qw(uncompress_palmdoc); | ||
| 59 | use Encode; | ||
| 60 | use Fcntl qw(SEEK_CUR SEEK_SET); | ||
| 61 | use File::Basename qw(dirname fileparse); | ||
| 62 | use File::Path; # Exports 'mkpath' and 'rmtree' | ||
| 63 | binmode(STDERR,":utf8"); | ||
| 64 | |||
| 65 | my $drmsupport = 0; | ||
| 66 | eval | ||
| 67 | { | ||
| 68 | require EBook::Tools::DRM; | ||
| 69 | EBook::Tools::DRM->import(); | ||
| 70 | }; # Trailing semicolon is required here | ||
| 71 | unless($@){ $drmsupport = 1; } | ||
| 72 | |||
| 73 | |||
| 74 | our %palmdbcodes = ( | ||
| 75 | '.pdfADBE' => 'adobereader', | ||
| 76 | 'TEXtREAd' => 'palmdoc', | ||
| 77 | 'BVokBDIC' => 'bdicty', | ||
| 78 | 'DB99DBOS' => 'db', | ||
| 79 | 'PNPdPPrs' => 'ereader', | ||
| 80 | 'PNRdPPrs' => 'ereader', | ||
| 81 | 'vIMGView' => 'fireviewer', | ||
| 82 | 'PmDBPmDB' => 'handbase', | ||
| 83 | 'InfoINDB' => 'infoview', | ||
| 84 | 'ToGoToGo' => 'isilo1', | ||
| 85 | 'SDocSilX' => 'isilo3', | ||
| 86 | 'JbDbJBas' => 'jfile', | ||
| 87 | 'JfDbJFil' => 'jfilepro', | ||
| 88 | 'DATALSdb' => 'list', | ||
| 89 | 'Mdb1Mdb1' => 'mobiledb', | ||
| 90 | 'BOOKMOBI' => 'mobipocket', | ||
| 91 | 'DataPlkr' => 'plucker', | ||
| 92 | 'DataSprd' => 'quicksheet', | ||
| 93 | 'SM01SMem' => 'supermemo', | ||
| 94 | 'TEXtTlDc' => 'tealdoc', | ||
| 95 | 'InfoTlIf' => 'tealinfo', | ||
| 96 | 'DataTlMl' => 'tealmeal', | ||
| 97 | 'DataTlPt' => 'tealpaint', | ||
| 98 | 'dataTDBP' => 'thinkdb', | ||
| 99 | 'TdatTide' => 'tides', | ||
| 100 | 'ToRaTRPW' => 'tomeraider', | ||
| 101 | 'BDOCWrdS' => 'wordsmith', | ||
| 102 | 'zTXTGPlm' => 'ztxt', | ||
| 103 | ); | ||
| 104 | |||
| 105 | our %pdbcompression = ( | ||
| 106 | 1 => 'no compression', | ||
| 107 | 2 => 'PalmDoc compression', | ||
| 108 | 17480 => 'Mobipocket DRM', | ||
| 109 | ); | ||
| 110 | |||
| 111 | |||
| 112 | ################################# | ||
| 113 | ########## CONSTRUCTOR ########## | ||
| 114 | ################################# | ||
| 115 | |||
| 116 | =head1 CONSTRUCTOR | ||
| 117 | |||
| 118 | =head2 C<new(%args)> | ||
| 119 | |||
| 120 | Instantiates a new Ebook::Tools::Unpack object. | ||
| 121 | |||
| 122 | =head3 Arguments | ||
| 123 | |||
| 124 | =over | ||
| 125 | |||
| 126 | =item * C<file> | ||
| 127 | |||
| 128 | The file to unpack. Specifying this is mandatory. | ||
| 129 | |||
| 130 | =item * C<dir> | ||
| 131 | |||
| 132 | The directory to unpack into. If not specified, defaults to the | ||
| 133 | basename of the file. | ||
| 134 | |||
| 135 | =item * C<encoding> | ||
| 136 | |||
| 137 | If specified, overrides the encoding to use when unpacking. This is | ||
| 138 | normally detected from the file and does not need to be specified. | ||
| 139 | |||
| 140 | Valid values are '1252' (specifying Windows-1252) and '65001' | ||
| 141 | (specifying UTF-8). | ||
| 142 | |||
| 143 | =item * C<htmlconvert> | ||
| 144 | |||
| 145 | If set to true, an attempt will be made to convert non-HTML output | ||
| 146 | text to HTML where possible. | ||
| 147 | |||
| 148 | =item * C<key> | ||
| 149 | |||
| 150 | The decryption key to use if necessary (not yet implemented) | ||
| 151 | |||
| 152 | =item * C<keyfile> | ||
| 153 | |||
| 154 | The file holding the decryption keys to use if necessary (not yet | ||
| 155 | implemented) | ||
| 156 | |||
| 157 | =item * C<language> | ||
| 158 | |||
| 159 | If specified, overrides the detected language information. | ||
| 160 | |||
| 161 | =item * C<opffile> | ||
| 162 | |||
| 163 | The name of the file in which the metadata will be stored. If not | ||
| 164 | specified, defaults to the value of C<dir> with C<.opf> appended. | ||
| 165 | |||
| 166 | =item * C<raw> | ||
| 167 | |||
| 168 | If set true, this forces no corrections to be done on any extracted | ||
| 169 | text and a lot of raw, unparsed, unmodified data to be dumped into the | ||
| 170 | directory along with everything else. It's useful for debugging | ||
| 171 | exactly what was in the file being unpacked, and (when combined with | ||
| 172 | C<nosave>) reducing the time needed to extract parsed data from an | ||
| 173 | ebook container without actually unpacking it. | ||
| 174 | |||
| 175 | =item * C<author> | ||
| 176 | |||
| 177 | Overrides the detected author name. | ||
| 178 | |||
| 179 | =item * C<title> | ||
| 180 | |||
| 181 | Overrides the detected title. | ||
| 182 | |||
| 183 | =item * C<tidy> | ||
| 184 | |||
| 185 | If set to true, the unpacker will run tidy on any HTML output files to | ||
| 186 | convert them to valid XHTML. Be warned that this can occasionally | ||
| 187 | change the formatting, as Tidy isn't very forgiving on certain common | ||
| 188 | tricks (such as empty <pre> elements with style elements) that abuse | ||
| 189 | the standard. | ||
| 190 | |||
| 191 | =item * C<nosave> | ||
| 192 | |||
| 193 | If set to true, the unpacker will run through all of the unpacking | ||
| 194 | steps except those that actually write to the disk. This is useful | ||
| 195 | for testing, but also (particularly when combined with C<raw>) can be | ||
| 196 | used for extracting parsed data from an ebook container without | ||
| 197 | actually unpacking it. | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | my @fields = ( | ||
| 204 | 'file', | ||
| 205 | 'dir', | ||
| 206 | 'encoding', | ||
| 207 | 'format', | ||
| 208 | 'formatinfo', | ||
| 209 | 'htmlconvert', | ||
| 210 | 'raw', | ||
| 211 | 'key', | ||
| 212 | 'keyfile', | ||
| 213 | 'opffile', | ||
| 214 | 'author', | ||
| 215 | 'language', | ||
| 216 | 'title', | ||
| 217 | 'datahashes', | ||
| 218 | 'detected', | ||
| 219 | 'tidy', | ||
| 220 | 'nosave', | ||
| 221 | ); | ||
| 222 | |||
| 223 | require fields; | ||
| 224 | fields->import(@fields); | ||
| 225 | |||
| 226 | sub new ## no critic (Always unpack @_ first) | ||
| 227 | { | ||
| 228 | my $self = shift; | ||
| 229 | my (%args) = @_; | ||
| 230 | my $class = ref($self) || $self; | ||
| 231 | my $subname = (caller(0))[3]; | ||
| 232 | debug(2,"DEBUG[",$subname,"]"); | ||
| 233 | |||
| 234 | my %valid_args = ( | ||
| 235 | 'file' => 1, | ||
| 236 | 'dir' => 1, | ||
| 237 | 'encoding' => 1, | ||
| 238 | 'key' => 1, | ||
| 239 | 'keyfile' => 1, | ||
| 240 | 'format' => 1, | ||
| 241 | 'htmlconvert' => 1, | ||
| 242 | 'raw' => 1, | ||
| 243 | 'author' => 1, | ||
| 244 | 'language' => 1, | ||
| 245 | 'title' => 1, | ||
| 246 | 'opffile' => 1, | ||
| 247 | 'tidy' => 1, | ||
| 248 | 'nosave' => 1, | ||
| 249 | ); | ||
| 250 | foreach my $arg (keys %args) | ||
| 251 | { | ||
| 252 | croak($subname,"(): invalid argument '",$arg,"'") | ||
| 253 | if(!$valid_args{$arg}); | ||
| 254 | } | ||
| 255 | croak($subname,"(): no input file specified") | ||
| 256 | unless($args{file}); | ||
| 257 | croak($subname,"(): '",$args{file},"' not found") | ||
| 258 | unless(-f $args{file}); | ||
| 259 | |||
| 260 | $self = fields::new($class); | ||
| 261 | $self->{file} = $args{file}; | ||
| 262 | $self->{dir} = $args{dir} || $self->filebase; | ||
| 263 | $self->{encoding} = $args{encoding}; | ||
| 264 | $self->{format} = $args{format} if($args{format}); | ||
| 265 | $self->{key} = $args{key} if($args{key}); | ||
| 266 | $self->{keyfile} = $args{keyfile} if($args{keyfile}); | ||
| 267 | $self->{opffile} = $args{opffile} || ($self->{dir} . ".opf"); | ||
| 268 | $self->{author} = $args{author} if($args{author}); | ||
| 269 | $self->{title} = $args{title} if($args{title}); | ||
| 270 | $self->{datahashes} = {}; | ||
| 271 | $self->{detected} = {}; | ||
| 272 | $self->{htmlconvert} = $args{htmlconvert}; | ||
| 273 | $self->{raw} = $args{raw}; | ||
| 274 | $self->{tidy} = $args{tidy}; | ||
| 275 | $self->{nosave} = $args{nosave}; | ||
| 276 | |||
| 277 | $self->detect_format unless($self->{format}); | ||
| 278 | return $self; | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | =head1 ACCESSOR METHODS | ||
| 283 | |||
| 284 | See L</new()> for more details on what some of these mean. Note | ||
| 285 | that some values cannot be autodetected until an unpack method | ||
| 286 | executes. | ||
| 287 | |||
| 288 | =head2 C<author> | ||
| 289 | |||
| 290 | =head2 C<dir> | ||
| 291 | |||
| 292 | =head2 C<file> | ||
| 293 | |||
| 294 | =head2 C<filebase> | ||
| 295 | |||
| 296 | In scalar context, this is the basename of C<file>. In list context, | ||
| 297 | it actually returns the basename, directory, and extension as per | ||
| 298 | C<fileparse> from L<File::Basename>. | ||
| 299 | |||
| 300 | =head2 C<format> | ||
| 301 | |||
| 302 | =head2 C<key> | ||
| 303 | |||
| 304 | =head2 C<keyfile> | ||
| 305 | |||
| 306 | =head2 C<language> | ||
| 307 | |||
| 308 | This returns the language specified by the user, if any. It remains | ||
| 309 | undefined if the user has not requested that a language code be set | ||
| 310 | even if a language was autodetected. | ||
| 311 | |||
| 312 | =head2 C<opffile> | ||
| 313 | |||
| 314 | =head2 C<raw> | ||
| 315 | |||
| 316 | =head2 C<title> | ||
| 317 | |||
| 318 | This returns the title specified by the user, if any. It remains | ||
| 319 | undefined if the user has not requested a title be set even if a title | ||
| 320 | was autodetected. | ||
| 321 | |||
| 322 | =head2 C<detected> | ||
| 323 | |||
| 324 | This returns a hash containing the autodetected metadata, if any. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | sub author :method | ||
| 329 | { | ||
| 330 | my $self = shift; | ||
| 331 | return $$self{author}; | ||
| 332 | } | ||
| 333 | |||
| 334 | sub dir :method | ||
| 335 | { | ||
| 336 | my $self = shift; | ||
| 337 | return $$self{dir}; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub file :method | ||
| 341 | { | ||
| 342 | my $self = shift; | ||
| 343 | return $$self{file}; | ||
| 344 | } | ||
| 345 | |||
| 346 | sub filebase :method | ||
| 347 | { | ||
| 348 | my $self = shift; | ||
| 349 | return fileparse($$self{file},'\.\w+$'); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub format :method | ||
| 353 | { | ||
| 354 | my $self = shift; | ||
| 355 | return $$self{format}; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub key :method | ||
| 359 | { | ||
| 360 | my $self = shift; | ||
| 361 | return $$self{key}; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub keyfile :method | ||
| 365 | { | ||
| 366 | my $self = shift; | ||
| 367 | return $$self{keyfile}; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub language :method | ||
| 371 | { | ||
| 372 | my $self = shift; | ||
| 373 | return $$self{language}; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub opffile :method | ||
| 377 | { | ||
| 378 | my $self = shift; | ||
| 379 | return $$self{opffile}; | ||
| 380 | } | ||
| 381 | |||
| 382 | sub raw :method | ||
| 383 | { | ||
| 384 | my $self = shift; | ||
| 385 | return $$self{raw}; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub title :method | ||
| 389 | { | ||
| 390 | my $self = shift; | ||
| 391 | return $$self{opffile}; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub detected :method | ||
| 395 | { | ||
| 396 | my $self = shift; | ||
| 397 | return $$self{detected}; | ||
| 398 | } | ||
| 399 | |||
| 400 | |||
| 401 | =head1 MODIFIER METHODS | ||
| 402 | |||
| 403 | =head2 C<detect_format()> | ||
| 404 | |||
| 405 | Attempts to automatically detect the format of the input file and set | ||
| 406 | the internal object attributes C<< $self->{format} >> and | ||
| 407 | C<< $self->{formatinfo} >>, where the former is a one-word string used by | ||
| 408 | the dispatcher to select the correct unpacking method and the latter | ||
| 409 | may contain additional detected information (such as a title or | ||
| 410 | version). | ||
| 411 | |||
| 412 | Croaks if detection fails. | ||
| 413 | |||
| 414 | In scalar context, returns C<< $self->{format} >>. In list context, | ||
| 415 | returns the two element list C<< ($self->{format},$self->{formatinfo}) >> | ||
| 416 | |||
| 417 | This is automatically called by L</new()> if the C<format> argument is | ||
| 418 | not specified. | ||
| 419 | |||
| 420 | =cut | ||
| 421 | |||
| 422 | sub detect_format :method | ||
| 423 | { | ||
| 424 | my $self = shift; | ||
| 425 | my $subname = ( caller(0) )[3]; | ||
| 426 | my $filename = $$self{file}; | ||
| 427 | my $fh; | ||
| 428 | my $headerdata; | ||
| 429 | my $ident; | ||
| 430 | my $info; | ||
| 431 | my $index; | ||
| 432 | debug(2,"DEBUG[",$subname,"]"); | ||
| 433 | |||
| 434 | open($fh,"<",$filename) | ||
| 435 | or croak($subname,"(): failed to open '",$filename,"' for reading!\n"); | ||
| 436 | sysread($fh,$headerdata,68); | ||
| 437 | close($fh) | ||
| 438 | or croak($subname,"(): failed to close '",$filename,"'!\n"); | ||
| 439 | |||
| 440 | # Check for PalmDB identifiers | ||
| 441 | $ident = substr($headerdata,60,8); | ||
| 442 | debug(3,"DEBUG: PalmDB ident = '$ident'"); | ||
| 443 | if($palmdbcodes{$ident}) | ||
| 444 | { | ||
| 445 | $$self{format} = $palmdbcodes{$ident}; | ||
| 446 | $info = substr($headerdata,0,32); | ||
| 447 | $index = index($info,"\0"); | ||
| 448 | if($index < 0) | ||
| 449 | { | ||
| 450 | debug(0,"WARNING: detected header in '",$filename, | ||
| 451 | "' is not null-terminated."); | ||
| 452 | } | ||
| 453 | else | ||
| 454 | { | ||
| 455 | $info = substr($info,0,$index); | ||
| 456 | } | ||
| 457 | debug(1,"DEBUG: Autodetected book format '",$$self{format}, | ||
| 458 | "', info '",$info,"'"); | ||
| 459 | $$self{formatinfo} = $info; | ||
| 460 | # The info here is always the title, but there may be better | ||
| 461 | # ways of extracting it later. | ||
| 462 | $$self{detected}{title} = $info; | ||
| 463 | } | ||
| 464 | |||
| 465 | # Check for Microsoft Reader | ||
| 466 | $ident = substr($headerdata,0,8); | ||
| 467 | debug(3,"DEBUG: MS Reader ident = '$ident'"); | ||
| 468 | if($ident eq 'ITOLITLS') | ||
| 469 | { | ||
| 470 | $$self{format} = 'msreader'; | ||
| 471 | $$self{formatinfo} = unpack("c",substr($headerdata,8,1)); | ||
| 472 | debug(1,"DEBUG: Autodetected book format '",$$self{format}, | ||
| 473 | "', version ",$$self{formatinfo}); | ||
| 474 | } | ||
| 475 | |||
| 476 | # Check for ePub | ||
| 477 | $ident = substr($headerdata,30,28); | ||
| 478 | $info = substr($headerdata,0,2); | ||
| 479 | if($ident eq 'mimetypeapplication/epub+zip' | ||
| 480 | && $info eq 'PK') | ||
| 481 | { | ||
| 482 | $$self{format} = 'epub'; | ||
| 483 | $$self{formatinfo} = ''; | ||
| 484 | debug(1,"DEBUG: autodetected book format '",$$self{format},"'"); | ||
| 485 | } | ||
| 486 | |||
| 487 | # Check for .IMP | ||
| 488 | $ident = substr($headerdata,2,8); | ||
| 489 | $info = unpack('n',substr($headerdata,0,2)); | ||
| 490 | if($ident eq 'BOOKDOUG') | ||
| 491 | { | ||
| 492 | $$self{format} = 'imp'; | ||
| 493 | $$self{formatinfo} = $info; | ||
| 494 | debug(1,"DEBUG: autodetected book format '",$$self{format}, | ||
| 495 | "' version ",$$self{formatinfo}); | ||
| 496 | } | ||
| 497 | |||
| 498 | # Check for miscellaneous zip archive (OEBZip?) | ||
| 499 | $ident = substr($headerdata,0,4); | ||
| 500 | if($ident eq "PK\x{03}\x{04}") | ||
| 501 | { | ||
| 502 | $$self{format} = 'ziparchive'; | ||
| 503 | $$self{formatinfo} = unpack('c',substr($headerdata,4,1)) / 10; | ||
| 504 | debug(1,"DEBUG: autodetected book format '",$$self{format}, | ||
| 505 | "', version ",$$self{formatinfo}); | ||
| 506 | } | ||
| 507 | |||
| 508 | croak($subname,"(): unable to determine book format") | ||
| 509 | unless($$self{format}); | ||
| 510 | |||
| 511 | if(wantarray) { return ($$self{format},$$self{formatinfo}); } | ||
| 512 | else { return $$self{format}; } | ||
| 513 | } | ||
| 514 | |||
| 515 | |||
| 516 | =head2 C<detect_from_mobi_exth()> | ||
| 517 | |||
| 518 | Detects metadata values from the MOBI EXTH headers retrieved via | ||
| 519 | L</unpack_mobi_exth()> and places them into the C<detected> attribute. | ||
| 520 | |||
| 521 | =cut | ||
| 522 | |||
| 523 | sub detect_from_mobi_exth :method | ||
| 524 | { | ||
| 525 | my $self = shift; | ||
| 526 | my $subname = ( caller(0) )[3]; | ||
| 527 | debug(2,"DEBUG[",$subname,"]"); | ||
| 528 | |||
| 529 | my @mobiexth; | ||
| 530 | if(defined $self->{datahashes}{mobiexth}) | ||
| 531 | { | ||
| 532 | @mobiexth = @{$self->{datahashes}{mobiexth}}; | ||
| 533 | } | ||
| 534 | my $data; | ||
| 535 | my %exthtypes = %EBook::Tools::Mobipocket::exthtypes; | ||
| 536 | my %exth_is_int = %EBook::Tools::Mobipocket::exth_is_int; | ||
| 537 | my %exth_repeats = %EBook::Tools::Mobipocket::exth_repeats; | ||
| 538 | |||
| 539 | # EXTH records | ||
| 540 | foreach my $exth (@mobiexth) | ||
| 541 | { | ||
| 542 | my $type = $exthtypes{$$exth{type}}; | ||
| 543 | unless($type) | ||
| 544 | { | ||
| 545 | carp($subname,"(): unknown EXTH record type ",$$exth{type}); | ||
| 546 | next; | ||
| 547 | } | ||
| 548 | if($exth_is_int{$$exth{type}}) | ||
| 549 | { | ||
| 550 | $data = '0x' . hexstring($$exth{data}); | ||
| 551 | } | ||
| 552 | else | ||
| 553 | { | ||
| 554 | $data = $$exth{data}; | ||
| 555 | } | ||
| 556 | |||
| 557 | if($exth_repeats{$$exth{type}}) | ||
| 558 | { | ||
| 559 | debug(2,"DEBUG: Repeating EXTH ",$type," = '",$data,"'"); | ||
| 560 | my @extharray = (); | ||
| 561 | my $oldexth = $$self{detected}{$type}; | ||
| 562 | if(ref $oldexth eq 'ARRAY') { @extharray = @$oldexth; } | ||
| 563 | elsif($oldexth) { push(@extharray,$oldexth); } | ||
| 564 | push(@extharray,$data); | ||
| 565 | $$self{detected}{$type} = \@extharray; | ||
| 566 | } | ||
| 567 | else | ||
| 568 | { | ||
| 569 | debug(2,"DEBUG: Single EXTH ",$type," = '",$data,"'"); | ||
| 570 | $$self{detected}{$type} = $data; | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | return 1; | ||
| 575 | } | ||
| 576 | |||
| 577 | |||
| 578 | =head2 C<gen_opf(%args)> | ||
| 579 | |||
| 580 | This generates an OPF file from detected and specified metadata. It | ||
| 581 | does not honor the C<nosave> flag, and will always write its output. | ||
| 582 | |||
| 583 | Normally this is called automatically from inside the C<unpack> | ||
| 584 | methods, but can be called manually after an unpack if the C<nosave> | ||
| 585 | flag was set to write an OPF anyway. | ||
| 586 | |||
| 587 | Returns the filename of the OPF file. | ||
| 588 | |||
| 589 | =head3 Arguments | ||
| 590 | |||
| 591 | =over | ||
| 592 | |||
| 593 | =item * C<opffile> (optional) | ||
| 594 | |||
| 595 | If specified, this overrides the object attribute C<opffile>, and | ||
| 596 | determines the filename to use for the generated OPF file. If not | ||
| 597 | specified, and the object attribute C<opffile> has somehow been | ||
| 598 | cleared (the attribute is set during L</new()>), it will be generated | ||
| 599 | by looking at the C<textfile> argument. If no value can be found, the | ||
| 600 | method croaks. If a value was found somewhere other than the object | ||
| 601 | attribute C<opffile>, then the object attribute is updated to match. | ||
| 602 | |||
| 603 | =item * C<textfile> (optional) | ||
| 604 | |||
| 605 | The file containing the main text of the document. If specified, the | ||
| 606 | method will attempt to split metadata out of the file and add whatever | ||
| 607 | remains to the manifest of the OPF. | ||
| 608 | |||
| 609 | =item * C<mediatype> (optional) | ||
| 610 | |||
| 611 | The media type (mime type) of the document specified via C<textfile>. | ||
| 612 | If C<textfile> is not specified, this argument is ignored. If C<textfile> is specified, but | ||
| 613 | |||
| 614 | =back | ||
| 615 | |||
| 616 | =cut | ||
| 617 | |||
| 618 | sub gen_opf :method ## no critic (Always unpack @_ first) | ||
| 619 | { | ||
| 620 | my $self = shift; | ||
| 621 | my (%args) = @_; | ||
| 622 | my $subname = ( caller(0) )[3]; | ||
| 623 | debug(2,"DEBUG[",$subname,"]"); | ||
| 624 | my %valid_args = ( | ||
| 625 | 'opffile' => 1, | ||
| 626 | 'textfile' => 1, | ||
| 627 | 'mediatype' => 1, | ||
| 628 | ); | ||
| 629 | foreach my $arg (keys %args) | ||
| 630 | { | ||
| 631 | croak($subname,"(): invalid argument '",$arg,"'") | ||
| 632 | if(!$valid_args{$arg}); | ||
| 633 | } | ||
| 634 | my $ebook = EBook::Tools->new(); | ||
| 635 | my $textfile = $args{textfile}; | ||
| 636 | my $opffile = $args{opffile} || $$self{opffile}; | ||
| 637 | my $mediatype = $args{mediatype}; | ||
| 638 | unless($self->{raw}) | ||
| 639 | { | ||
| 640 | $opffile = split_metadata($textfile,$opffile) if($textfile); | ||
| 641 | } | ||
| 642 | my $detected; | ||
| 643 | my $code; | ||
| 644 | my $index; | ||
| 645 | |||
| 646 | croak($subname,"(): could not determine OPF filename\n") | ||
| 647 | unless($opffile); | ||
| 648 | $$self{opffile} ||= $opffile; | ||
| 649 | |||
| 650 | if(-f $opffile) | ||
| 651 | { | ||
| 652 | $ebook->init($opffile); | ||
| 653 | } | ||
| 654 | else | ||
| 655 | { | ||
| 656 | $ebook->init_blank(opffile => $opffile, | ||
| 657 | title => $$self{title}, | ||
| 658 | author => $$self{author}); | ||
| 659 | } | ||
| 660 | $ebook->fix_metastructure_oeb12(); | ||
| 661 | $ebook->add_document($textfile,'text-main',$mediatype) if($textfile); | ||
| 662 | |||
| 663 | # Set author, title, and opffile from manual overrides | ||
| 664 | $ebook->set_primary_author(text => $$self{author}) if($$self{author}); | ||
| 665 | $ebook->set_title(text => $$self{title}) if($$self{title}); | ||
| 666 | $ebook->set_opffile($$self{opffile}) if($$self{opffile}); | ||
| 667 | |||
| 668 | # If we still don't have author or title, set it from the best | ||
| 669 | # extraction we have | ||
| 670 | $ebook->set_primary_author(text => $$self{detected}{author}) | ||
| 671 | if(!$$self{author} && $$self{detected}{author}); | ||
| 672 | $ebook->set_title(text => $$self{detected}{title}) | ||
| 673 | if(!$$self{title} && $$self{detected}{title}); | ||
| 674 | |||
| 675 | # Set the language codes | ||
| 676 | $ebook->set_language(text => $$self{detected}{language}) | ||
| 677 | if($$self{detected}{language}); | ||
| 678 | $ebook->set_metadata(gi => 'DictionaryInLanguage', | ||
| 679 | text => $$self{detected}{dictionaryinlanguage}) | ||
| 680 | if($$self{detected}{dictionaryinlanguage}); | ||
| 681 | $ebook->set_metadata(gi => 'DictionaryOutLanguage', | ||
| 682 | text => $$self{detected}{dictionaryoutlanguage}) | ||
| 683 | if($$self{detected}{dictionaryoutlanguage}); | ||
| 684 | |||
| 685 | |||
| 686 | # Set the remaining autodetected metadata, some of which may or | ||
| 687 | # may not be in array form | ||
| 688 | $detected = $$self{detected}{contributor}; | ||
| 689 | if( $detected && (ref($detected) eq 'ARRAY') ) | ||
| 690 | { | ||
| 691 | foreach my $text (@$detected) | ||
| 692 | { | ||
| 693 | $ebook->add_metadata(gi => 'dc:Contributor', | ||
| 694 | parent => 'dc-metadata', | ||
| 695 | text => $text); | ||
| 696 | } | ||
| 697 | } | ||
| 698 | elsif($detected) | ||
| 699 | { | ||
| 700 | $ebook->add_metadata(gi => 'dc:Contributor', | ||
| 701 | parent => 'dc-metadata', | ||
| 702 | text => $detected); | ||
| 703 | } | ||
| 704 | |||
| 705 | $detected = $$self{detected}{publisher}; | ||
| 706 | if( $detected && (ref($detected) eq 'ARRAY') ) | ||
| 707 | { | ||
| 708 | foreach my $text (@$detected) | ||
| 709 | { | ||
| 710 | $ebook->add_metadata(gi => 'dc:Publisher', | ||
| 711 | parent => 'dc-metadata', | ||
| 712 | text => $text); | ||
| 713 | } | ||
| 714 | } | ||
| 715 | elsif($detected) | ||
| 716 | { | ||
| 717 | $ebook->set_publisher(text => $detected); | ||
| 718 | } | ||
| 719 | |||
| 720 | $ebook->set_description(text => decode_utf8($$self{detected}{description})) | ||
| 721 | if($$self{detected}{description}); | ||
| 722 | |||
| 723 | $detected = $$self{detected}{isbn}; | ||
| 724 | if( $detected && (ref($detected) eq 'ARRAY') ) | ||
| 725 | { | ||
| 726 | foreach my $text (@$detected) | ||
| 727 | { | ||
| 728 | $ebook->add_identifier(text => $text, | ||
| 729 | scheme => 'ISBN') | ||
| 730 | } | ||
| 731 | } | ||
| 732 | elsif($detected) | ||
| 733 | { | ||
| 734 | $ebook->add_identifier(text => $detected, | ||
| 735 | scheme => 'ISBN') | ||
| 736 | } | ||
| 737 | |||
| 738 | $detected = $$self{detected}{subject}; | ||
| 739 | if( $detected && (ref($detected) eq 'ARRAY') ) | ||
| 740 | { | ||
| 741 | debug(2,"Multiple dc:Subject entries"); | ||
| 742 | $index = 0; | ||
| 743 | foreach my $text (@$detected) | ||
| 744 | { | ||
| 745 | $code = @{$$self{detected}{subjectcode}}[$index]; | ||
| 746 | $ebook->add_subject(text => $text, | ||
| 747 | basiccode => $code); | ||
| 748 | $index++; | ||
| 749 | } | ||
| 750 | } | ||
| 751 | elsif($detected) | ||
| 752 | { | ||
| 753 | debug(2,"Single dc:Subject entry"); | ||
| 754 | $code = $$self{detected}{subjectcode}; | ||
| 755 | $ebook->add_subject(text => $$self{detected}{subject}, | ||
| 756 | basiccode => $code) | ||
| 757 | } | ||
| 758 | |||
| 759 | $ebook->set_date(text => $$self{detected}{publicationdate}, | ||
| 760 | event => 'publication') | ||
| 761 | if($$self{detected}{publicationdate}); | ||
| 762 | $ebook->set_rights(text => $$self{detected}{rights}) | ||
| 763 | if($$self{detected}{rights}); | ||
| 764 | $ebook->set_type(text => $$self{detected}{type}) | ||
| 765 | if($$self{detected}{type}); | ||
| 766 | $ebook->set_adult($$self{detected}{adult}) | ||
| 767 | if($$self{detected}{adult}); | ||
| 768 | $ebook->set_review(text => decode_utf8($$self{detected}{review})) | ||
| 769 | if($$self{detected}{review}); | ||
| 770 | $ebook->set_retailprice(text => $$self{detected}{retailprice}, | ||
| 771 | currency => $$self{detected}{currency}) | ||
| 772 | if($$self{detected}{retailprice}); | ||
| 773 | |||
| 774 | # Automatically clean up any mess | ||
| 775 | $ebook->fix_misc; | ||
| 776 | $ebook->fix_oeb12; | ||
| 777 | $ebook->fix_mobi; | ||
| 778 | unlink($opffile); | ||
| 779 | $ebook->save; | ||
| 780 | return 1; | ||
| 781 | } | ||
| 782 | |||
| 783 | |||
| 784 | =head2 C<unpack()> | ||
| 785 | |||
| 786 | This is a dispatcher for the specific unpacking methods needed to | ||
| 787 | unpack a particular format. Unless you feel a need to override the | ||
| 788 | unpacking method specified or detected during object construction, it | ||
| 789 | is probalby better to call this than the specific unpacking methods. | ||
| 790 | |||
| 791 | =cut | ||
| 792 | |||
| 793 | sub unpack :method | ||
| 794 | { | ||
| 795 | my $self = shift; | ||
| 796 | my $subname = ( caller(0) )[3]; | ||
| 797 | debug(2,"DEBUG[",$subname,"]"); | ||
| 798 | |||
| 799 | my $filename = $$self{file}; | ||
| 800 | croak($subname,"(): no input file specified\n") | ||
| 801 | unless($filename); | ||
| 802 | my $retval; | ||
| 803 | |||
| 804 | my %unpack_dispatch = ( | ||
| 805 | 'ereader' => \&unpack_ereader, | ||
| 806 | 'imp' => \&unpack_imp, | ||
| 807 | 'mobipocket' => \&unpack_mobi, | ||
| 808 | 'msreader' => \&unpack_msreader, | ||
| 809 | 'palmdoc' => \&unpack_palmdoc, | ||
| 810 | 'aportisdoc' => \&unpack_palmdoc, | ||
| 811 | 'ziparchive' => \&unpack_zip, | ||
| 812 | ); | ||
| 813 | |||
| 814 | croak($subname, | ||
| 815 | "(): don't know how to handle format '",$$self{format},"'\n") | ||
| 816 | if(!$unpack_dispatch{$$self{format}}); | ||
| 817 | $retval = $unpack_dispatch{$$self{format}}->($self); | ||
| 818 | return $retval; | ||
| 819 | } | ||
| 820 | |||
| 821 | |||
| 822 | =head2 C<unpack_ereader()> | ||
| 823 | |||
| 824 | Unpacks Fictionwise/PeanutPress eReader (-er.pdb) files. | ||
| 825 | |||
| 826 | =cut | ||
| 827 | |||
| 828 | sub unpack_ereader :method | ||
| 829 | { | ||
| 830 | my $self = shift; | ||
| 831 | my $subname = ( caller(0) )[3]; | ||
| 832 | debug(2,"DEBUG[",$subname,"]"); | ||
| 833 | |||
| 834 | my $pdb = EBook::Tools::EReader->new(); | ||
| 835 | my $textname; | ||
| 836 | |||
| 837 | $pdb->Load($$self{file}); | ||
| 838 | |||
| 839 | $$self{datahashes}{ereader} = $pdb->{header}; | ||
| 840 | $$self{detected}{title} = decode('Windows-1252',$pdb->{title}); | ||
| 841 | $$self{detected}{author} = decode('Windows-1252',$pdb->{author}); | ||
| 842 | $$self{detected}{rights} = decode('Windows-1252',$pdb->{rights}); | ||
| 843 | $$self{detected}{publisher} = decode('Windows-1252',$pdb->{publisher}); | ||
| 844 | $$self{detected}{isbn} = decode('Windows-1252',$pdb->{isbn}); | ||
| 845 | debug(1,"DEBUG: PDB title: '",$$self{detected}{title},"'"); | ||
| 846 | debug(1,"DEBUG: PDB author: '",$$self{detected}{author},"'"); | ||
| 847 | debug(1,"DEBUG: PDB copyright: '",$$self{detected}{rights},"'"); | ||
| 848 | debug(1,"DEBUG: PDB publisher: '",$$self{detected}{publisher},"'"); | ||
| 849 | debug(1,"DEBUG: PDB ISBN: '",$$self{detected}{isbn},"'"); | ||
| 850 | |||
| 851 | unless($$self{nosave}) | ||
| 852 | { | ||
| 853 | my $cwd = usedir($self->{dir}); | ||
| 854 | $pdb->write_images; | ||
| 855 | $pdb->write_unknown_records if($$self{raw}); | ||
| 856 | if($$self{htmlconvert}) | ||
| 857 | { | ||
| 858 | $textname = $pdb->write_html(); | ||
| 859 | $self->gen_opf(textfile => $textname, | ||
| 860 | mediatype => 'application/xhtml+xml'); | ||
| 861 | } | ||
| 862 | else | ||
| 863 | { | ||
| 864 | $textname = $pdb->write_pml(); | ||
| 865 | $self->gen_opf(textfile => $textname, | ||
| 866 | mediatype => 'text/plain'); | ||
| 867 | } | ||
| 868 | chdir($cwd); | ||
| 869 | } | ||
| 870 | return 1; | ||
| 871 | } | ||
| 872 | |||
| 873 | |||
| 874 | =head2 C<unpack_imp()> | ||
| 875 | |||
| 876 | Unpacks SoftBook/GEB/REB/eBookWise (.imp) files. | ||
| 877 | |||
| 878 | =cut | ||
| 879 | |||
| 880 | sub unpack_imp | ||
| 881 | { | ||
| 882 | my $self = shift; | ||
| 883 | my $subname = ( caller(0) )[3]; | ||
| 884 | debug(2,"DEBUG[",$subname,"]"); | ||
| 885 | |||
| 886 | my $imp = EBook::Tools::IMP->new(); | ||
| 887 | $imp->load($self->{file}); | ||
| 888 | |||
| 889 | $self->{detected}->{author} = $imp->author; | ||
| 890 | $self->{detected}->{title} = $imp->title; | ||
| 891 | |||
| 892 | if($self->{raw}) | ||
| 893 | { | ||
| 894 | $imp->write_resdir(); | ||
| 895 | } | ||
| 896 | |||
| 897 | $imp->write_text(dir => $self->{dir}); | ||
| 898 | $imp->write_images(dir => $self->{dir}); | ||
| 899 | |||
| 900 | print {*STDERR} "WARNING: IMP support not yet functional!\n"; | ||
| 901 | return 0; | ||
| 902 | } | ||
| 903 | |||
| 904 | |||
| 905 | =head2 C<unpack_mobi()> | ||
| 906 | |||
| 907 | Unpacks Mobipocket (.prc / .mobi) files. | ||
| 908 | |||
| 909 | =cut | ||
| 910 | |||
| 911 | sub unpack_mobi :method | ||
| 912 | { | ||
| 913 | my $self = shift; | ||
| 914 | my $subname = ( caller(0) )[3]; | ||
| 915 | debug(2,"DEBUG[",$subname,"]"); | ||
| 916 | |||
| 917 | my $mobi = EBook::Tools::Mobipocket->new(); | ||
| 918 | my @records; | ||
| 919 | my $data; | ||
| 920 | my $opffile; | ||
| 921 | |||
| 922 | # Used for extracting images | ||
| 923 | my $imageid = 0; | ||
| 924 | my $firstimagerec = 0; | ||
| 925 | |||
| 926 | # Used for file output | ||
| 927 | my $htmlname = $self->filebase . ".html"; | ||
| 928 | |||
| 929 | my $reccount = 0; # The Record ID cannot be reliably used to identify | ||
| 930 | # the first record. This increments as each | ||
| 931 | # record is examined | ||
| 932 | |||
| 933 | $mobi->Load($$self{file}); | ||
| 934 | |||
| 935 | @records = @{$mobi->{records}}; | ||
| 936 | croak($subname,"(): no pdb records found!") unless(@records); | ||
| 937 | |||
| 938 | $$self{datahashes}{palm} = $mobi->{header}{palm}; | ||
| 939 | $$self{datahashes}{mobi} = $mobi->{header}{mobi}; | ||
| 940 | $$self{datahashes}{mobiexth} = $mobi->{header}{exth}; | ||
| 941 | $$self{encoding} | ||
| 942 | = $$self{datahashes}{mobi}{encoding} unless($$self{encoding}); | ||
| 943 | $$self{detected}{title} = $mobi->{title}; | ||
| 944 | $$self{detected}{language} = $mobi->{header}{mobi}{language}; | ||
| 945 | $$self{detected}{dictionaryinlanguage} | ||
| 946 | = $mobi->{header}{mobi}{dilanguage}; | ||
| 947 | $$self{detected}{dictionaryoutlanguage} | ||
| 948 | = $mobi->{header}{mobi}{dolanguage}; | ||
| 949 | |||
| 950 | $self->detect_from_mobi_exth(); | ||
| 951 | |||
| 952 | if($$self{raw} && !$$self{nosave}) | ||
| 953 | { | ||
| 954 | $mobi->write_unknown_records(); | ||
| 955 | } | ||
| 956 | |||
| 957 | croak($subname,"(): found no text in '",$$self{file},"'!") | ||
| 958 | unless($mobi->{text}); | ||
| 959 | |||
| 960 | $mobi->fix_html(filename => $htmlname) unless($$self{raw}); | ||
| 961 | |||
| 962 | unless($$self{nosave}) | ||
| 963 | { | ||
| 964 | my $cwd = usedir($self->{dir}); | ||
| 965 | $mobi->write_text($htmlname); | ||
| 966 | $mobi->write_images(); | ||
| 967 | $self->gen_opf(textfile => $htmlname); | ||
| 968 | |||
| 969 | if($$self{tidy}) | ||
| 970 | { | ||
| 971 | debug(1,"Tidying '",$htmlname,"'"); | ||
| 972 | system_tidy_xhtml($htmlname); | ||
| 973 | } | ||
| 974 | chdir($cwd); | ||
| 975 | } | ||
| 976 | return 1; | ||
| 977 | } | ||
| 978 | |||
| 979 | |||
| 980 | =head2 C<unpack_msreader()> | ||
| 981 | |||
| 982 | Unpacks Microsoft Reader (.lit) files | ||
| 983 | |||
| 984 | =cut | ||
| 985 | |||
| 986 | sub unpack_msreader :method | ||
| 987 | { | ||
| 988 | my $self = shift; | ||
| 989 | my $subname = ( caller(0) )[3]; | ||
| 990 | debug(2,"DEBUG[",$subname,"]"); | ||
| 991 | |||
| 992 | my $convertlit = find_convertlit(); | ||
| 993 | my $keys = find_convertlit_keys($$self{file}); | ||
| 994 | |||
| 995 | my $retval = system_convertlit(infile => $$self{file}, | ||
| 996 | keyfile => $keys, | ||
| 997 | dir => $$self{dir}); | ||
| 998 | return $retval; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | |||
| 1002 | =head2 C<unpack_palmdoc()> | ||
| 1003 | |||
| 1004 | Unpacks PalmDoc / AportisDoc (.pdb) files | ||
| 1005 | |||
| 1006 | =cut | ||
| 1007 | |||
| 1008 | sub unpack_palmdoc :method | ||
| 1009 | { | ||
| 1010 | my $self = shift; | ||
| 1011 | my $subname = ( caller(0) )[3]; | ||
| 1012 | debug(2,"DEBUG[",$subname,"]"); | ||
| 1013 | |||
| 1014 | my $filename = $$self{file}; | ||
| 1015 | |||
| 1016 | my $ebook = EBook::Tools->new(); | ||
| 1017 | my $pdb = EBook::Tools::PalmDoc->new(); | ||
| 1018 | my ($outfile,$bookmarkfile,$fh); | ||
| 1019 | my %bookmarks; | ||
| 1020 | $bookmarkfile = $self->filebase . "-bookmarks.txt"; | ||
| 1021 | |||
| 1022 | $pdb->Load($filename); | ||
| 1023 | debug(2,"DEBUG: PalmDoc Name: ",$pdb->{'name'}); | ||
| 1024 | debug(2,"DEBUG: PalmDoc Version: ",$pdb->{'version'}); | ||
| 1025 | debug(2,"DEBUG: PalmDoc Type: ",$pdb->{'type'}); | ||
| 1026 | debug(2,"DEBUG: PalmDoc Creator: ",$pdb->{'creator'}); | ||
| 1027 | |||
| 1028 | $$self{title} ||= $pdb->{'name'}; | ||
| 1029 | $$self{author} ||= 'Unknown Author'; | ||
| 1030 | $$self{opffile} ||= $$self{title} . ".opf"; | ||
| 1031 | |||
| 1032 | unless($$self{nosave}) | ||
| 1033 | { | ||
| 1034 | usedir($self->{dir}); | ||
| 1035 | if($$self{htmlconvert}) | ||
| 1036 | { | ||
| 1037 | $outfile = $self->filebase . ".html"; | ||
| 1038 | open($fh,'>:utf8',$outfile) | ||
| 1039 | or croak($subname,"(): unable to open '",$outfile, | ||
| 1040 | "' for writing!\n"); | ||
| 1041 | print {*$fh} $pdb->html; | ||
| 1042 | close($fh) | ||
| 1043 | or croak("Failed to close '",$outfile,"'!"); | ||
| 1044 | } | ||
| 1045 | else | ||
| 1046 | { | ||
| 1047 | $outfile = $self->filebase . ".txt"; | ||
| 1048 | open($fh,">:raw",$outfile) | ||
| 1049 | or croak("Failed to open '",$outfile,"' for writing!"); | ||
| 1050 | print {*$fh} $pdb->text; | ||
| 1051 | close($fh) | ||
| 1052 | or croak("Failed to close '",$outfile,"'!"); | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | open($fh,">:raw",$bookmarkfile) | ||
| 1056 | or croak("Failed to open '",$bookmarkfile,"' for writing!"); | ||
| 1057 | %bookmarks = $pdb->bookmarks; | ||
| 1058 | if(%bookmarks) | ||
| 1059 | { | ||
| 1060 | foreach my $offset (sort {$a <=> $b} keys %bookmarks) | ||
| 1061 | { | ||
| 1062 | print {*$fh} $offset,"\t",$bookmarks{$offset},"\n"; | ||
| 1063 | } | ||
| 1064 | close($fh) | ||
| 1065 | or croak("Failed to close '",$bookmarkfile,"'!"); | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | $ebook->init_blank(opffile => $$self{opffile}, | ||
| 1069 | title => $$self{title}, | ||
| 1070 | author => $$self{author}); | ||
| 1071 | $ebook->add_document($outfile,'text-main'); | ||
| 1072 | $ebook->save; | ||
| 1073 | } | ||
| 1074 | return $$self{opffile}; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | |||
| 1078 | =head2 C<unpack_zip()> | ||
| 1079 | |||
| 1080 | Unpacks Zip archives (including ePub files). | ||
| 1081 | |||
| 1082 | =cut | ||
| 1083 | |||
| 1084 | sub unpack_zip :method | ||
| 1085 | { | ||
| 1086 | my $self = shift; | ||
| 1087 | my $subname = ( caller(0) )[3]; | ||
| 1088 | debug(2,"DEBUG[",$subname,"]"); | ||
| 1089 | |||
| 1090 | unless($self->{nosave}) | ||
| 1091 | { | ||
| 1092 | my $cwd = usedir($self->{dir}); | ||
| 1093 | my $zip = Archive::Zip->new(); | ||
| 1094 | my $status = $zip->read($cwd.'/'.$self->{file}); | ||
| 1095 | if ($status != AZ_OK) | ||
| 1096 | { | ||
| 1097 | croak($subname,'(): error while parsing zip file "',$self->{file},'" (',$status,')!'); | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | $status = $zip->extractTree(undef); | ||
| 1101 | if ($status != AZ_OK) | ||
| 1102 | { | ||
| 1103 | croak($subname,'(): error while extracting zip file "',$self->{file},'" (',$status,')!'); | ||
| 1104 | } | ||
| 1105 | chdir($cwd); | ||
| 1106 | } | ||
| 1107 | return 1; | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | |||
| 1111 | ########## PROCEDURES ########## | ||
| 1112 | |||
| 1113 | # No procedures | ||
| 1114 | |||
| 1115 | |||
| 1116 | ########## END CODE ########## | ||
| 1117 | |||
| 1118 | =head1 BUGS AND LIMITATIONS | ||
| 1119 | |||
| 1120 | =over | ||
| 1121 | |||
| 1122 | =item * DRM isn't handled. Infrastructure to support this via an | ||
| 1123 | external plug-in module may eventually be built, but it will never | ||
| 1124 | become part of the main module for legal reasons. | ||
| 1125 | |||
| 1126 | =item * Unit tests are incomplete | ||
| 1127 | |||
| 1128 | =item * Documentation is incomplete. Accessors in particular could | ||
| 1129 | use some cleaning up. | ||
| 1130 | |||
| 1131 | =item * Need to implement setter methods for object attributes | ||
| 1132 | |||
| 1133 | =item * Import/extraction/unpacking is currently limited to PalmDoc, | ||
| 1134 | Mobipocket, and eReader. Extraction from Microsoft Reader (.lit) and | ||
| 1135 | ePub is also eventually planned. Other formats may follow from there. | ||
| 1136 | |||
| 1137 | =back | ||
| 1138 | |||
| 1139 | =head1 AUTHOR | ||
| 1140 | |||
| 1141 | Zed Pobre <zed@debian.org> | ||
| 1142 | |||
| 1143 | =head1 LICENSE AND COPYRIGHT | ||
| 1144 | |||
| 1145 | Copyright 2008 Zed Pobre | ||
| 1146 | |||
| 1147 | Licensed to the public under the terms of the GNU GPL, version 2 | ||
| 1148 | |||
| 1149 | =cut | ||
| 1150 | |||
| 1151 | 1; | ||
| 1152 | __END__ | ||