blib/lib/EBook/Ishmael/EBook.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 80.0% 8 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod 100% 2 / 2 totaltotal 92.5% 112 / 121 CC 8 SCAR 8.5
CC
8
Cov
93%
ebook_id
6.1
new
3.2
BEGIN
1.0
CRAP 2.3
« blib/lib/EBook/Ishmael/EBook/XHTML.pm ↑ Summary blib/lib/EBook/Ishmael/HTML.pm »
1 package EBook::Ishmael::EBook;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.04';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 our @EXPORT = qw(&ebook_id %EBOOK_FORMATS);
9  
10 17 ▶use EBook::Ishmael::EBook::CB7;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use EBook::Ishmael::EBook::CBR;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use EBook::Ishmael::EBook::CBZ;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use EBook::Ishmael::EBook::CHM;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use EBook::Ishmael::EBook::Epub;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use EBook::Ishmael::EBook::FictionBook2;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use EBook::Ishmael::EBook::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use EBook::Ishmael::EBook::KF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use EBook::Ishmael::EBook::Mobi;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use EBook::Ishmael::EBook::PalmDoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 ▶use EBook::Ishmael::EBook::PDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 17 ▶use EBook::Ishmael::EBook::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use EBook::Ishmael::EBook::XHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use EBook::Ishmael::EBook::Zip;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use EBook::Ishmael::EBook::zTXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 17 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 ▶ my $file = shift;
Subroutine ebook_id: called CC=6 SCAR=18.1
Pod documented
34  
35 126 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Failed to open $file for reading: $!\n" 0 126 126
36 or die "Failed to open $file for reading: $!\n";
37 126 binmode $fh;
38  
39 126 for my $f (
40 # Make sure text is last
41 sort {
42 4834 ▶ return 1 if $a eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "text" 133 4701 4834
43 4701 ▶ return -1 if $b eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $b eq "text" 145 4556 4701
44 4556 return $a cmp $b;
45 } keys %EBOOK_FORMATS
46 ) {
47  
48 1181 seek $fh, 0, 0;
49  
50 1181 ▶ if ($EBOOK_FORMATS{ $f }->heuristic($file, $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($EBook::Ishmael::EBook::EBOOK_FORMATS{$f}->heuristic($file, $fh)) 126 1055 1181
51 126 close $fh;
52 126 return $f;
53 }
54  
55 }
56  
57 0 close $fh;
58 0 return undef;
59  
60 }
61  
62 sub new {
63  
64 115 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.7
Pod documented
65 115 my $file = shift;
66 115 ▶ my $type = shift // ebook_id($file);
Condition33%1/3
shift() // ebook_id($file)
l!l&&r!l&&!r
01150
MC/DC0%0/2
shift() // ebook_id($file)
shift() ebook_id($file)
Truth table33%1/3
A // B
A= shift()B= ebook_id($file)
ABresult
1X1
011
000
67 115 my $enc = shift;
68 115 my $net = shift;
69  
70 115 ▶ if (not defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 115 115
71 0 die "Could not identify $file format\n";
72 }
73  
74 115 my $obj = $EBOOK_FORMATS{ $type }->new($file, $enc, $net);
75  
76 115 return $obj;
77  
78 }
79  
80 17 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<EBook::Ishmael::EBook> is a module used by L<ishmael> to read ebook files.
95 If you are looking for user documentation, you should consult the L<ishmael>
96 manual (this is developer documentation).
97  
98 This page will not only detail B<EBook::Ishmael::EBook>'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<new()> 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<undef> 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<undef> 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<undef>.
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, E<lt>samyoung12788@gmail.comE<gt>.
161  
162 This project's source can be found on its
163 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. 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<EBook::Ishmael>,
178  
179 =cut
« blib/lib/EBook/Ishmael/EBook/XHTML.pm ↑ Summary blib/lib/EBook/Ishmael/HTML.pm »