| 1 | package EBook::Ishmael::EBook::HTML; | ||||||||||||||||||||||||||||||||||||||
| 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::Spec; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use EBook::Ishmael::EBook::Metadata; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 17 | my $XHTML_NS = 'http://www.w3.org/1999/xhtml'; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | sub heuristic { | ||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | 93 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
heuristic: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 19 | 93 | my $file = shift; | |||||||||||||||||||||||||||||||||||||
| 20 | 93 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | 93 | ▶ | return 1 if $file =~ /\.html?$/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 23 | 81 | ▶ | return 0 unless -T $fh; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 23 | read $fh, my ($head), 1024; | |||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 23 | ▶ | return 0 if $head =~ /<[^<>]+xmlns\s*=\s*"\Q$XHTML_NS\E"[^<>]*>/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | 12 | return $head =~ /<\s*html[^<>]*>/; | |||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | sub _read_metadata { | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | 21 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_metadata: called CC=20 SCAR=40.8
| |||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | 21 | my ($ns) = $self->{_dom}->findnodes('/html/@xmlns'); | |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 21 | ▶ | if (defined $ns and $ns->value eq $XHTML_NS) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ns and $ns->value eq $XHTML_NS
MC/DC50%1/2
defined $ns and $ns->value eq $XHTML_NS
defined $ns
$ns->value eq $XHTML_NS
Truth table66%2/3
A and B
A= defined $nsB= $ns->value eq $XHTML_NS
| |||||||||||||||||||||||||||||||||||||||
| 40 | 10 | $self->{Metadata}->set_format('XHTML'); | |||||||||||||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||||||||||||
| 42 | 11 | $self->{Metadata}->set_format('HTML'); | |||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||
| 44 | 21 | my ($head) = $self->{_dom}->findnodes('/html/head'); | |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 21 | ▶ | unless (defined $head) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | 21 | my ($title) = $head->findnodes('./title'); | |||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | 21 | ▶ | if (defined $title) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 53 | 21 | my $str = $title->textContent =~ s/\s+/ /gr; | |||||||||||||||||||||||||||||||||||||
| 54 | 21 | $self->{Metadata}->set_title($str); | |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | 21 | for my $n ($head->findnodes('./meta')) { | |||||||||||||||||||||||||||||||||||||
| 58 | 32 | my $name = $n->getAttribute('name'); | |||||||||||||||||||||||||||||||||||||
| 59 | 32 | ▶ | if (not defined $name) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 60 | 21 | next; | |||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||
| 62 | 11 | my $content = $n->getAttribute('content'); | |||||||||||||||||||||||||||||||||||||
| 63 | 11 | ▶ | if (not defined $content) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 64 | 0 | next; | |||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | if ($name eq 'dc.title') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->{Metadata}->set_title($content); | |||||||||||||||||||||||||||||||||||||
| 68 | } elsif ($name eq 'dc.language') { | ||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $self->{Metadata}->add_language($content); | |||||||||||||||||||||||||||||||||||||
| 70 | } elsif ($name eq 'dcterms.modified') { | ||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $t = eval { guess_time($content) }; | |||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (defined $t) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->{Metadata}->set_modified($t); | |||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | } elsif ($name eq 'dc.creator') { | ||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self->{Metadata}->add_author($content); | |||||||||||||||||||||||||||||||||||||
| 77 | } elsif ($name eq 'dc.subject') { | ||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $self->{Metadata}->add_genre($content); | |||||||||||||||||||||||||||||||||||||
| 79 | } elsif ($name eq 'dcterms.created') { | ||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $t = eval { guess_time($content) }; | |||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (defined $t) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->{Metadata}->set_created($t); | |||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||
| 84 | } elsif ($name eq 'generator') { | ||||||||||||||||||||||||||||||||||||||
| 85 | 11 | $self->{Metadata}->set_software($content); | |||||||||||||||||||||||||||||||||||||
| 86 | } elsif ($name eq 'description') { | ||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->{Metadata}->set_description($content); | |||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | 21 | my ($lang) = $self->{_dom}->findnodes('/html/@lang'); | |||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | 21 | ▶ | if (defined $lang and not defined $self->{Metadata}->language) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $lang and not defined $self->{"Metadata"}->language
MC/DC0%0/2
defined $lang and not defined $self->{"Metadata"}->language
defined $lang
not defined $self->{"Metadata"}->language
Truth table33%1/3
A and not(B)
A= defined $langB= not defined $self->{"Metadata"}->language
| |||||||||||||||||||||||||||||||||||||||
| 94 | 21 | $self->{Metadata}->add_language($lang->value); | |||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||
| 98 | 21 | return 1; | |||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | sub new { | ||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | 21 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 105 | 21 | my $file = shift; | |||||||||||||||||||||||||||||||||||||
| 106 | 21 | my $enc = shift; | |||||||||||||||||||||||||||||||||||||
| 107 | 21 | ▶ | my $net = shift // 1; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() // 1
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | 21 | my $self = { | |||||||||||||||||||||||||||||||||||||
| 110 | Source => undef, | ||||||||||||||||||||||||||||||||||||||
| 111 | Metadata => EBook::Ishmael::EBook::Metadata->new, | ||||||||||||||||||||||||||||||||||||||
| 112 | Encode => $enc, | ||||||||||||||||||||||||||||||||||||||
| 113 | Network => $net, | ||||||||||||||||||||||||||||||||||||||
| 114 | _dom => undef, | ||||||||||||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | 21 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | 21 | $self->{Source} = File::Spec->rel2abs($file); | |||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | $self->{_dom} = XML::LibXML->load_html( | ||||||||||||||||||||||||||||||||||||||
| 122 | location => $file, | ||||||||||||||||||||||||||||||||||||||
| 123 | no_network => !$self->{Network}, | ||||||||||||||||||||||||||||||||||||||
| 124 | recover => 2, | ||||||||||||||||||||||||||||||||||||||
| 125 | encoding => $self->{Encode}, | ||||||||||||||||||||||||||||||||||||||
| 126 | 21 | ); | |||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | 21 | $self->_read_metadata; | |||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 21 | ▶ | if (not defined $self->{Metadata}->title) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $self->{Metadata}->set_title((fileparse($file, qr/\.[^.]*/))[0]); | |||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 21 | return $self; | |||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | sub html { | ||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
html: called CC=4 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 141 | 6 | my $out = shift; | |||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | # Extract body from HTML tree and serialize that, or just serialize the | ||||||||||||||||||||||||||||||||||||||
| 144 | # entire tree if there is no body. | ||||||||||||||||||||||||||||||||||||||
| 145 | 6 | my ($body) = $self->{_dom}->documentElement->findnodes('/html/body'); | |||||||||||||||||||||||||||||||||||||
| 146 | 6 | ▶ | $body //= $self->{_dom}->documentElement; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$body //= $self->{"_dom"}->documentElement
MC/DC0%0/2
$body //= $self->{"_dom"}->documentElement
$body
$self->{"_dom"}->documentElement
Truth table33%1/3
A //= B
A= $bodyB= $self->{"_dom"}->documentElement
| |||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | 6 | my $html = join '', map { $_->toString } $body->childNodes; | |||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | 6 | ▶ | if (defined $out) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | open my $fh, '>', $out | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 152 | or die "Failed to open $out for writing: $!\n"; | ||||||||||||||||||||||||||||||||||||||
| 153 | 0 | binmode $fh, ':utf8'; | |||||||||||||||||||||||||||||||||||||
| 154 | 0 | print { $fh } $html; | |||||||||||||||||||||||||||||||||||||
| 155 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 156 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||||||||||||
| 158 | 6 | return $html; | |||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | sub raw { | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
raw: called CC=4 SCAR=19.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 166 | 5 | my $out = shift; | |||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | 5 | my ($body) = $self->{_dom}->findnodes('/html/body'); | |||||||||||||||||||||||||||||||||||||
| 169 | 5 | ▶ | $body //= $self->{_dom}->documentElement; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$body //= $self->{"_dom"}->documentElement
MC/DC0%0/2
$body //= $self->{"_dom"}->documentElement
$body
$self->{"_dom"}->documentElement
Truth table33%1/3
A //= B
A= $bodyB= $self->{"_dom"}->documentElement
| |||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | 5 | my $raw = $body->textContent; | |||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | 5 | ▶ | if (defined $out) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | open my $fh, '>', $out | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 175 | or die "Failed to open $out for writing: $!\n"; | ||||||||||||||||||||||||||||||||||||||
| 176 | 0 | binmode $fh, ':utf8'; | |||||||||||||||||||||||||||||||||||||
| 177 | 0 | print { $fh } $raw; | |||||||||||||||||||||||||||||||||||||
| 178 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 179 | 0 | return $out; | |||||||||||||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||||||||||||
| 181 | 5 | return $raw; | |||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | sub metadata { | ||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||
| 188 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
metadata: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||
| 190 | 8 | return $self->{Metadata}; | |||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||
| 194 | 4 | ▶ | sub has_cover { 0 } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
has_cover: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||
| 196 | 2 | ▶ | sub cover { (undef, undef) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cover: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||
| 198 | 4 | ▶ | sub image_num { 0 } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
image_num: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||
| 200 | 2 | ▶ | sub image { (undef, undef) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
image: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||
| 202 | 17 | 1; | |||||||||||||||||||||||||||||||||||||