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

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