blib/lib/EBook/Ishmael/EBook/Epub.pm

filter: Statementstmt 88.6% 188 / 212 Branchbran 55.4% 51 / 92 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 10 totaltotal 77.0% 265 / 344 CC 52 SCAR 32.7
CC
52
Cov
78%
_read_rootfile
53.3
raw
5.2
html
5.0
CRAP 26.3
« blib/lib/EBook/Ishmael/EBook/PDF.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/HZ.pm »
1 package EBook::Ishmael::EBook::Epub;
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 File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use EBook::Ishmael::ImageID qw(mimetype_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use EBook::Ishmael::Time qw(guess_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use EBook::Ishmael::Unzip qw(unzip safe_tmp_unzip);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 my $MAGIC = pack 'C4', ( 0x50, 0x4b, 0x03, 0x04 );
19 17 my $CONTAINER = File::Spec->catfile(qw/META-INF container.xml/);
20  
21 17 my $DCNS = "http://purl.org/dc/elements/1.1/";
22  
23 # This module only supports EPUBs with a single rootfile. The standard states
24 # there can be multiple rootfiles, but I have yet to encounter one that does.
25  
26 # TODO: Make heuristic more precise.
27 # If file uses zip magic bytes, assume it to be an EPUB.
28 sub heuristic {
29  
30 115 ▶ my $class = shift;
Subroutine heuristic: called CC=2 SCAR=6.9
Pod undocumented
31 115 my $file = shift;
32 115 my $fh = shift;
33  
34 115 ▶ return 0 if $file =~ /\.zip$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.zip$/u 12 103 115
35  
36 103 read $fh, my $mag, 4;
37  
38 103 return $mag eq $MAGIC;
39 }
40  
41 # Set _rootfile based on container.xml's rootfile@full-path attribute.
42 sub _get_rootfile {
43  
44 10 ▶ my $self = shift;
Subroutine _get_rootfile: called CC=2 SCAR=7.0
45  
46 my $dom = XML::LibXML->load_xml(
47 location => $self->{_container},
48 no_network => !$self->{Network},
49 10 );
50 10 my $ns = $dom->documentElement->namespaceURI;
51  
52 10 my $xpc = XML::LibXML::XPathContext->new($dom);
53 10 $xpc->registerNs('container', $ns);
54  
55 10 my ($rf) = $xpc->findnodes(
56 '/container:container' .
57 '/container:rootfiles' .
58 '/container:rootfile[1]' .
59 '/@full-path'
60 );
61  
62 10 ▶ unless (defined $rf) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rf) 0 10 10
63 0 die "Could not find root file in EPUB $self->{Source}\n";
64 }
65  
66 10 $self->{_rootfile} = File::Spec->catfile($self->{_unzip}, $rf->value());
67  
68 10 return $self->{_rootfile};
69  
70 }
71  
72 # Reads metadata and _spine from rootfile.
73 sub _read_rootfile {
74  
75 10 ▶ my $self = shift;
Subroutine _read_rootfile: called CC=35 SCAR=39.8
76  
77 10 $self->{_contdir} = dirname($self->{_rootfile});
78  
79 my $dom = XML::LibXML-> load_xml(
80 location => $self->{_rootfile},
81 no_network => !$self->{Network},
82 10 );
83 10 my $ns = $dom->documentElement->namespaceURI;
84  
85 10 my $xpc = XML::LibXML::XPathContext->new($dom);
86 10 $xpc->registerNs('package', $ns);
87 10 $xpc->registerNs('dc', $DCNS);
88  
89 10 my ($vernode) = $xpc->findnodes(
90 '/package:package/@version'
91 );
92 10 ▶ if (defined $vernode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vernode) 10 0 10
93 10 $self->{_version} = $vernode->getValue;
94 }
95  
96 10 my ($meta) = $xpc->findnodes(
97 '/package:package/package:metadata'
98 );
99  
100 10 my ($manif) = $xpc->findnodes(
101 '/package:package/package:manifest'
102 );
103  
104 10 my ($spine) = $xpc->findnodes(
105 '/package:package/package:spine'
106 );
107  
108 10 ▶ unless (defined $meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $meta) 0 10 10
109 0 die "EPUB $self->{Source} is missing metadata in rootfile\n";
110 }
111  
112 10 ▶ unless (defined $manif) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $manif) 0 10 10
113 0 die "EPUB $self->{Source} is missing manifest in rootfile\n";
114 }
115  
116 10 ▶ unless (defined $spine) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $spine) 0 10 10
117 0 die "EPUB $self->{Source} is missing spine in rootfile\n";
118 }
119  
120 10 for my $dc ($xpc->findnodes('./dc:*', $meta)) {
121  
122 70 my $name = $dc->nodeName =~ s/^dc://r;
123 70 my $text = $dc->textContent();
124  
125 70 $text =~ s/\s+/ /g;
126  
127 70 ▶ if ($name eq 'contributor') {
Branch: 9 branches
BranchTrueFalseTotal
if ($name eq 'contributor') { } 10 60 70
elsif ($name eq 'creator') { } 10 50 60
elsif ($name eq 'date') { } 10 40 50
elsif ($name eq 'description') { } 0 40 40
elsif ($name eq 'identifier') { } 20 20 40
elsif ($name eq 'language') { } 10 10 20
elsif ($name eq 'publisher') { } 0 10 10
elsif ($name eq 'subject') { } 0 10 10
elsif ($name eq 'title') { } 10 0 10
128 10 $self->{Metadata}->add_contributor($text);
129 } elsif ($name eq 'creator') {
130 10 $self->{Metadata}->add_author($text);
131 } elsif ($name eq 'date') {
132 10 my $t = eval { guess_time($text) };
133 10 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 10 0 10
134 10 $self->{Metadata}->set_created($t);
135 }
136 } elsif ($name eq 'description') {
137 0 $self->{Metadata}->set_description($text);
138 } elsif ($name eq 'identifier') {
139 20 $self->{Metadata}->set_id($text);
140 } elsif ($name eq 'language') {
141 10 $self->{Metadata}->set_language($text);
142 } elsif ($name eq 'publisher') {
143 0 $self->{Metadata}->add_contributor($text);
144 } elsif ($name eq 'subject') {
145 0 $self->{Metadata}->add_genre($text);
146 } elsif ($name eq 'title') {
147 10 $self->{Metadata}->set_title($text);
148 }
149  
150 }
151  
152 10 for my $itemref ($xpc->findnodes('./package:itemref', $spine)) {
153  
154 40 ▶ my $id = $itemref->getAttribute('idref') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $itemref->getAttribute("idref") or next 0 40 40
155  
156 40 ▶ my ($item) = $xpc->findnodes(
Branch: 1 branch
BranchTrueFalseTotal
my($item) = $xpc->findnodes(qq[./package:item[\@id="$id"]], $manif) or next 0 40 40
157 "./package:item[\@id=\"$id\"]", $manif
158 ) or next;
159  
160 40 ▶ unless (($item->getAttribute('media-type') // '') eq 'application/xhtml+xml') {
Branch: 1 branch
BranchTrueFalseTotal
unless (($item->getAttribute("media-type") // "") eq "application/xhtml+xml") 0 40 40
Condition50%1/2
$item->getAttribute("media-type") // ""
l!l
400
MC/DC0%0/1
$item->getAttribute("media-type") // ""
$item->getAttribute("media-type")
Truth table50%1/2
A
A= $item->getAttribute("media-type")
Aresult
11
00
161 0 next;
162 }
163  
164 40 ▶ my $href = $item->getAttribute('href') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $href = $item->getAttribute("href") or next 0 40 40
165  
166 40 $href = File::Spec->catfile($self->{_contdir}, $href);
167  
168 40 ▶ next unless -f $href;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $href 0 40 40
169  
170 40 push @{ $self->{_spine} }, $href;
171  
172 }
173  
174 # Get list of images
175 10 for my $item ($xpc->findnodes('./package:item', $manif)) {
176 80 my $mime = $item->getAttribute('media-type');
177 80 ▶ next if not defined $mime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mime 0 80 80
178  
179 80 my $format = mimetype_id($mime);
180 80 ▶ next if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 70 10 80
181  
182 10 ▶ my $href = $item->getAttribute('href') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $href = $item->getAttribute("href") or next 0 10 10
183 10 $href = File::Spec->catfile($self->{_contdir}, $href);
184 10 ▶ next if not -f $href;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $href 0 10 10
185  
186 10 push @{ $self->{_images} }, [ $href, $format ];
187  
188 }
189  
190 10 my ($covmeta) = $xpc->findnodes('./package:meta[@name="cover"]', $meta);
191 # Put if code in own block so that we can last out of it.
192 10 ▶ if (defined $covmeta) {{
Branch: 1 branch
BranchTrueFalseTotal
if (defined $covmeta) 10 0 10
193 10 ▶ my $covcont = $covmeta->getAttribute('content') or last;
Branch: 1 branch
BranchTrueFalseTotal
my $covcont = $covmeta->getAttribute("content") or last 0 10 10
194 10 ▶ my ($covitem) = $xpc->findnodes("./package:item[\@id=\"$covcont\"]", $manif)
Branch: 1 branch
BranchTrueFalseTotal
my($covitem) = $xpc->findnodes(qq[./package:item[\@id="$covcont"]], $manif) or last 0 10 10
195 or last;
196 10 ▶ my $covhref = $covitem->getAttribute('href') or last;
Branch: 1 branch
BranchTrueFalseTotal
my $covhref = $covitem->getAttribute("href") or last 0 10 10
197 10 ▶ my $covmime = $covitem->getAttribute('media-type') or last;
Branch: 1 branch
BranchTrueFalseTotal
my $covmime = $covitem->getAttribute("media-type") or last 0 10 10
198 10 ▶ my $format = mimetype_id($covmime) or last;
Branch: 1 branch
BranchTrueFalseTotal
my $format = mimetype_id($covmime) or last 0 10 10
199 10 my $covpath = File::Spec->catfile($self->{_contdir}, $covhref);
200 10 ▶ last unless -f $covpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $covpath 0 10 10
201 10 $self->{_cover} = [ $covpath, $format ];
202 }}
203  
204 10 return 1;
205  
206 }
207  
208 sub new {
209  
210 10 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.6
Pod undocumented
211 10 my $file = shift;
212 10 my $enc = shift;
213 10 ▶ my $net = shift // 1;
Condition50%1/2
shift() // 1
l!l
100
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
214  
215 10 my $self = {
216 Source => undef,
217 Metadata => EBook::Ishmael::EBook::Metadata->new,
218 Network => $net,
219 _unzip => undef,
220 _container => undef,
221 _rootfile => undef,
222 # Directory where _rootfile is, as that is where all of the "content"
223 # files are.
224 _contdir => undef,
225 # List of content files in order specified by spine.
226 _spine => [],
227 _cover => undef,
228 _images => [],
229 _version => undef,
230 };
231  
232 10 bless $self, $class;
233  
234 10 $self->read($file);
235  
236 10 ▶ if (not defined $self->{Metadata}->title) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Metadata"}->title) 0 10 10
237 0 $self->{Metadata}->set_title((fileparse($file, qr/\.[^.]*/))[0]);
238 }
239  
240 10 ▶ if (defined $self->{_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_version'}) { } 10 0 10
241 10 $self->{Metadata}->set_format('EPUB ' . $self->{_version});
242 } else {
243 0 $self->{Metadata}->set_format('EPUB');
244 }
245  
246 10 return $self;
247  
248 }
249  
250 sub read {
251  
252 10 ▶ my $self = shift;
Subroutine read: called CC=2 SCAR=7.0
Pod undocumented
253 10 my $src = shift;
254  
255 10 my $tmpdir = safe_tmp_unzip;
256  
257 10 unzip($src, $tmpdir);
258  
259 10 $self->{Source} = File::Spec->rel2abs($src);
260 10 $self->{_unzip} = $tmpdir;
261  
262 10 $self->{_container} = File::Spec->catfile($self->{_unzip}, $CONTAINER);
263  
264 10 ▶ unless (-f $self->{_container}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"_container"}) 0 10 10
265 0 die "$src is an invalid EPUB file: does not have a META-INF/container.xml\n";
266 }
267  
268 10 $self->_get_rootfile();
269 10 $self->_read_rootfile();
270  
271 10 return 1;
272  
273 }
274  
275 sub html {
276  
277 3 ▶ my $self = shift;
Subroutine html: called CC=4 SCAR=16.1
Pod undocumented
278 3 my $out = shift;
279  
280 my $html = join '', map {
281  
282 my $dom = XML::LibXML->load_xml(
283 location => $_,
284 no_network => !$self->{Network},
285 12 );
286 12 my $ns = $dom->documentElement->namespaceURI;
287  
288 12 my $xpc = XML::LibXML::XPathContext->new($dom);
289 12 $xpc->registerNs('html', $ns);
290  
291 12 ▶ my ($body) = $xpc->findnodes('/html:html/html:body')
Branch: 1 branch
BranchTrueFalseTotal
my($body) = $xpc->findnodes("/html:html/html:body") or next 0 12 12
292 or next;
293  
294 12 map { $_->toString } $body->childNodes;
295  
296 3 } @{ $self->{_spine} };
297  
298 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
299 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
300 or die "Failed to open $out for writing: $!\n";
301 0 binmode $fh, ':utf8';
302 0 print { $fh } $html;
303 0 close $fh;
304 0 return $out;
305 } else {
306 3 return $html;
307 }
308  
309 }
310  
311 sub raw {
312  
313 2 ▶ my $self = shift;
Subroutine raw: called CC=4 SCAR=16.4
Pod undocumented
314 2 my $out = shift;
315  
316 my $raw = join "\n\n", map {
317  
318 my $dom = XML::LibXML->load_xml(
319 location => $_,
320 no_network => !$self->{Network},
321 8 );
322 8 my $ns = $dom->documentElement->namespaceURI;
323  
324 8 my $xpc = XML::LibXML::XPathContext->new($dom);
325 8 $xpc->registerNs('html', $ns);
326  
327 8 ▶ my ($body) = $xpc->findnodes('/html:html/html:body')
Branch: 1 branch
BranchTrueFalseTotal
my($body) = $xpc->findnodes("/html:html/html:body") or next 0 8 8
328 or next;
329  
330 8 $body->textContent;
331  
332 2 } @{ $self->{_spine} };
333  
334 2 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 2 2
335 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
336 or die "Failed to open $out for writing: $!\n";
337 0 binmode $fh, ':utf8';
338 0 print { $fh } $raw;
339 0 close $fh;
340 0 return $out;
341 } else {
342 2 return $raw;
343 }
344  
345 }
346  
347 sub metadata {
348  
349 10 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
350  
351 10 return $self->{Metadata};
352  
353 }
354  
355 sub has_cover {
356  
357 4 ▶ my $self = shift;
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
358  
359 4 return defined $self->{_cover};
360  
361 }
362  
363 sub cover {
364  
365 2 ▶ my $self = shift;
Subroutine cover: called CC=3 SCAR=11.1
Pod undocumented
366  
367 2 ▶ return (undef, undef) if not $self->has_cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cover 0 2 2
368  
369 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
370 or die "Failed to open $self->{_cover}[0] for reading: $!\n";
371 2 binmode $fh;
372 2 my $img = do { local $/; readline $fh };
373 2 close $fh;
374  
375 2 return ($img, $self->{_cover}[1]);
376  
377 }
378  
379 sub image_num {
380  
381 4 ▶ my $self = shift;
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
382  
383 4 return scalar @{ $self->{_images} };
384  
385 }
386  
387 sub image {
388  
389 2 ▶ my $self = shift;
Subroutine image: called CC=3 SCAR=11.2
Pod undocumented
390 2 my $n = shift;
391  
392 2 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 2 2
393 0 return (undef, undef);
394 }
395  
396 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
397 or die "Failed to open $self->{_images}[$n][0] for reading: $!\n";
398 2 binmode $fh;
399 2 my $img = do { local $/ = undef; readline $fh };
400 2 close $fh;
401  
402 2 return ($img, $self->{_images}[$n][1]);
403  
404 }
405  
406 DESTROY {
407  
408 10 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
409  
410 10 ▶ remove_tree($self->{_unzip}, { safe => 1 }) if -d $self->{_unzip};
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"_unzip"} 10 0 10
411  
412 }
413  
414 17 1;
« blib/lib/EBook/Ishmael/EBook/PDF.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/HZ.pm »