blib/lib/EBook/Ishmael.pm

filter: Statementstmt 80.5% 240 / 298 Branchbran 62.7% 74 / 118 Conditioncond 52.6% 10 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 87.5% 35 / 40 Podpod 91.6% 11 / 12 totaltotal 74.1% 370 / 499 CC 73 SCAR 23.9
CC
73
Cov
74%
init
32.0
image
23.5
text
20.0
CRAP 10.9
« blib/lib/EBook/Ishmael/ImageID.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/UTF8.pm »
1 package EBook::Ishmael;
2 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 our $VERSION = '2.04';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Encode qw(find_encoding encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use EBook::Ishmael::EBook;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use EBook::Ishmael::TextBrowserDump;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use EBook::Ishmael::Time qw(format_locale_time format_rfc3339_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 use constant {
22 2 MODE_TEXT => 0,
23 MODE_META => 1,
24 MODE_ID => 2,
25 MODE_HTML => 3,
26 MODE_RAW_TIME => 4,
27 MODE_COVER => 5,
28 MODE_IMAGE => 6,
29 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # TODO: Temporary files are not cleaned up if ishmael is piped into another
32 # program like less
33  
34 # TODO: It would be nice if we had a way to automatically determine an ebook's
35 # encoding...
36  
37 2 $0 =~ s!^.*[/\\]!!;
38  
39 2 my $PRGNAM = 'ishmael';
40 2 my $PRGVER = $VERSION;
41  
42 2 my $HELP = <<"HERE";
43 $PRGNAM - $PRGVER
44  
45 Usage:
46 $0 [options] file [output]
47  
48 Options:
49 -d|--dumper=<dumper> Specify dumper to use for formatting text
50 -e|--encoding=<enc> Print text output in specified encoding
51 -I|--file-encoding=<enc> Specify ebook character encoding
52 -f|--format=<format> Specify ebook format
53 -w|--width=<width> Specify output line width
54 -N|--no-network Disable fetching remove resources
55 -t|--text Dump formatted ebook text (default)
56 -H|--html Dump ebook HTML
57 -c|--cover Dump ebook cover image
58 -g|--image Dump ebook images
59 -i|--identify Identify ebook format
60 -m|--metadata[=<form>] Print ebook metadata
61 -r|--raw Dump the raw, unformatted ebook text
62  
63 -h|--help Print help message
64 -v|--version Print version/copyright info
65 HERE
66  
67 2 my $VERSION_MSG = <<"HERE";
68 $PRGNAM - $PRGVER
69  
70 Copyright (C) 2025-2026 Samuel Young
71  
72 This program is free software: you can redistribute it and/or modify
73 it under the terms of the GNU General Public License as published by
74 the Free Software Foundation, either version 3 of the License, or
75 (at your option) any later version.
76 HERE
77  
78 2 my $STDIN = '-';
79 2 my $STDOUT = '-';
80  
81 2 my %FORMAT_ALTS = (
82 'fb2' => 'fictionbook2',
83 'azw' => 'mobi',
84 'azw3' => 'kf8',
85 );
86  
87 2 my %META_MODES = map { $_ => 1 } qw(
88 ishmael json xml
89 );
90  
91 # Replace characters that cannot be encoded with empty strings.
92 0 ▶my $ENC_SUBST = sub { q[] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
93  
94 # If reading from stdin, write stdin to temporary file and dump that.
95 sub _get_in_path {
96  
97 115 ▶ my $file = shift;
Subroutine _get_in_path: called CC=2 SCAR=12.4
98  
99 115 ▶ if ($file eq $STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $STDIN) { } 0 115 115
100 0 return do {
101 0 my ($h, $p) = tempfile(UNLINK => 1);
102 0 binmode $h;
103 0 print { $h } do { local $/; <STDIN> };
104 0 close $h;
105 0 $p;
106 };
107 } else {
108 115 return $file;
109 }
110  
111 }
112  
113 sub _get_out {
114  
115 88 ▶ my $file = shift;
Subroutine _get_out: called CC=3 SCAR=12.0
116  
117 88 ▶ if ($file ne $STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file ne $STDOUT) { } 88 0 88
118 88 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Failed to open $file for writing: $!\n" 0 88 88
119 or die "Failed to open $file for writing: $!\n";
120 88 return $fh;
121 } else {
122 0 return *STDOUT;
123 }
124  
125 }
126  
127 sub init {
128  
129 115 ▶ my $class = shift;
Subroutine init: called CC=15 SCAR=34.7
Pod documented
130  
131 my $self = {
132 Ebook => undef,
133 Mode => MODE_TEXT,
134 Dumper => $ENV{ISHMAEL_DUMPER},
135 Encode => $ENV{ISHMAEL_ENCODING},
136 115 FileEnc => undef,
137 Format => undef,
138 Output => undef,
139 Width => 80,
140 Meta => undef,
141 Network => 1,
142 };
143  
144 115 Getopt::Long::config('bundling');
145 GetOptions(
146 'dumper|d=s' => \$self->{Dumper},
147 'encoding|e=s' => \$self->{Encode},
148 'file-encoding|I=s' => \$self->{FileEnc},
149 'format|f=s' => \$self->{Format},
150 'width|w=i' => \$self->{Width},
151 115 ▶ 'no-network|N' => sub { $self->{Network} = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
152 0 ▶ 'text|t' => sub { $self->{Mode} = MODE_TEXT },
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 22 ▶ 'html|H' => sub { $self->{Mode} = MODE_HTML },
Subroutine __ANON__: called CC=1 SCAR=0.0
154 11 ▶ 'cover|c' => sub { $self->{Mode} = MODE_COVER },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 11 ▶ 'image|g' => sub { $self->{Mode} = MODE_IMAGE },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 11 ▶ 'identify|i' => sub { $self->{Mode} = MODE_ID },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 'metadata|m:s' => sub {
158 # Some DWIMery that if the given argument is not a valid metadata
159 # format, assume the user meant for it be a file argument and put
160 # it back into @ARGV.
161 33 ▶ $self->{Mode} = MODE_META;
Subroutine __ANON__: called CC=4 SCAR=17.9
162 33 ▶ if (!$_[1] or exists $META_MODES{ lc $_[1] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[1] or exists $META_MODES{lc $_[1]}) { } 33 0 33
Condition33%1/3
not $_[1] or exists $META_MODES{lc $_[1]}
l!l&&r!l&&!r
0330
MC/DC0%0/2
not $_[1] or exists $META_MODES{lc $_[1]}
not $_[1] exists $META_MODES{lc $_[1]}
Truth table33%1/3
not(A) or B
A= not $_[1]B= exists $META_MODES{lc $_[1]}
ABresult
1X1
011
000
163 33 ▶ $self->{Meta} = lc $_[1] || 'ishmael';
Condition50%1/2
lc $_[1] || 'ishmael'
l!l
330
MC/DC0%0/1
lc $_[1] || 'ishmael'
lc $_[1]
Truth table50%1/2
A
A= lc $_[1]
Aresult
11
00
164 } else {
165 0 $self->{Meta} = 'ishmael';
166 0 unshift @ARGV, $_[1];
167 }
168 },
169 27 ▶ 'raw|r' => sub { $self->{Mode} = MODE_RAW_TIME },
Subroutine __ANON__: called CC=1 SCAR=0.0
170 0 ▶ 'help|h' => sub { print $HELP; exit 0; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 ▶ 'version|v' => sub { print $VERSION_MSG; exit 0; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 115 ▶ ) or die "Error in command line arguments\n$HELP";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("dumper|d=s", \$self->{"Dumper"}, "encoding|e=s", \$self->{"Encode"}, "file-encoding|I=s", \$self->{"FileEnc"}, "format|f=s", \$self->{"Format"}, "width|w=i", \$self->{"Width"}, "no-network|N", sub { $self->{"Network"} = 0; } , "text|t", sub { $self->{"Mode"} = 0; } , "html|H", sub { $self->{"Mode"} = 3; } , "cover|c", sub { $self->{"Mode"} = 5; } , "image|g", sub { $self->{"Mode"} = 6; } , "identify|i", sub { $self->{"Mode"} = 2; } , "metadata|m:s", sub { $self->{"Mode"} = 1; if (not $_[1] or exists $META_MODES{lc $_[1]}) { $self->{"Meta"} = lc $_[1] || "ishmael"; } else { $self->{"Meta"} = "ishmael"; unshift @ARGV, $_[1]; } ; } , "raw|r", sub { $self->{"Mode"} = 4; } , "help|h", sub { print $HELP; exit 0; } , "version|v", sub { print $VERSION_MSG; exit 0; } ) or die "Error in command line arguments\n$HELP" 0 115 115
173  
174 115 ▶ $self->{Ebook} = shift @ARGV or die $HELP;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Ebook"} = shift @ARGV or die $HELP 0 115 115
175 115 $self->{Output} = shift @ARGV;
176  
177 115 $self->{Ebook} = _get_in_path($self->{Ebook});
178  
179 115 ▶ if ($self->{Mode} == MODE_COVER) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'Mode'} == 5) { } 11 104 115
elsif ($self->{'Mode'} == 6) { } 11 93 104
180 11 ▶ $self->{Output} //= (fileparse($self->{Ebook}, qr/\.[^.]*/))[0] . '.-';
Condition50%1/2
$self->{"Output"} //= (fileparse($self->{"Ebook"}, qr/\.[^.]*/u))[0] . '.-'
l!l
110
MC/DC0%0/1
$self->{"Output"} //= (fileparse($self->{"Ebook"}, qr/\.[^.]*/u))[0] . '.-'
$self->{"Output"}
Truth table50%1/2
A
A= $self->{"Output"}
Aresult
11
00
181 } elsif ($self->{Mode} == MODE_IMAGE) {
182 11 ▶ $self->{Output} //= (fileparse($self->{Ebook}, qr/\.[^.]*/))[0];
Condition33%1/3
$self->{"Output"} //= (fileparse($self->{"Ebook"}, qr/\.[^.]*/u))[0]
l!l&&r!l&&!r
1100
MC/DC0%0/2
$self->{"Output"} //= (fileparse($self->{"Ebook"}, qr/\.[^.]*/u))[0]
$self->{"Output"} (fileparse($self->{"Ebook"}, qr/\.[^.]*/u))[0]
Truth table33%1/3
A //= B
A= $self->{"Output"}B= (fileparse($self->{"Ebook"}, qr/\.[^.]*/u))[0]
ABresult
1X1
011
000
183 } else {
184 93 ▶ $self->{Output} //= $STDOUT;
Condition66%2/3
$self->{"Output"} //= $STDOUT
l!l&&r!l&&!r
82110
MC/DC0%0/2
$self->{"Output"} //= $STDOUT
$self->{"Output"} $STDOUT
Truth table66%2/3
A //= B
A= $self->{"Output"}B= $STDOUT
ABresult
1X1
011
000
185 }
186  
187 115 ▶ if (defined $self->{Format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Format"}) 0 115 115
188  
189 0 $self->{Format} = lc $self->{Format};
190  
191 0 ▶ if (exists $FORMAT_ALTS{ $self->{Format} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $FORMAT_ALTS{$self->{"Format"}}) 0 0 0
192 0 $self->{Format} = $FORMAT_ALTS{ $self->{Format} };
193 }
194  
195 0 ▶ unless (exists $EBOOK_FORMATS{ $self->{Format} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $EBook::Ishmael::EBOOK_FORMATS{$self->{"Format"}}) 0 0 0
196 0 die "$self->{Format} is not a recognized ebook format\n";
197 }
198  
199 }
200  
201 115 ▶ if (defined $self->{Encode} and not defined find_encoding($self->{Encode})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Encode"} and not defined &find_encoding($self->{"Encode"})) 0 115 115
Condition66%2/3
defined $self->{"Encode"} and not defined &find_encoding($self->{"Encode"})
!ll&&!rl&&r
93220
MC/DC0%0/2
defined $self->{"Encode"} and not defined &find_encoding($self->{"Encode"})
defined $self->{"Encode"} not defined &find_encoding($self->{"Encode"})
Truth table66%2/3
A and not(B)
A= defined $self->{"Encode"}B= not defined &find_encoding($self->{"Encode"})
ABresult
0X0
100
111
202 0 die "'$self->{Encode}' is an invalid character encoding\n";
203 }
204  
205 115 ▶ if (defined $self->{FileEnc} and not defined find_encoding($self->{FileEnc})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"FileEnc"} and not defined &find_encoding($self->{"FileEnc"})) 0 115 115
Condition66%2/3
defined $self->{"FileEnc"} and not defined &find_encoding($self->{"FileEnc"})
!ll&&!rl&&r
11050
MC/DC0%0/2
defined $self->{"FileEnc"} and not defined &find_encoding($self->{"FileEnc"})
defined $self->{"FileEnc"} not defined &find_encoding($self->{"FileEnc"})
Truth table66%2/3
A and not(B)
A= defined $self->{"FileEnc"}B= not defined &find_encoding($self->{"FileEnc"})
ABresult
0X0
100
111
206 0 die "'$self->{FileEnc}' is an invalid character encoding\n";
207 }
208  
209 115 bless $self, $class;
210  
211 115 return $self;
212  
213 }
214  
215 sub text {
216  
217 0 ▶ my $self = shift;
Subroutine text: not called CC=4 SCAR=30.0
Pod documented
218  
219 my $ebook = EBook::Ishmael::EBook->new(
220 $self->{Ebook},
221 $self->{Format},
222 $self->{FileEnc},
223 $self->{Network},
224 0 );
225  
226 0 my $tmp = do {
227 0 my ($tf, $tp) = tempfile(UNLINK => 1);
228 0 close $tf;
229 0 $tp;
230 };
231  
232 0 $ebook->html($tmp);
233  
234 0 my $oh = _get_out($self->{Output});
235  
236 0 ▶ unless (defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 0 0 0
237 0 binmode $oh, ':utf8';
238 }
239  
240 my $dump = browser_dump(
241 $tmp,
242 {
243 browser => $self->{Dumper},
244 width => $self->{Width},
245 }
246 0 );
247  
248 0 ▶ if (defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Encode'}) { } 0 0 0
249 0 print { $oh } encode($self->{Encode}, $dump, $ENC_SUBST);
250 } else {
251 0 print { $oh } $dump;
252 }
253  
254 0 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 0 0 0
255  
256 0 1;
257  
258 }
259  
260 sub meta {
261  
262 33 ▶ my $self = shift;
Subroutine meta: called CC=4 SCAR=14.0
Pod documented
263  
264 33 ▶ if ($self->{Meta} eq 'ishmael') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'Meta'} eq 'ishmael') { } 11 22 33
elsif ($self->{'Meta'} eq 'json') { } 11 11 22
elsif ($self->{'Meta'} eq 'xml') { } 11 0 11
265 11 $self->meta_ishmael;
266 } elsif ($self->{Meta} eq 'json') {
267 11 $self->meta_json;
268 } elsif ($self->{Meta} eq 'xml') {
269 11 $self->meta_xml;
270 } else {
271 0 die "'$self->{Meta}' is not a valid metadata format\n";
272 }
273  
274 33 1;
275  
276 }
277  
278 sub meta_ishmael {
279  
280 11 ▶ my $self = shift;
Subroutine meta_ishmael: called CC=7 SCAR=19.5
Pod documented
281  
282 my $ebook = EBook::Ishmael::EBook->new(
283 $self->{Ebook},
284 $self->{Format},
285 $self->{FileEnc},
286 $self->{Network},
287 11 );
288  
289 11 my %meta = %{ $ebook->metadata->hash };
290 11 ▶ if (defined $meta{Created}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta{"Created"}) 6 5 11
291 6 $meta{ Created } = format_locale_time($meta{Created});
292 }
293 11 ▶ if (defined $meta{Modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta{"Modified"}) 7 4 11
294 7 $meta{Modified} = format_locale_time($meta{Modified});
295 }
296  
297 11 my $oh = _get_out($self->{Output});
298 11 binmode $oh, ':utf8';
299  
300 11 my $klen = max(map { length } keys %meta) + 1;
301 11 for my $k (sort keys %meta) {
302 54 ▶ next if not defined $meta{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta{$k} 0 54 54
303 54 ▶ if (ref $meta{ $k } eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $meta{$k} eq 'ARRAY') { } 15 39 54
304 15 printf { $oh } "%-*s %s\n", $klen, "$k:", join ", ", @{ $meta{$k} };
305 } else {
306 39 printf { $oh } "%-*s %s\n", $klen, "$k:", $meta{$k};
307 }
308 }
309  
310 11 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 11 0 11
311  
312 11 1;
313  
314 }
315  
316 sub meta_json {
317  
318 11 ▶ my $self = shift;
Subroutine meta_json: called CC=4 SCAR=13.9
Pod documented
319  
320 my $ebook = EBook::Ishmael::EBook->new(
321 $self->{Ebook},
322 $self->{Format},
323 $self->{FileEnc},
324 $self->{Network},
325 11 );
326  
327 11 my $meta = $ebook->metadata->hash;
328 11 ▶ if (defined $meta->{ Created }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Created"}) 6 5 11
329 6 $meta->{ Created } = format_rfc3339_time($meta->{ Created });
330 }
331 11 ▶ if (defined $meta->{ Modified }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Modified"}) 7 4 11
332 7 $meta->{ Modified } = format_rfc3339_time($meta->{ Modified });
333 }
334  
335 11 my $oh = _get_out($self->{Output});
336  
337 11 my $json = JSON::PP->new->utf8->pretty->canonical;
338 11 print { $oh } $json->encode($meta);
339  
340 11 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 11 0 11
341  
342 11 1;
343  
344 }
345  
346 sub meta_xml {
347  
348 11 ▶ my $self = shift;
Subroutine meta_xml: called CC=8 SCAR=20.8
Pod documented
349  
350 my $ebook = EBook::Ishmael::EBook->new(
351 $self->{Ebook},
352 $self->{Format},
353 $self->{FileEnc},
354 $self->{Network},
355 11 );
356  
357 11 my $meta = $ebook->metadata->hash;
358 11 ▶ if (defined $meta->{Created}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Created"}) 6 5 11
359 6 $meta->{Created} = format_rfc3339_time($meta->{Created});
360 }
361 11 ▶ if (defined $meta->{ Modified }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"Modified"}) 7 4 11
362 7 $meta->{ Modified } = format_rfc3339_time($meta->{Modified});
363 }
364  
365 11 my $oh = _get_out($self->{Output});
366  
367 11 my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
368 11 my $root = XML::LibXML::Element->new('ishmael');
369 11 $dom->setDocumentElement($root);
370 11 $root->setAttribute('version', $PRGVER);
371 11 my $metan = $root->appendChild(
372 XML::LibXML::Element->new('metadata')
373 );
374  
375 11 for my $k (sort keys %$meta) {
376 54 ▶ next if not defined $meta->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta->{$k} 0 54 54
377 54 my $n = $metan->appendChild(
378 XML::LibXML::Element->new(lc $k)
379 );
380 54 ▶ if (ref $meta->{ $k } eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $meta->{$k} eq 'ARRAY') { } 15 39 54
381 15 for my $i (@{ $meta->{$k} }) {
382 15 my $in = $n->appendChild(
383 XML::LibXML::Element->new('item')
384 );
385 15 $in->appendChild(
386 XML::LibXML::Text->new($i)
387 );
388 }
389 } else {
390 $n->appendChild(
391 39 XML::LibXML::Text->new($meta->{ $k })
392 );
393 }
394 }
395  
396 11 $dom->toFH($oh, 1);
397  
398 11 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 11 0 11
399  
400 11 1;
401  
402 }
403  
404 sub id {
405  
406 11 ▶ my $self = shift;
Subroutine id: called CC=2 SCAR=7.0
Pod documented
407  
408 11 my $id = ebook_id($self->{Ebook});
409  
410 11 ▶ say defined $id ? $id : "Could not identify format for $self->{Ebook}";
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 11 0 11
411  
412 11 1;
413  
414 }
415  
416 sub html {
417  
418 22 ▶ my $self = shift;
Subroutine html: called CC=4 SCAR=13.9
Pod documented
419  
420 my $ebook = EBook::Ishmael::EBook->new(
421 $self->{Ebook},
422 $self->{Format},
423 $self->{FileEnc},
424 $self->{Network},
425 22 );
426  
427 22 my $oh = _get_out($self->{Output});
428  
429 22 ▶ unless (defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 11 11 22
430 11 binmode $oh, ':utf8';
431 }
432  
433 22 my $html = $ebook->html;
434  
435 22 ▶ if (defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Encode'}) { } 11 11 22
436 11 say { $oh } encode($self->{Encode}, $html, $ENC_SUBST);
437 } else {
438 11 say { $oh } $html;
439 }
440  
441 22 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 22 0 22
442  
443 22 1;
444  
445 }
446  
447 sub raw {
448  
449 27 ▶ my $self = shift;
Subroutine raw: called CC=4 SCAR=13.9
Pod documented
450  
451 my $ebook = EBook::Ishmael::EBook->new(
452 $self->{Ebook},
453 $self->{Format},
454 $self->{FileEnc},
455 $self->{Network},
456 27 );
457  
458 27 my $oh = _get_out($self->{Output});
459  
460 27 ▶ unless (defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 16 11 27
461 16 binmode $oh, ':utf8';
462 }
463  
464 27 my $raw = $ebook->raw;
465  
466 27 ▶ if (defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Encode'}) { } 11 16 27
467 11 say { $oh } encode($self->{Encode}, $raw, $ENC_SUBST);
468 } else {
469 16 say { $oh } $raw;
470 }
471  
472 27 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 27 0 27
473  
474 27 1;
475  
476 }
477  
478 sub cover {
479  
480 11 ▶ my $self = shift;
Subroutine cover: called CC=4 SCAR=14.1
Pod documented
481  
482 my $ebook = EBook::Ishmael::EBook->new(
483 $self->{Ebook},
484 $self->{Format},
485 $self->{FileEnc},
486 $self->{Network},
487 11 );
488  
489 11 ▶ unless ($ebook->has_cover) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ebook->has_cover) 5 6 11
490 5 say "$self->{Ebook} does not have a cover";
491 5 return;
492 }
493  
494 6 my ($cover, $format) = $ebook->cover;
495 6 ▶ if (not defined $cover) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cover) 0 6 6
496 0 say "$self->{Ebook} does not have a cover";
497 0 return;
498 }
499  
500 6 $self->{Output} =~ s/\.-$/.$format/;
501  
502 6 my $oh = _get_out($self->{Output});
503 6 binmode $oh;
504 6 print { $oh } $cover;
505 6 ▶ close $oh unless $self->{Output} eq $STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Output"} eq $STDOUT 6 0 6
506  
507 6 1;
508  
509 }
510  
511 sub image {
512  
513 11 ▶ my $self = shift;
Subroutine image: called CC=14 SCAR=31.6
Pod undocumented
514  
515 11 ▶ if ($self->{Output} eq $STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Output"} eq $STDOUT) 0 11 11
516 0 die "Cannot dump images to stdout\n";
517 }
518  
519 my $ebook = EBook::Ishmael::EBook->new(
520 $self->{Ebook},
521 $self->{Format},
522 $self->{FileEnc},
523 $self->{Network},
524 11 );
525  
526 11 my $num = $ebook->image_num;
527  
528 11 ▶ unless ($num) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num) 5 6 11
529 5 say "$self->{Ebook} has no images";
530 5 return;
531 }
532  
533 6 my $base = basename($self->{Output});
534 6 my $pad = length $num;
535  
536 6 my $mkdir = 0;
537  
538 6 ▶ unless (-d $self->{Output}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"Output"}) 0 6 6
539 mkdir $self->{Output}
540 0 ▶ or die "Failed to mkdir $self->{Output}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->{"Output"} or die qq[Failed to mkdir $self->{"Output"}: $!\n] 0 0 0
541 0 $mkdir = 1;
542 }
543  
544 6 my @created;
545  
546 eval {
547 6 for my $i (0 .. $num - 1) {
548  
549 35 my $ii = $i + 1;
550  
551 35 my ($img, $format) = $ebook->image($i);
552 35 ▶ if (not defined $img) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $img) 0 35 35
553 0 warn "Error dumping image #$i, skipping\n";
554 0 next;
555 }
556  
557 35 my $b = sprintf "%s-%0*d.%s", $base, $pad, $ii, $format;
558  
559 35 my $p = File::Spec->catfile($self->{Output}, $b);
560  
561 35 ▶ open my $fh, '>', $p
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $p or die "Failed to open $p for writing: $!\n" 0 35 35
562 or die "Failed to open $p for writing: $!\n";
563 35 binmode $fh;
564 35 print { $fh } $img;
565 35 close $fh;
566  
567 35 push @created, $p;
568  
569 }
570 6 1;
571 6 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $i (0 .. $num - 1) { my $ii = $i + 1; () = $ebook->image($i); unless (defined $img) { warn "Error dumping image #$i, skipping\n"; next; } ; my $b = sprintf("%s-%0*d.%s", $base, $pad, $ii, $format); my $p = "File::Spec"->catfile($self->{"Output"}, $b); open my $fh, ">", $p or die "Failed to open $p for writing: $!\n"; binmode $fh; print {$fh;} $img; close $fh; push @created, $p; } ; 1 } } or do { foreach my $c (@created) { unlink $c; } ; rmdir $self->{"Output"} if $mkdir; die $@ } 0 6 6
572  
573 0 for my $c (@created) {
574 0 unlink $c;
575 }
576  
577 0 ▶ rmdir $self->{Output} if $mkdir;
Branch: 1 branch
BranchTrueFalseTotal
if $mkdir 0 0 0
578  
579 0 die $@;
580 };
581  
582 6 ▶ unless (@created) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@created) 0 6 6
583 0 ▶ rmdir $self->{Output} if $mkdir;
Branch: 1 branch
BranchTrueFalseTotal
if $mkdir 0 0 0
584 0 die "Could not dump any images in $self->{Output}\n";
585 }
586  
587 6 say $self->{Output};
588 6 for my $c (map { basename($_) } @created) {
589 35 say " $c";
590 }
591  
592 6 1;
593  
594 }
595  
596 sub run {
597  
598 115 ▶ my $self = shift;
Subroutine run: called CC=8 SCAR=20.9
Pod documented
599  
600 115 ▶ if ($self->{Mode} == MODE_TEXT) {
Branch: 7 branches
BranchTrueFalseTotal
if ($self->{'Mode'} == 0) { } 0 115 115
elsif ($self->{'Mode'} == 1) { } 33 82 115
elsif ($self->{'Mode'} == 2) { } 11 71 82
elsif ($self->{'Mode'} == 3) { } 22 49 71
elsif ($self->{'Mode'} == 4) { } 27 22 49
elsif ($self->{'Mode'} == 5) { } 11 11 22
elsif ($self->{'Mode'} == 6) { } 11 0 11
601 0 $self->text;
602 } elsif ($self->{Mode} == MODE_META) {
603 33 $self->meta;
604 } elsif ($self->{Mode} == MODE_ID) {
605 11 $self->id;
606 } elsif ($self->{Mode} == MODE_HTML) {
607 22 $self->html;
608 } elsif ($self->{Mode} == MODE_RAW_TIME) {
609 27 $self->raw;
610 } elsif ($self->{Mode} == MODE_COVER) {
611 11 $self->cover;
612 } elsif ($self->{Mode} == MODE_IMAGE) {
613 11 $self->image;
614 }
615  
616 115 1;
617  
618 }
619  
620 2 1;
621  
622  
623 =head1 NAME
624  
625 EBook::Ishmael - EBook dumper
626  
627 =head1 SYNOPSIS
628  
629 use EBook::Ishmael;
630  
631 my $ishmael = EBook::Ishmael->init();
632 $ishmael->run();
633  
634 =head1 DESCRIPTION
635  
636 B<EBook::Ishmael> is the workhorse module for L<ishmael>. If you're looking for
637 user documentation, you should consult its manual instead of this (this is
638 developer documentation).
639  
640 =head1 METHODS
641  
642 =head2 $i = EBook::Ishmael->init()
643  
644 Reads C<@ARGV> and returns a blessed C<EBook::Ishmael> object. Consult the
645 manual for L<ishmael> for a list of options that are available.
646  
647 =head2 $i->text()
648  
649 Dumps ebook file to text, default run mode.
650  
651 =head2 $i->meta()
652  
653 Dumps ebook metadata, C<--metadata> mode.
654  
655 =head2 $i->meta_ishmael()
656  
657 Dumps ebook metadata, C<--metadata=ishmael> mode.
658  
659 =head2 $i->meta_json()
660  
661 Dumps ebook metadata in JSON form, C<--metadata=json> mode.
662  
663 =head2 $i->meta_xml()
664  
665 Dumps ebook metadata in XML form, C<--metadata=xml> mode.
666  
667 =head2 $i->id()
668  
669 Identify the format of the given ebook, C<--identify> mode.
670  
671 =head2 $i->html()
672  
673 Dump the HTML-ified contents of a given ebook, C<--html> mode.
674  
675 =head2 $i->raw()
676  
677 Dump the raw, unformatted text contents of a given ebook, C<--raw> mode.
678  
679 =head2 $i->cover()
680  
681 Dump the binary data of the cover image of a given ebook, if one is present,
682 C<--cover> mode.
683  
684 =head2 $i->run()
685  
686 Runs L<ishmael> based on the parameters processed during C<init()>.
687  
688 =head1 AUTHOR
689  
690 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
691  
692 This project's source can be found on its
693 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull
694 requests are welcome!
695  
696 =head1 COPYRIGHT
697  
698 Copyright (C) 2025-2026 Samuel Young
699  
700 This program is free software: you can redistribute it and/or modify
701 it under the terms of the GNU General Public License as published by
702 the Free Software Foundation, either version 3 of the License, or
703 (at your option) any later version.
704  
705 =head1 SEE ALSO
706  
707 L<ishmael>
708  
709 =cut
« blib/lib/EBook/Ishmael/ImageID.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/UTF8.pm »