blib/lib/EBook/Ishmael/EBook/CHM.pm

filter: Statementstmt 23.4% 46 / 196 Branchbran 0.0% 0 / 60 Conditioncond 3.4% 1 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 50.0% 13 / 26 Podpod 0.0% 0 / 9 totaltotal 17.7% 60 / 339 CC 43 SCAR 37.7
CC
43
Cov
16%
_clean_html
132.0
cover
72.0
_urlstr
42.0
CRAP 43.3
« blib/lib/EBook/Ishmael/TextBrowserDump.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/FictionBook2.pm »
1 package EBook::Ishmael::EBook::CHM;
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::Temp qw(tempdir);
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 File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use EBook::Ishmael::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use EBook::Ishmael::ImageID qw(image_path_id image_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use EBook::Ishmael::ShellQuote qw(safe_qx);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # TODO: Make more of an effort to find metadata
20  
21 17 my $HAS_CHMLIB = defined which('extract_chmLib');
22 17 my $HAS_HH = defined which('hh.exe');
23 17 ▶our $CAN_TEST = $HAS_CHMLIB || $HAS_HH;
Condition33%1/3
$HAS_CHMLIB || $HAS_HH
l!l&&r!l&&!r
0017
MC/DC0%0/2
$HAS_CHMLIB || $HAS_HH
$HAS_CHMLIB $HAS_HH
Truth table33%1/3
A || B
A= $HAS_CHMLIBB= $HAS_HH
ABresult
1X1
011
000
24  
25 17 my $MAGIC = 'ITSF';
26  
27 sub heuristic {
28  
29 115 ▶ my $class = shift;
Subroutine heuristic: called CC=1 SCAR=0.0
Pod undocumented
30 115 my $file = shift;
31 115 my $fh = shift;
32  
33 115 read $fh, my $mag, length $MAGIC;
34  
35 115 return $mag eq $MAGIC;
36  
37 }
38  
39 sub _extract {
40  
41 0 ▶ my $self = shift;
Subroutine _extract: not called CC=5 SCAR=34.0
42  
43 0 ▶ if ($HAS_CHMLIB) {
Branch: 2 branches
BranchTrueFalseTotal
if ($HAS_CHMLIB) { } 0 0 0
elsif ($HAS_HH) { } 0 0 0
44 0 safe_qx('extract_chmLib', $self->{Source}, $self->{_extract});
45 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
46 0 die "Failed to run 'extract_chmLib' on $self->{Source}\n";
47 }
48 } elsif ($HAS_HH) {
49 0 safe_qx('hh.exe', '-decompile', $self->{_extract}, $self->{Source});
50 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
51 0 die "Failed to run 'hh.exe' on $self->{Source}\n";
52 }
53 } else {
54 0 die "Cannot extract CHM $self->{Source}; extract_chmLib nor hh.exe installed\n";
55 }
56  
57  
58 0 return 1;
59  
60 }
61  
62 sub _urlstr {
63  
64 0 ▶ my $self = shift;
Subroutine _urlstr: not called CC=6 SCAR=37.4
65  
66 0 my $strfile = File::Spec->catfile($self->{_extract}, '#URLSTR');
67  
68 0 ▶ unless (-f $strfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $strfile) 0 0 0
69 0 die "Cannot read CHM $self->{Source}; #URLSTR missing\n";
70 }
71  
72 0 ▶ open my $fh, '<', $strfile
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $strfile or die "Failed to open $strfile for reading: $!\n" 0 0 0
73 or die "Failed to open $strfile for reading: $!\n";
74 0 binmode $fh;
75  
76 0 my @urls = split /\0+/, do { local $/ = undef; readline $fh };
77  
78 0 for my $url (@urls) {
79 0 ▶ next unless $url =~ /\.html$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /\.html$/u 0 0 0
80 0 $url = File::Spec->catfile($self->{_extract}, $url);
81 0 ▶ next unless -f $url;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $url 0 0 0
82 0 push @{ $self->{_content} }, $url;
83 }
84  
85 0 close $fh;
86  
87 0 return 1;
88  
89 }
90  
91 sub _hhc {
92  
93 0 ▶ my $self = shift;
Subroutine _hhc: not called CC=3 SCAR=24.8
94  
95 0 my ($hhc) = grep { /\.hhc$/ } dir($self->{_extract});
96  
97 0 ▶ unless (defined $hhc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hhc) 0 0 0
98 0 die "CHM $self->{Source} is missing HHC\n";
99 }
100  
101 my $dom = XML::LibXML->load_html(
102 location => $hhc,
103 recover => 2,
104 no_network => !$self->{Network},
105 0 );
106  
107 0 my @locals = $dom->findnodes('//li/object/param[@name="Local"]');
108  
109 0 @{ $self->{_content} } =
110 0 grep { /\.html?$/ }
111 0 grep { -f }
112 0 map { File::Spec->catfile($self->{_extract}, $_->getAttribute('value')) }
113 0 grep { defined $_->getAttribute('value') }
114 @locals;
115  
116 0 my ($gen) = $dom->findnodes('/html/head/meta[@name="GENERATOR"]/@content');
117  
118 0 ▶ if (defined $gen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gen) 0 0 0
119 0 $self->{Metadata}->contributor([ $gen->value ]);
120 }
121  
122 0 return 1;
123  
124 }
125  
126 sub _images {
127  
128 0 ▶ my $self = shift;
Subroutine _images: not called CC=6 SCAR=37.4
129 0 ▶ my $dir = shift // $self->{_extract};
Condition0%0/3
shift() // $self->{"_extract"}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->{"_extract"}
shift() $self->{"_extract"}
Truth table0%0/3
A // B
A= shift()B= $self->{"_extract"}
ABresult
1X1
011
000
130  
131 0 for my $f (dir($dir)) {
132 0 ▶ if (-d $f) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $f) { } 0 0 0
elsif (-f $f) { } 0 0 0
133 0 $self->_images($f);
134 } elsif (-f $f) {
135 0 my $format = image_path_id($f);
136 0 ▶ next if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
137 0 push @{ $self->{_images} }, [ $f, $format ];
138 }
139 }
140  
141 0 return 1;
142  
143 }
144  
145 sub _clean_html {
146  
147 0 ▶ my $node = shift;
Subroutine _clean_html: not called CC=11 SCAR=48.8
148  
149 0 my @children = grep { $_->isa('XML::LibXML::Element') } $node->childNodes;
150  
151 # Remove the ugly nav bars from the top and bottom of the page. We
152 # determine if a node is a navbar node if it contains an image that is
153 # alt-tagged with either next or prev.
154 0 ▶ if (@children and my ($alt) = $children[0]->findnodes('.//img/@alt')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children and my($alt) = $children[0]->findnodes(".//img/\@alt")) 0 0 0
Condition0%0/3
@children and my($alt) = $children[0]->findnodes(".//img/\@alt")
!ll&&!rl&&r
000
MC/DC0%0/2
@children and my($alt) = $children[0]->findnodes(".//img/\@alt")
@children my($alt) = $children[0]->findnodes(".//img/\@alt")
Truth table0%0/3
A and B
A= @childrenB= my($alt) = $children[0]->findnodes(".//img/\@alt")
ABresult
0X0
100
111
155 0 ▶ if ($alt->value =~ m/next|prev/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alt->value =~ /next|prev/iu) 0 0 0
156 0 $node->removeChild(shift @children);
157 }
158 }
159 0 ▶ if (@children and my ($alt) = $children[-1]->findnodes('.//img/@alt')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children and my($alt) = $children[-1]->findnodes(".//img/\@alt")) 0 0 0
Condition0%0/3
@children and my($alt) = $children[-1]->findnodes(".//img/\@alt")
!ll&&!rl&&r
000
MC/DC0%0/2
@children and my($alt) = $children[-1]->findnodes(".//img/\@alt")
@children my($alt) = $children[-1]->findnodes(".//img/\@alt")
Truth table0%0/3
A and B
A= @childrenB= my($alt) = $children[-1]->findnodes(".//img/\@alt")
ABresult
0X0
100
111
160 0 ▶ if ($alt->value =~ m/next|prev/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alt->value =~ /next|prev/iu) 0 0 0
161 0 $node->removeChild(pop @children);
162 }
163 }
164  
165 # Now get rid of the horizontal space placed before/after each nav bar.
166 0 ▶ if (@children and $children[0]->nodeName eq 'hr') {
Branch: 1 branch
BranchTrueFalseTotal
if (@children and $children[0]->nodeName eq "hr") 0 0 0
Condition0%0/3
@children and $children[0]->nodeName eq "hr"
!ll&&!rl&&r
000
MC/DC0%0/2
@children and $children[0]->nodeName eq "hr"
@children $children[0]->nodeName eq "hr"
Truth table0%0/3
A and B
A= @childrenB= $children[0]->nodeName eq "hr"
ABresult
0X0
100
111
167 0 $node->removeChild(shift @children);
168 }
169 0 ▶ if (@children and $children[-1]->nodeName eq 'hr') {
Branch: 1 branch
BranchTrueFalseTotal
if (@children and $children[-1]->nodeName eq "hr") 0 0 0
Condition0%0/3
@children and $children[-1]->nodeName eq "hr"
!ll&&!rl&&r
000
MC/DC0%0/2
@children and $children[-1]->nodeName eq "hr"
@children $children[-1]->nodeName eq "hr"
Truth table0%0/3
A and B
A= @childrenB= $children[-1]->nodeName eq "hr"
ABresult
0X0
100
111
170 0 $node->removeChild(pop @children);
171 }
172  
173 0 return 1;
174  
175 }
176  
177 sub new {
178  
179 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
180 0 my $file = shift;
181 0 my $enc = shift;
182 0 ▶ my $net = shift // 1;
Condition0%0/2
shift() // 1
l!l
00
MC/DC0%0/1
shift() // 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
183  
184 0 my $self = {
185 Source => undef,
186 Metadata => EBook::Ishmael::EBook::Metadata->new,
187 Network => $net,
188 _extract => undef,
189 _images => [],
190 _content => [],
191 };
192  
193 0 bless $self, $class;
194  
195 0 $self->{Source} = File::Spec->rel2abs($file);
196  
197 0 $self->{_extract} = tempdir(CLEANUP => 1);
198 0 $self->_extract;
199 #$self->_urlstr;
200 0 $self->_hhc;
201 0 $self->_images;
202  
203 0 $self->{Metadata}->set_title((fileparse($self->{Source}, qr/\.[^.]*/))[0]);
204 0 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
205 0 $self->{Metadata}->set_format('CHM');
206  
207 0 return $self;
208  
209 }
210  
211 sub html {
212  
213 0 ▶ my $self = shift;
Subroutine html: not called CC=4 SCAR=30.0
Pod undocumented
214 0 my $out = shift;
215  
216 my $html = join '', map {
217  
218 my $dom = XML::LibXML->load_html(
219 location => $_,
220 recover => 2,
221 no_network => !$self->{Network},
222 0 );
223  
224 0 my ($body) = $dom->findnodes('/html/body');
225 0 ▶ $body //= $dom->documentElement;
Condition0%0/3
$body //= $dom->documentElement
l!l&&r!l&&!r
000
MC/DC0%0/2
$body //= $dom->documentElement
$body $dom->documentElement
Truth table0%0/3
A //= B
A= $bodyB= $dom->documentElement
ABresult
1X1
011
000
226  
227 0 _clean_html($body);
228  
229 0 map { $_->toString } $body->childNodes;
230  
231 0 } @{ $self->{_content} };
232  
233 0 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 0 0
234 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
235 or die "Failed to open $out for writing: $!\n";
236 0 binmode $fh, ':utf8';
237 0 print { $fh } $html;
238 0 close $fh;
239 0 return $out;
240 } else {
241 0 return $html;
242 }
243  
244 }
245  
246 sub raw {
247  
248 0 ▶ my $self = shift;
Subroutine raw: not called CC=4 SCAR=30.0
Pod undocumented
249 0 my $out = shift;
250  
251 my $raw = join '', map {
252  
253 my $dom = XML::LibXML->load_html(
254 location => $_,
255 recover => 2,
256 no_network => !$self->{Network},
257 0 );
258  
259 0 my ($body) = $dom->findnodes('/html/body');
260 0 ▶ $body //= $dom->documentElement;
Condition0%0/3
$body //= $dom->documentElement
l!l&&r!l&&!r
000
MC/DC0%0/2
$body //= $dom->documentElement
$body $dom->documentElement
Truth table0%0/3
A //= B
A= $bodyB= $dom->documentElement
ABresult
1X1
011
000
261  
262 0 $body->textContent;
263  
264 0 } @{ $self->{_content} };
265  
266 0 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 0 0
267 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
268 or die "Failed to open $out for writing: $!\n";
269 0 binmode $fh, ':utf8';
270 0 print { $fh } $raw;
271 0 close $fh;
272 0 return $out;
273 } else {
274 0 return $raw;
275 }
276  
277 }
278  
279 sub metadata {
280  
281 0 ▶ my $self = shift;
Subroutine metadata: not called CC=1 SCAR=6.9
Pod undocumented
282  
283 0 return $self->{Metadata};
284  
285 }
286  
287 sub has_cover {
288  
289 0 ▶ my $self = shift;
Subroutine has_cover: not called CC=1 SCAR=6.9
Pod undocumented
290  
291 0 return $self->image_num > 0;
292  
293 }
294  
295 sub cover {
296  
297 0 ▶ my $self = shift;
Subroutine cover: not called CC=8 SCAR=42.8
Pod undocumented
298  
299 0 ▶ return (undef, undef) unless $self->has_cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cover 0 0 0
300  
301 # Find largest image with a 1.3 height-width ratio, which is most likely
302 # the cover image.
303 0 my $cover;
304 0 for my $i (0 .. $self->image_num - 1) {
305 0 my ($data, $format) = $self->image($i);
306 0 my $size = image_size($data, $format);
307 0 ▶ if (not defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 0 0 0
308 0 next;
309 }
310 # Cover images probably have at least a 1.3 height-width ratio.
311 0 ▶ if ($size->[1] / $size->[0] < 1.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size->[1] / $size->[0] < "1.3") 0 0 0
312 0 next;
313 }
314 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cover or $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]) 0 0 0
Condition0%0/3
not defined $cover or $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cover or $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]
not defined $cover $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]
Truth table0%0/3
not(A) or B
A= not defined $coverB= $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]
ABresult
1X1
011
000
315 not defined $cover or
316 $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]
317 ) {
318 0 $cover = [ $data, $format, $size ];
319 }
320 }
321  
322 0 ▶ if (not defined $cover) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cover) 0 0 0
323 0 return (undef, undef);
324 }
325  
326 0 return ($cover->[0], $cover->[1]);
327  
328 }
329  
330 sub image_num {
331  
332 0 ▶ my $self = shift;
Subroutine image_num: not called CC=1 SCAR=6.9
Pod undocumented
333  
334 0 return scalar @{ $self->{_images} };
335  
336 }
337  
338 sub image {
339  
340 0 ▶ my $self = shift;
Subroutine image: not called CC=3 SCAR=24.8
Pod undocumented
341 0 my $n = shift;
342  
343 0 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 0 0
344 0 return (undef, undef);
345 }
346  
347 0 ▶ 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 0 0
348 or die "Failed to open $self->{_images}[$n][0] for reading: $!\n";
349 0 binmode $fh;
350 0 my $img = do { local $/ = undef; readline $fh };
351 0 close $fh;
352  
353 0 return ($img, $self->{_images}[$n][1]);
354  
355 }
356  
357 17 1;
« blib/lib/EBook/Ishmael/TextBrowserDump.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/FictionBook2.pm »