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

filter: Statementstmt 88.2% 157 / 178 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 73.1% 218 / 298 CC 36 SCAR 20.0
CC
36
Cov
77%
_images
30.0
html
11.6
raw
11.1
CRAP 7.4
« 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.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 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::HTML qw(prepare_html text2html);
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 # TODO: Sort files more accurately
27 # For example, a zip file with these files would be sorted incorrectly:
28 # z/file-1.html
29 # z/file-2.html
30 # ...
31 # z/file-10.html These will be sorted before file-2.html
32 # z/file-11.html
33  
34 17 my $MAGIC = pack 'C4', ( 0x50, 0x4b, 0x03, 0x04 );
35  
36 sub heuristic {
37  
38 36 ▶ my $class = shift;
Subroutine heuristic: called CC=2 SCAR=6.9
Pod undocumented
39 36 my $file = shift;
40 36 my $fh = shift;
41  
42 36 ▶ return 0 unless $file =~ /\.zip$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.zip$/u 24 12 36
43  
44 12 read $fh, my $mag, length $MAGIC;
45  
46 12 return $mag eq $MAGIC;
47  
48 }
49  
50 sub _images {
51  
52 0 ▶ my $dir = shift;
Subroutine _images: not called CC=5 SCAR=34.0
53  
54 0 my @img;
55  
56 0 for my $f (dir($dir)) {
57 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
58 0 push @img, _images($f);
59 ▶ } 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
60 0 push @img, $f;
61 }
62 }
63  
64 0 return @img;
65  
66 }
67  
68 sub _files {
69  
70 22 ▶ my $self = shift;
Subroutine _files: called CC=7 SCAR=19.9
71 22 my $dir = shift;
72  
73 22 for my $f (dir($dir)) {
74 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
75 11 $self->_files($f);
76 ▶ } 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
77 11 push @{ $self->{_images} }, [ $f, image_path_id($f) ];
78 ▶ } 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
79 22 push @{ $self->{_content} }, $f;
80 }
81 }
82  
83 }
84  
85 sub new {
86  
87 11 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.1
Pod undocumented
88 11 my $file = shift;
89 11 my $enc = shift;
90 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
91  
92 11 my $self = {
93 Source => undef,
94 Metadata => EBook::Ishmael::EBook::Metadata->new,
95 Encode => $enc,
96 Network => $net,
97 _tmpdir => undef,
98 _content => [],
99 _images => [],
100 _cover => undef,
101 };
102  
103 11 bless $self, $class;
104  
105 11 my $title = (fileparse($file, qr/\.[^.]*/))[0];
106  
107 11 $self->{Source} = File::Spec->rel2abs($file);
108  
109 11 $self->{_tmpdir} = safe_tmp_unzip;
110 11 unzip($self->{Source}, $self->{_tmpdir});
111  
112 11 $self->_files($self->{_tmpdir});
113  
114 11 ▶ unless (@{ $self->{_content} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_content"};}) 0 11 11
115 0 die "$self->{Source}: Found no content files in Zip ebook archive\n";
116 }
117  
118 11 ▶ $self->{_cover} = first { basename($_) =~ m/cover/i } @{ $self->{_images} };
Subroutine __ANON__: called CC=1 SCAR=0.0
119 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
120  
121 11 $self->{Metadata}->set_title($title);
122 11 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
123 11 $self->{Metadata}->set_format('Zip');
124  
125 11 return $self;
126  
127 }
128  
129 sub html {
130  
131 3 ▶ my $self = shift;
Subroutine html: called CC=9 SCAR=24.5
Pod undocumented
132 3 my $out = shift;
133  
134 3 my $html = '';
135  
136 3 for my $f (@{ $self->{_content} }) {
137 6 ▶ if ($f =~ /\.txt$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f =~ /\.txt$/u) { } 3 3 6
138 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
139 or die "Failed to open $f for reading: $!\n";
140 3 binmode $fh;
141 3 my $text = do { local $/; <$fh> };
142 3 close $fh;
143 3 ▶ if (not defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 3 0 3
144 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
145 }
146 3 $html .= text2html(decode($self->{Encode}, $text));
147 } else {
148 my $dom = XML::LibXML->load_html(
149 location => $f,
150 recover => 2,
151 no_network => !$self->{Network},
152 3 );
153 3 my ($body) = $dom->findnodes('/html/body');
154 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
155 3 prepare_html($body);
156 3 $html .= join '', map { $_->toString } $body->childNodes;
157 }
158 }
159  
160 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
161 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
162 or die "Failed to open $out for writing: $!\n";
163 0 binmode $fh, ':utf8';
164 0 print { $fh } $html;
165 0 close $fh;
166 0 return $out;
167 } else {
168 3 return $html;
169 }
170  
171 }
172  
173 sub raw {
174  
175 3 ▶ my $self = shift;
Subroutine raw: called CC=9 SCAR=24.1
Pod undocumented
176 3 my $out = shift;
177  
178 3 my $raw;
179 3 for my $c (@{ $self->{_content} }) {
180 6 ▶ if ($c =~ /\.txt$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\.txt$/u) { } 3 3 6
181 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
182 or die "Failed to open $c for reading: $!\n";
183 3 binmode $fh;
184 3 my $r = do { local $/; <$fh> };
185 3 close $fh;
186 3 ▶ if (not defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 2 1 3
187 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
188 }
189 3 $raw .= decode($self->{Encode}, $r) . "\n\n";
190 } else {
191 my $dom = XML::LibXML->load_html(
192 location => $c,
193 recover => 2,
194 no_network => !$self->{Network},
195 3 );
196 3 my ($body) = $dom->findnodes('/html/body');
197 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
198 3 prepare_html($body);
199 3 $raw .= $body->textContent . "\n\n";
200 }
201 }
202 3 $raw =~ s/\n\n//;
203  
204 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
205 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
206 or die "Failed to open $out for writing: $!\n";
207 0 binmode $fh, ':utf8';
208 0 print { $fh } $raw;
209 0 close $fh;
210 0 return $out;
211 } else {
212 3 return $raw;
213 }
214  
215 }
216  
217 sub metadata {
218  
219 4 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
220  
221 4 return $self->{Metadata};
222  
223 }
224  
225 sub has_cover {
226  
227 4 ▶ my $self = shift;
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
228  
229 4 return defined $self->{_cover};
230  
231 }
232  
233 sub cover {
234  
235 2 ▶ my $self = shift;
Subroutine cover: called CC=3 SCAR=11.1
Pod undocumented
236  
237 2 ▶ return (undef, undef) unless $self->has_cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cover 0 2 2
238  
239 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
240 or die "Failed to open $self->{_cover}[0] for reading: $!\n";
241 2 binmode $fh;
242 2 my $bin = do { local $/ = undef; readline $fh };
243 2 close $fh;
244  
245 2 return ($bin, $self->{_cover}[1]);
246  
247 }
248  
249 sub image_num {
250  
251 4 ▶ my $self = shift;
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
252  
253 4 return scalar @{ $self->{_images} };
254  
255 }
256  
257 sub image {
258  
259 2 ▶ my $self = shift;
Subroutine image: called CC=3 SCAR=11.2
Pod undocumented
260 2 my $n = shift;
261  
262 2 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 2 2
263 0 return (undef, undef);
264 }
265  
266 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
267 or die "Failed to open $self->{_images}[$n][0] for reading: $!\n";
268 2 binmode $fh;
269 2 my $img = do { local $/ = undef; readline $fh };
270 2 close $fh;
271  
272 2 return ($img, $self->{_images}[$n][1]);
273  
274 }
275  
276 DESTROY {
277  
278 11 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
279  
280 11 ▶ remove_tree($self->{_tmpdir}, { safe => 1 }) if -d $self->{_tmpdir};
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"_tmpdir"} 11 0 11
281  
282 }
283  
284 17 1;
« blib/lib/EBook/Ishmael/PDB.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/EUCKR.pm »