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