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   1856398 use 5.016;
  17         75  
3             our $VERSION = '2.01';
4 17     17   133 use strict;
  17         33  
  17         646  
5 17     17   91 use warnings;
  17         30  
  17         1185  
6              
7 17     17   144 use Exporter 'import';
  17         70  
  17         1314  
8             our @EXPORT = qw(&ebook_id %EBOOK_FORMATS);
9              
10 17     17   8719 use EBook::Ishmael::EBook::CB7;
  17         103  
  17         956  
11 17     17   9648 use EBook::Ishmael::EBook::CBR;
  17         79  
  17         947  
12 17     17   9333 use EBook::Ishmael::EBook::CBZ;
  17         61  
  17         730  
13 17     17   9748 use EBook::Ishmael::EBook::CHM;
  17         63  
  17         977  
14 17     17   10275 use EBook::Ishmael::EBook::Epub;
  17         95  
  17         1338  
15 17     17   12531 use EBook::Ishmael::EBook::FictionBook2;
  17         64  
  17         1025  
16 17     17   10034 use EBook::Ishmael::EBook::HTML;
  17         67  
  17         891  
17 17     17   9305 use EBook::Ishmael::EBook::KF8;
  17         83  
  17         834  
18 17     17   149 use EBook::Ishmael::EBook::Mobi;
  17         37  
  17         465  
19 17     17   10316 use EBook::Ishmael::EBook::PalmDoc;
  17         67  
  17         748  
20 17     17   9649 use EBook::Ishmael::EBook::PDF;
  17         89  
  17         1004  
21 17     17   9694 use EBook::Ishmael::EBook::Text;
  17         65  
  17         720  
22 17     17   8774 use EBook::Ishmael::EBook::XHTML;
  17         67  
  17         708  
23 17     17   9182 use EBook::Ishmael::EBook::Zip;
  17         64  
  17         856  
24 17     17   9699 use EBook::Ishmael::EBook::zTXT;
  17         90  
  17         7993  
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 240 my $file = shift;
34              
35 126 50       8018 open my $fh, '<', $file
36             or die "Failed to open $file for reading: $!\n";
37 126         520 binmode $fh;
38              
39 126         1302 for my $f (
40             # Make sure text is last
41             sort {
42 5392 100       9775 return 1 if $a eq 'text';
43 5265 100       10108 return -1 if $b eq 'text';
44 4894         7682 return $a cmp $b;
45             } keys %EBOOK_FORMATS
46             ) {
47              
48 1181         7090 seek $fh, 0, 0;
49              
50 1181 100       10720 if ($EBOOK_FORMATS{ $f }->heuristic($file, $fh)) {
51 126         1910 close $fh;
52 126         1106 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 2725744 my $class = shift;
65 115         331 my $file = shift;
66 115   33     597 my $type = shift // ebook_id($file);
67 115         467 my $enc = shift;
68 115         201 my $net = shift;
69              
70 115 50       344 if (not defined $type) {
71 0         0 die "Could not identify $file format\n";
72             }
73              
74 115         721 my $obj = $EBOOK_FORMATS{ $type }->new($file, $enc, $net);
75              
76 115         634 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 = $e->cover([$out])
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 = $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