File Coverage

blib/lib/EBook/Ishmael/EBook.pm
Criterion Covered Total %
statement 75 78 96.1
branch 8 10 80.0
condition 1 3 33.3
subroutine 21 21 100.0
pod 2 2 100.0
total 107 114 93.8


line stmt bran cond sub pod time code
1             package EBook::Ishmael::EBook;
2 17     17   1755484 use 5.016;
  17         68  
3             our $VERSION = '2.03';
4 17     17   98 use strict;
  17         38  
  17         557  
5 17     17   78 use warnings;
  17         30  
  17         1248  
6              
7 17     17   126 use Exporter 'import';
  17         80  
  17         2234  
8             our @EXPORT = qw(&ebook_id %EBOOK_FORMATS);
9              
10 17     17   8578 use EBook::Ishmael::EBook::CB7;
  17         164  
  17         1080  
11 17     17   8810 use EBook::Ishmael::EBook::CBR;
  17         52  
  17         938  
12 17     17   8071 use EBook::Ishmael::EBook::CBZ;
  17         50  
  17         606  
13 17     17   8695 use EBook::Ishmael::EBook::CHM;
  17         62  
  17         846  
14 17     17   9827 use EBook::Ishmael::EBook::Epub;
  17         53  
  17         814  
15 17     17   11188 use EBook::Ishmael::EBook::FictionBook2;
  17         85  
  17         780  
16 17     17   8578 use EBook::Ishmael::EBook::HTML;
  17         55  
  17         729  
17 17     17   8019 use EBook::Ishmael::EBook::KF8;
  17         84  
  17         771  
18 17     17   140 use EBook::Ishmael::EBook::Mobi;
  17         38  
  17         408  
19 17     17   9870 use EBook::Ishmael::EBook::PalmDoc;
  17         78  
  17         806  
20 17     17   9443 use EBook::Ishmael::EBook::PDF;
  17         72  
  17         869  
21 17     17   8731 use EBook::Ishmael::EBook::Text;
  17         66  
  17         710  
22 17     17   8491 use EBook::Ishmael::EBook::XHTML;
  17         61  
  17         742  
23 17     17   10172 use EBook::Ishmael::EBook::Zip;
  17         97  
  17         865  
24 17     17   9002 use EBook::Ishmael::EBook::zTXT;
  17         57  
  17         7685  
25              
26             our %EBOOK_FORMATS = map { lc $_ => "EBook::Ishmael::EBook::$_" } qw(
27             CB7 CBR CBZ CHM Epub FictionBook2 HTML KF8 Mobi PalmDoc PDF Text XHTML Zip
28             zTXT
29             );
30              
31             sub ebook_id {
32              
33 126     126 1 285 my $file = shift;
34              
35 126 50       8510 open my $fh, '<', $file
36             or die "Failed to open $file for reading: $!\n";
37 126         555 binmode $fh;
38              
39 126         1362 for my $f (
40             # Make sure text is last
41             sort {
42 5409 100       9155 return 1 if $a eq 'text';
43 4924 100       8329 return -1 if $b eq 'text';
44 4658         7013 return $a cmp $b;
45             } keys %EBOOK_FORMATS
46             ) {
47              
48 1181         6865 seek $fh, 0, 0;
49              
50 1181 100       10325 if ($EBOOK_FORMATS{ $f }->heuristic($file, $fh)) {
51 126         1794 close $fh;
52 126         1057 return $f;
53             }
54              
55             }
56              
57 0         0 close $fh;
58 0         0 return undef;
59              
60             }
61              
62             sub new {
63              
64 115     115 1 2422586 my $class = shift;
65 115         258 my $file = shift;
66 115   33     625 my $type = shift // ebook_id($file);
67 115         432 my $enc = shift;
68 115         293 my $net = shift;
69              
70 115 50       406 if (not defined $type) {
71 0         0 die "Could not identify $file format\n";
72             }
73              
74 115         714 my $obj = $EBOOK_FORMATS{ $type }->new($file, $enc, $net);
75              
76 115         545 return $obj;
77              
78             }
79              
80             1;
81              
82             =head1 NAME
83              
84             EBook::Ishmael::EBook - Interface for processing ebook documents
85              
86             =head1 SYNOPSIS
87              
88             use App::Ishmael::EBook;
89              
90             my $ebook = App::Ishmael::EBook->new($file);
91              
92             =head1 DESCRIPTION
93              
94             B is a module used by L to read ebook files.
95             If you are looking for user documentation, you should consult the L
96             manual (this is developer documentation).
97              
98             This page will not only detail B's methods, but some of
99             the methods of the various specific ebook modules that this module uses, as they
100             all (mostly) share the same API.
101              
102             =head1 METHODS
103              
104             =head2 $e = EBook::Ishmael::EBook->new($file, $type, $enc, $net)
105              
106             Reads C<$file> and returns some ebook object, the exact class will depend the on
107             the format of C<$file> or C<$type>. C<$type> is the name of the format you would
108             like to read C<$file> as. If not specified, C will try to identify
109             C<$file>'s format automatically via a series of heuristics. C<$enc> is an
110             optional argument specify the character encoding to read the ebook format as, if
111             the ebook format supports user-specified encodings. C<$net> is an optional
112             boolean argument that determines whether to allow or disallow performing
113             network operations when reading an ebook.
114              
115             =head2 $html = $e->html([$out])
116              
117             Dumps the ebook's HTML-ified contents. Contents will be written to C<$out>, if
118             provided, otherwise it will be returned as a string.
119              
120             =head2 $meta = $e->meta()
121              
122             Returns a hash ref of the ebook object's metadata.
123              
124             =head2 $raw = $e->raw([$out])
125              
126             Dumps the ebook's raw, unformatted text contents.
127              
128             =head2 $bool = $e->has_cover()
129              
130             Returns bool of whether the ebook has a cover image or not.
131              
132             =head2 ($cover, $format) = $e->cover()
133              
134             Dumps the ebook's cover image data. Returns C is there is no cover.
135              
136             =head2 $n = $e->image_num()
137              
138             Returns the number of images in the ebook.
139              
140             =head2 ($img, $format) = $e->image($n)
141              
142             Returns a scalar ref C<$img> of image data from image C<$n> (starting from
143             C<0>). Returns C if the image is not available.
144              
145             =head1 SUBROUTINES
146              
147             =head2 $type = ebook_id($file)
148              
149             Identifies the ebook format of C<$file> using a series of heuristics. If
150             C<$file> could not be identified, returns C.
151              
152             =head1 EXPORTED VARIABLES
153              
154             =head2 %EBOOK_FORMATS
155              
156             Hash of ebook formats and their respective class.
157              
158             =head1 AUTHOR
159              
160             Written by Samuel Young, Esamyoung12788@gmail.comE.
161              
162             This project's source can be found on its
163             L. Comments and pull
164             requests are welcome!
165              
166             =head1 COPYRIGHT
167              
168             Copyright (C) 2025-2026 Samuel Young
169              
170             This program is free software: you can redistribute it and/or modify
171             it under the terms of the GNU General Public License as published by
172             the Free Software Foundation, either version 3 of the License, or
173             (at your option) any later version.
174              
175             =head1 SEE ALSO
176              
177             L,
178              
179             =cut