« 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.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::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::HTML qw(prepare_html); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 17 | ▶ | use EBook::Ishmael::ImageID qw(image_path_id image_size); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 17 | ▶ | use EBook::Ishmael::ShellQuote qw(safe_qx); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # TODO: Make more of an effort to find metadata | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 17 | my $HAS_CHMLIB = defined which('extract_chmLib'); | |||||||||||||||||||||||||||
| 23 | 17 | my $HAS_HH = defined which('hh.exe'); | |||||||||||||||||||||||||||
| 24 | 17 | ▶ | our $CAN_TEST = $HAS_CHMLIB || $HAS_HH; | ||||||||||||||||||||||||||
|
Condition33%1/3
$HAS_CHMLIB || $HAS_HH
MC/DC0%0/2
$HAS_CHMLIB || $HAS_HH
$HAS_CHMLIB
$HAS_HH
Truth table33%1/3
A || B
A= $HAS_CHMLIBB= $HAS_HH
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 17 | my $MAGIC = 'ITSF'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub heuristic { | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 115 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
heuristic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 115 | my $file = shift; | |||||||||||||||||||||||||||
| 32 | 115 | my $fh = shift; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 115 | read $fh, my $mag, length $MAGIC; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 115 | return $mag eq $MAGIC; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub _extract { | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_extract: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($HAS_CHMLIB) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 0 | safe_qx('extract_chmLib', $self->{Source}, $self->{_extract}); | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | unless ($? >> 8 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | die "Failed to run 'extract_chmLib' on $self->{Source}\n"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } elsif ($HAS_HH) { | ||||||||||||||||||||||||||||
| 50 | 0 | safe_qx('hh.exe', '-decompile', $self->{_extract}, $self->{Source}); | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | unless ($? >> 8 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | die "Failed to run 'hh.exe' on $self->{Source}\n"; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 0 | die "Cannot extract CHM $self->{Source}; extract_chmLib nor hh.exe installed\n"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return 1; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub _urlstr { | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_urlstr: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $strfile = File::Spec->catfile($self->{_extract}, '#URLSTR'); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | unless (-f $strfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | die "Cannot read CHM $self->{Source}; #URLSTR missing\n"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | open my $fh, '<', $strfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | or die "Failed to open $strfile for reading: $!\n"; | ||||||||||||||||||||||||||||
| 75 | 0 | binmode $fh; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my @urls = split /\0+/, do { local $/ = undef; readline $fh }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | for my $url (@urls) { | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | next unless $url =~ /\.html$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | $url = File::Spec->catfile($self->{_extract}, $url); | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | next unless -f $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | push @{ $self->{_content} }, $url; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | close $fh; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | return 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub _hhc { | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_hhc: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my ($hhc) = grep { /\.hhc$/ } dir($self->{_extract}); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | unless (defined $hhc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | die "CHM $self->{Source} is missing HHC\n"; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | my $dom = XML::LibXML->load_html( | ||||||||||||||||||||||||||||
| 103 | location => $hhc, | ||||||||||||||||||||||||||||
| 104 | recover => 2, | ||||||||||||||||||||||||||||
| 105 | no_network => !$self->{Network}, | ||||||||||||||||||||||||||||
| 106 | 0 | ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my @locals = $dom->findnodes('//li/object/param[@name="Local"]'); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | @{ $self->{_content} } = | |||||||||||||||||||||||||||
| 111 | 0 | grep { /\.html?$/ } | |||||||||||||||||||||||||||
| 112 | 0 | grep { -f } | |||||||||||||||||||||||||||
| 113 | 0 | map { File::Spec->catfile($self->{_extract}, $_->getAttribute('value')) } | |||||||||||||||||||||||||||
| 114 | 0 | grep { defined $_->getAttribute('value') } | |||||||||||||||||||||||||||
| 115 | @locals; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my ($gen) = $dom->findnodes('/html/head/meta[@name="GENERATOR"]/@content'); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (defined $gen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $self->{Metadata}->contributor([ $gen->value ]); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return 1; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _images { | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_images: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $dir = shift // $self->{_extract}; | ||||||||||||||||||||||||||
|
Condition0%0/3
shift() // $self->{"_extract"}
MC/DC0%0/2
shift() // $self->{"_extract"}
shift()
$self->{"_extract"}
Truth table0%0/3
A // B
A= shift()B= $self->{"_extract"}
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | for my $f (dir($dir)) { | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (-d $f) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 134 | 0 | $self->_images($f); | |||||||||||||||||||||||||||
| 135 | } elsif (-f $f) { | ||||||||||||||||||||||||||||
| 136 | 0 | my $format = image_path_id($f); | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | next if not defined $format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | push @{ $self->{_images} }, [ $f, $format ]; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | return 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub _clean_html { | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my $node = shift; | ||||||||||||||||||||||||||
|
Subroutine
_clean_html: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | my @children = grep { $_->isa('XML::LibXML::Element') } $node->childNodes; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Remove the ugly nav bars from the top and bottom of the page. We | ||||||||||||||||||||||||||||
| 153 | # determine if a node is a navbar node if it contains an image that is | ||||||||||||||||||||||||||||
| 154 | # alt-tagged with either next or prev. | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (@children and my ($alt) = $children[0]->findnodes('.//img/@alt')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@children and my($alt) = $children[0]->findnodes(".//img/\@alt")
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")
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ($alt->value =~ m/next|prev/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | $node->removeChild(shift @children); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if (@children and my ($alt) = $children[-1]->findnodes('.//img/@alt')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@children and my($alt) = $children[-1]->findnodes(".//img/\@alt")
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")
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($alt->value =~ m/next|prev/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $node->removeChild(pop @children); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # Now get rid of the horizontal space placed before/after each nav bar. | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if (@children and $children[0]->nodeName eq 'hr') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@children and $children[0]->nodeName eq "hr"
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"
| |||||||||||||||||||||||||||||
| 168 | 0 | $node->removeChild(shift @children); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if (@children and $children[-1]->nodeName eq 'hr') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@children and $children[-1]->nodeName eq "hr"
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"
| |||||||||||||||||||||||||||||
| 171 | 0 | $node->removeChild(pop @children); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | return 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub new { | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 182 | 0 | my $enc = shift; | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | my $net = shift // 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() // 1
MC/DC0%0/1
shift() // 1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | my $self = { | |||||||||||||||||||||||||||
| 186 | Source => undef, | ||||||||||||||||||||||||||||
| 187 | Metadata => EBook::Ishmael::EBook::Metadata->new, | ||||||||||||||||||||||||||||
| 188 | Network => $net, | ||||||||||||||||||||||||||||
| 189 | _extract => undef, | ||||||||||||||||||||||||||||
| 190 | _images => [], | ||||||||||||||||||||||||||||
| 191 | _content => [], | ||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | $self->{Source} = File::Spec->rel2abs($file); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | $self->{_extract} = tempdir(CLEANUP => 1); | |||||||||||||||||||||||||||
| 199 | 0 | $self->_extract; | |||||||||||||||||||||||||||
| 200 | #$self->_urlstr; | ||||||||||||||||||||||||||||
| 201 | 0 | $self->_hhc; | |||||||||||||||||||||||||||
| 202 | 0 | $self->_images; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | $self->{Metadata}->set_title((fileparse($self->{Source}, qr/\.[^.]*/))[0]); | |||||||||||||||||||||||||||
| 205 | 0 | $self->{Metadata}->set_modified((stat $self->{Source})[9]); | |||||||||||||||||||||||||||
| 206 | 0 | $self->{Metadata}->set_format('CHM'); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | return $self; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | sub html { | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
html: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | 0 | my $out = shift; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | my $html = join '', map { | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my $dom = XML::LibXML->load_html( | ||||||||||||||||||||||||||||
| 220 | location => $_, | ||||||||||||||||||||||||||||
| 221 | recover => 2, | ||||||||||||||||||||||||||||
| 222 | no_network => !$self->{Network}, | ||||||||||||||||||||||||||||
| 223 | 0 | ); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | my ($body) = $dom->findnodes('/html/body'); | |||||||||||||||||||||||||||
| 226 | 0 | ▶ | $body //= $dom->documentElement; | ||||||||||||||||||||||||||
|
Condition0%0/3
$body //= $dom->documentElement
MC/DC0%0/2
$body //= $dom->documentElement
$body
$dom->documentElement
Truth table0%0/3
A //= B
A= $bodyB= $dom->documentElement
| |||||||||||||||||||||||||||||
| 227 | 0 | _clean_html($body); | |||||||||||||||||||||||||||
| 228 | 0 | prepare_html($body); | |||||||||||||||||||||||||||
| 229 | 0 | map { $_->toString } $body->childNodes; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | } @{ $self->{_content} }; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | if (defined $out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 0 | ▶ | open my $fh, '>', $out | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$body //= $dom->documentElement
$body
$dom->documentElement
Truth table0%0/3
A //= B
A= $bodyB= $dom->documentElement
| |||||||||||||||||||||||||||||
| 261 | 0 | prepare_html($body); | |||||||||||||||||||||||||||
| 262 | 0 | $body->textContent; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | } @{ $self->{_content} }; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | if (defined $out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 0 | ▶ | open my $fh, '>', $out | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 312 | 0 | next; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $cover or $cover->[2][0] * $cover->[2][1] < $size->[1] * $size->[0]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 344 | 0 | return (undef, undef); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 0 | ▶ | open my $fh, '<', $self->{_images}[$n][0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||