blib/lib/EBook/Ishmael/EBook/Zip.pm

filter: Statementstmt 88.0% 155 / 176 Branchbran 54.5% 24 / 44 Conditioncond 37.5% 9 / 24 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 96.4% 27 / 28 Podpod 0.0% 0 / 9 totaltotal 72.9% 216 / 296 CC 36 SCAR 20.1
CC
36
Cov
77%
_images
30.0
html
11.8
raw
11.2
CRAP 7.5
« blib/lib/EBook/Ishmael/PDB.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/EUCKR.pm »
1 package EBook::Ishmael::EBook::Zip;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.03';
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 Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 ▶use EBook::Ishmael::CharDet;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use EBook::Ishmael::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use EBook::Ishmael::ImageID qw(image_path_id is_image_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use EBook::Ishmael::TextToHtml;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 ▶use EBook::Ishmael::Unzip qw(unzip safe_tmp_unzip);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # This isn't any official format, but generic zip archives are a common way of
23 # distributing some ebooks. This module basically looks for any text or HTML
24 # files and extracts content from those.
25  
26 17 my $MAGIC = pack 'C4', ( 0x50, 0x4b, 0x03, 0x04 );
27  
28 sub heuristic {
29  
30 36 ▶ my $class = shift;
Subroutine heuristic: called CC=2 SCAR=6.9
Pod undocumented
31 36 my $file = shift;
32 36 my $fh = shift;
33  
34 36 ▶ return 0 unless $file =~ /\.zip$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.zip$/u 24 12 36
35  
36 12 read $fh, my $mag, length $MAGIC;
37  
38 12 return $mag eq $MAGIC;
39  
40 }
41  
42 sub _images {
43  
44 0 ▶ my $dir = shift;
Subroutine _images: not called CC=5 SCAR=34.0
45  
46 0 my @img;
47  
48 0 for my $f (dir($dir)) {
49 0 ▶ if (-d $f) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $f) { } 0 0 0
elsif (-f $f and is_image_path($f)) { } 0 0 0
50 0 push @img, _images($f);
51 ▶ } elsif (-f $f and is_image_path($f)) {
Condition0%0/3
-f $f and is_image_path($f)
!ll&&!rl&&r
000
MC/DC0%0/2
-f $f and is_image_path($f)
-f $f is_image_path($f)
Truth table0%0/3
A and B
A= -f $fB= is_image_path($f)
ABresult
0X0
100
111
52 0 push @img, $f;
53 }
54 }
55  
56 0 return @img;
57  
58 }
59  
60 sub _files {
61  
62 22 ▶ my $self = shift;
Subroutine _files: called CC=7 SCAR=19.9
63 22 my $dir = shift;
64  
65 22 for my $f (dir($dir)) {
66 44 ▶ if (-d $f) {
Branch: 3 branches
BranchTrueFalseTotal
if (-d $f) { } 11 33 44
elsif (-f $f and is_image_path($f)) { } 11 22 33
elsif (-f $f and $f =~ /\.(x?html?|txt)$/u) { } 22 0 22
67 11 $self->_files($f);
68 ▶ } elsif (-f $f and is_image_path($f)) {
Condition66%2/3
-f $f and is_image_path($f)
!ll&&!rl&&r
02211
MC/DC50%1/2
-f $f and is_image_path($f)
-f $f is_image_path($f)
Truth table66%2/3
A and B
A= -f $fB= is_image_path($f)
ABresult
0X0
100
111
69 11 push @{ $self->{_images} }, [ $f, image_path_id($f) ];
70 ▶ } elsif (-f $f and $f =~ /\.(x?html?|txt)$/) {
Condition33%1/3
-f $f and $f =~ /\.(x?html?|txt)$/u
!ll&&!rl&&r
0022
MC/DC0%0/2
-f $f and $f =~ /\.(x?html?|txt)$/u
-f $f $f =~ /\.(x?html?|txt)$/u
Truth table33%1/3
A and B
A= -f $fB= $f =~ /\.(x?html?|txt)$/u
ABresult
0X0
100
111
71 22 push @{ $self->{_content} }, $f;
72 }
73 }
74  
75 }
76  
77 sub new {
78  
79 11 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.1
Pod undocumented
80 11 my $file = shift;
81 11 my $enc = shift;
82 11 ▶ my $net = shift // 1;
Condition50%1/2
shift() // 1
l!l
110
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
83  
84 11 my $self = {
85 Source => undef,
86 Metadata => EBook::Ishmael::EBook::Metadata->new,
87 Encode => $enc,
88 Network => $net,
89 _tmpdir => undef,
90 _content => [],
91 _images => [],
92 _cover => undef,
93 };
94  
95 11 bless $self, $class;
96  
97 11 my $title = (fileparse($file, qr/\.[^.]*/))[0];
98  
99 11 $self->{Source} = File::Spec->rel2abs($file);
100  
101 11 $self->{_tmpdir} = safe_tmp_unzip;
102 11 unzip($self->{Source}, $self->{_tmpdir});
103  
104 11 $self->_files($self->{_tmpdir});
105  
106 11 ▶ unless (@{ $self->{_content} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_content"};}) 0 11 11
107 0 die "$self->{Source}: Found no content files in Zip ebook archive\n";
108 }
109  
110 11 ▶ $self->{_cover} = first { basename($_) =~ m/cover/i } @{ $self->{_images} };
Subroutine __ANON__: called CC=1 SCAR=0.0
111 11 ▶ $self->{_cover} //= $self->{_images}[0];
Condition33%1/3
$self->{"_cover"} //= $self->{"_images"}[0]
l!l&&r!l&&!r
0110
MC/DC0%0/2
$self->{"_cover"} //= $self->{"_images"}[0]
$self->{"_cover"} $self->{"_images"}[0]
Truth table33%1/3
A //= B
A= $self->{"_cover"}B= $self->{"_images"}[0]
ABresult
1X1
011
000
112  
113 11 $self->{Metadata}->set_title($title);
114 11 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
115 11 $self->{Metadata}->set_format('Zip');
116  
117 11 return $self;
118  
119 }
120  
121 sub html {
122  
123 3 ▶ my $self = shift;
Subroutine html: called CC=9 SCAR=24.7
Pod undocumented
124 3 my $out = shift;
125  
126 3 my $html = '';
127  
128 3 for my $f (@{ $self->{_content} }) {
129 6 ▶ if ($f =~ /\.txt$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\.txt$/u) { } 3 3 6
130 3 ▶ open my $fh, '<', $f
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $f or die "Failed to open $f for reading: $!\n" 0 3 3
131 or die "Failed to open $f for reading: $!\n";
132 3 binmode $fh;
133 3 my $text = do { local $/; <$fh> };
134 3 close $fh;
135 3 ▶ if (not defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 3 0 3
136 3 ▶ $self->{Encode} = chardet($text) // 'ASCII';
Condition50%1/2
chardet($text) // "ASCII"
l!l
30
MC/DC0%0/1
chardet($text) // "ASCII"
chardet($text)
Truth table50%1/2
A
A= chardet($text)
Aresult
11
00
137 }
138 3 $html .= text2html(decode($self->{Encode}, $text));
139 } else {
140 my $dom = XML::LibXML->load_html(
141 location => $f,
142 recover => 2,
143 no_network => !$self->{Network},
144 3 );
145 3 my ($body) = $dom->findnodes('/html/body');
146 3 ▶ $body //= $dom->documentElement;
Condition33%1/3
$body //= $dom->documentElement
l!l&&r!l&&!r
300
MC/DC0%0/2
$body //= $dom->documentElement
$body $dom->documentElement
Truth table33%1/3
A //= B
A= $bodyB= $dom->documentElement
ABresult
1X1
011
000
147 3 $html .= join '', map { $_->toString } $body->childNodes;
148 }
149 }
150  
151 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
152 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
153 or die "Failed to open $out for writing: $!\n";
154 0 binmode $fh, ':utf8';
155 0 print { $fh } $html;
156 0 close $fh;
157 0 return $out;
158 } else {
159 3 return $html;
160 }
161  
162 }
163  
164 sub raw {
165  
166 3 ▶ my $self = shift;
Subroutine raw: called CC=9 SCAR=24.2
Pod undocumented
167 3 my $out = shift;
168  
169 3 my $raw;
170 3 for my $c (@{ $self->{_content} }) {
171 6 ▶ if ($c =~ /\.txt$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\.txt$/u) { } 3 3 6
172 3 ▶ open my $fh, '<', $c
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $c or die "Failed to open $c for reading: $!\n" 0 3 3
173 or die "Failed to open $c for reading: $!\n";
174 3 binmode $fh;
175 3 my $r = do { local $/; <$fh> };
176 3 close $fh;
177 3 ▶ if (not defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 2 1 3
178 2 ▶ $self->{Encode} = chardet($r) // 'ASCII';
Condition50%1/2
chardet($r) // "ASCII"
l!l
20
MC/DC0%0/1
chardet($r) // "ASCII"
chardet($r)
Truth table50%1/2
A
A= chardet($r)
Aresult
11
00
179 }
180 3 $raw .= decode($self->{Encode}, $r) . "\n\n";
181 } else {
182 my $dom = XML::LibXML->load_html(
183 location => $c,
184 recover => 2,
185 no_network => !$self->{Network},
186 3 );
187 3 my ($body) = $dom->findnodes('/html/body');
188 3 ▶ $body //= $dom->documentElement;
Condition33%1/3
$body //= $dom->documentElement
l!l&&r!l&&!r
300
MC/DC0%0/2
$body //= $dom->documentElement
$body $dom->documentElement
Truth table33%1/3
A //= B
A= $bodyB= $dom->documentElement
ABresult
1X1
011
000
189 3 $raw .= $body->textContent . "\n\n";
190 }
191 }
192 3 $raw =~ s/\n\n//;
193  
194 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
195 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
196 or die "Failed to open $out for writing: $!\n";
197 0 binmode $fh, ':utf8';
198 0 print { $fh } $raw;
199 0 close $fh;
200 0 return $out;
201 } else {
202 3 return $raw;
203 }
204  
205 }
206  
207 sub metadata {
208  
209 4 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
210  
211 4 return $self->{Metadata};
212  
213 }
214  
215 sub has_cover {
216  
217 4 ▶ my $self = shift;
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
218  
219 4 return defined $self->{_cover};
220  
221 }
222  
223 sub cover {
224  
225 2 ▶ my $self = shift;
Subroutine cover: called CC=3 SCAR=11.1
Pod undocumented
226  
227 2 ▶ return (undef, undef) unless $self->has_cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cover 0 2 2
228  
229 2 ▶ open my $fh, '<', $self->{_cover}[0]
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"_cover"}[0] or die qq[Failed to open $self->{"_cover"}[0] for reading: $!\n] 0 2 2
230 or die "Failed to open $self->{_cover}[0] for reading: $!\n";
231 2 binmode $fh;
232 2 my $bin = do { local $/ = undef; readline $fh };
233 2 close $fh;
234  
235 2 return ($bin, $self->{_cover}[1]);
236  
237 }
238  
239 sub image_num {
240  
241 4 ▶ my $self = shift;
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
242  
243 4 return scalar @{ $self->{_images} };
244  
245 }
246  
247 sub image {
248  
249 2 ▶ my $self = shift;
Subroutine image: called CC=3 SCAR=11.2
Pod undocumented
250 2 my $n = shift;
251  
252 2 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 2 2
253 0 return (undef, undef);
254 }
255  
256 2 ▶ open my $fh, '<', $self->{_images}[$n][0]
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"_images"}[$n][0] or die qq[Failed to open $self->{"_images"}[$n][0] for reading: $!\n] 0 2 2
257 or die "Failed to open $self->{_images}[$n][0] for reading: $!\n";
258 2 binmode $fh;
259 2 my $img = do { local $/ = undef; readline $fh };
260 2 close $fh;
261  
262 2 return ($img, $self->{_images}[$n][1]);
263  
264 }
265  
266 DESTROY {
267  
268 11 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
269  
270 11 ▶ remove_tree($self->{_tmpdir}, { safe => 1 }) if -d $self->{_tmpdir};
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"_tmpdir"} 11 0 11
271  
272 }
273  
274 17 1;
« blib/lib/EBook/Ishmael/PDB.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/EUCKR.pm »