blib/lib/EBook/Ishmael/EBook/FictionBook2.pm

filter: Statementstmt 86.8% 145 / 167 Branchbran 60.2% 47 / 78 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 9 totaltotal 75.9% 218 / 287 CC 47 SCAR 33.5
CC
47
Cov
78%
_read_metadata
51.5
raw
5.3
html
5.2
CRAP 28.6
« blib/lib/EBook/Ishmael/EBook/CHM.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/EUCJP.pm »
1 package EBook::Ishmael::EBook::FictionBook2;
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::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use MIME::Base64;
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::ImageID qw(mimetype_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use EBook::Ishmael::Time qw(guess_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 17 my $NS = "http://www.gribuser.ru/xml/fictionbook/2.0";
18  
19 sub heuristic {
20  
21 104 ▶ my $class = shift;
Subroutine heuristic: called CC=3 SCAR=11.0
Pod undocumented
22 104 my $file = shift;
23 104 my $fh = shift;
24  
25 104 ▶ return 1 if $file =~ /\.fb2$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.fb2$/u 11 93 104
26 93 ▶ return 0 unless -T $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless -T $fh 58 35 93
27  
28 35 read $fh, my ($head), 1024;
29  
30 35 return $head =~ /<\s*FictionBook[^<>]+xmlns\s*=\s*"\Q$NS\E"[^<>]*>/;
31  
32 }
33  
34 sub _read_metadata {
35  
36 10 ▶ my $self = shift;
Subroutine _read_metadata: called CC=35 SCAR=39.4
37  
38 10 my $ns = $self->{_dom}->documentElement->namespaceURI;
39  
40 10 my $xpc = XML::LibXML::XPathContext->new($self->{_dom});
41 10 $xpc->registerNs('FictionBook', $ns);
42  
43 10 ▶ my ($desc) = $xpc->findnodes(
Branch: 1 branch
BranchTrueFalseTotal
my($desc) = $xpc->findnodes("/FictionBook:FictionBook/FictionBook:description") or return 1 0 10 10
44 '/FictionBook:FictionBook' .
45 '/FictionBook:description'
46 ) or return 1;
47  
48 10 my ($title) = $xpc->findnodes('./FictionBook:title-info', $desc);
49 10 my ($doc) = $xpc->findnodes('./FictionBook:document-info', $desc);
50 10 my ($publish) = $xpc->findnodes('./FictionBook:publish-info', $desc);
51  
52 10 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) 10 0 10
53 10 for my $n ($title->childNodes) {
54 110 my $name = $n->nodeName;
55 110 ▶ if ($name eq 'genre') {
Branch: 5 branches
BranchTrueFalseTotal
if ($name eq 'genre') { } 10 100 110
elsif ($name eq 'author') { } 10 90 100
elsif ($name eq 'book-title') { } 10 80 90
elsif ($name eq 'lang' or $name eq 'src-lang') { } 10 70 80
elsif ($name eq 'translator') { } 0 70 70
56 10 $self->{Metadata}->add_genre($n->textContent);
57 } elsif ($name eq 'author') {
58 $self->{Metadata}->add_author(
59 10 join(' ', grep { /\S/ } map { $_->textContent } $n->childNodes)
60 );
61 } elsif ($name eq 'book-title') {
62 10 $self->{Metadata}->set_title($n->textContent);
63 ▶ } elsif ($name eq 'lang' or $name eq 'src-lang') {
Condition66%2/3
$name eq "lang" or $name eq "src-lang"
l!l&&r!l&&!r
10070
MC/DC50%1/2
$name eq "lang" or $name eq "src-lang"
$name eq "lang" $name eq "src-lang"
Truth table66%2/3
A or B
A= $name eq "lang"B= $name eq "src-lang"
ABresult
1X1
011
000
64 10 $self->{Metadata}->add_language($n->textContent);
65 } elsif ($name eq 'translator') {
66 0 $self->{Metadata}->add_contributor($n->textContent);
67 }
68 }
69 }
70  
71 10 ▶ if (defined $doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc) 10 0 10
72 10 for my $n ($doc->childNodes) {
73 110 my $name = $n->nodeName;
74 110 ▶ if ($name eq 'author') {
Branch: 6 branches
BranchTrueFalseTotal
if ($name eq 'author') { } 10 100 110
elsif ($name eq 'program-used') { } 10 90 100
elsif ($name eq 'date') { } 10 80 90
elsif ($name eq 'id') { } 10 70 80
elsif ($name eq 'version') { } 10 60 70
elsif ($name eq 'src-ocr') { } 0 60 60
75 $self->{Metadata}->add_contributor(
76 10 join(' ', grep { /\S/ } map { $_->textContent } $n->childNodes)
77 );
78 } elsif ($name eq 'program-used') {
79 10 $self->{Metadata}->set_software($n->textContent);
80 } elsif ($name eq 'date') {
81 10 my $t = eval { guess_time($n->textContent) };
82 10 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 10 0 10
83 10 $self->{Metadata}->set_created($t);
84 }
85 } elsif ($name eq 'id') {
86 10 $self->{Metadata}->set_id($n->textContent);
87 } elsif ($name eq 'version') {
88 10 $self->{Metadata}->set_format("FictionBook2 " . $n->textContent);
89 } elsif ($name eq 'src-ocr') {
90 0 $self->{Metadata}->add_author($n->textContent);
91 }
92 }
93 }
94  
95 10 ▶ if (defined $publish) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $publish) 10 0 10
96 10 for my $n ($publish->childNodes) {
97 30 my $name = $n->nodeName;
98 30 ▶ if ($name eq 'year' and not defined $self->{Metadata}->created) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'year' and not defined $self->{'Metadata'}->created) { } 0 30 30
elsif ($name eq 'publisher') { } 0 30 30
elsif ($name eq 'book-name') { } 0 30 30
Condition66%2/3
$name eq "year" and not defined $self->{"Metadata"}->created
!ll&&!rl&&r
20100
MC/DC0%0/2
$name eq "year" and not defined $self->{"Metadata"}->created
$name eq "year" not defined $self->{"Metadata"}->created
Truth table66%2/3
A and not(B)
A= $name eq "year"B= not defined $self->{"Metadata"}->created
ABresult
0X0
100
111
99 0 my $t = eval { guess_time($n->textContent) };
100 0 ▶ if (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 0 0 0
101 0 $self->{Metadata}->set_created($t);
102 }
103 } elsif ($name eq 'publisher') {
104 0 $self->{Metadata}->add_contributor($n->textContent);
105 } elsif ($name eq 'book-name') {
106 0 $self->{Metadata}->set_title($n->textContent);
107 }
108 }
109 }
110  
111 10 for my $n ($xpc->findnodes('/FictionBook:FictionBook/FictionBook:binary')) {
112 10 my $mime = $n->getAttribute('content-type');
113 10 ▶ next if not defined $mime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mime 0 10 10
114 10 my $format = mimetype_id($mime);
115 10 ▶ next if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 10 10
116 10 push @{ $self->{_images} }, [ $n, $format ];
117 }
118  
119 10 my ($covmeta) = $xpc->findnodes('./FictionBook:coverpage', $title);
120 # Put if code inside own block so we can easily last out of it.
121 10 ▶ if (defined $covmeta) {{
Branch: 1 branch
BranchTrueFalseTotal
if (defined $covmeta) 10 0 10
122 10 ▶ my ($img) = $xpc->findnodes('./FictionBook:image', $covmeta)
Branch: 1 branch
BranchTrueFalseTotal
my($img) = $xpc->findnodes("./FictionBook:image", $covmeta) or last 0 10 10
123 or last;
124 10 ▶ my $href = $img->getAttribute('l:href') or last;
Branch: 1 branch
BranchTrueFalseTotal
my $href = $img->getAttribute("l:href") or last 0 10 10
125 10 $href =~ s/^#//;
126 10 ▶ my ($binary) = $xpc->findnodes(
Branch: 1 branch
BranchTrueFalseTotal
my($binary) = $xpc->findnodes(qq[/FictionBook:FictionBook/FictionBook:binary[\@id="$href"]]) or last 0 10 10
127 "/FictionBook:FictionBook/FictionBook:binary[\@id=\"$href\"]"
128 ) or last;
129 10 my $mime = $binary->getAttribute('content-type');
130 10 ▶ last if not defined $mime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mime 0 10 10
131 10 my $format = mimetype_id($mime);
132 10 ▶ last if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 10 10
133 10 $self->{_cover} = [ $binary, $format ];
134 }}
135  
136 10 return 1;
137  
138 }
139  
140 sub new {
141  
142 10 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
143 10 my $file = shift;
144 10 my $enc = shift;
145 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
146  
147 10 my $self = {
148 Source => undef,
149 Metadata => EBook::Ishmael::EBook::Metadata->new,
150 Network => $net,
151 _dom => undef,
152 _cover => undef,
153 _images => [],
154 };
155  
156 10 bless $self, $class;
157  
158 10 $self->{Source} = File::Spec->rel2abs($file);
159  
160 $self->{_dom} = XML::LibXML->load_xml(
161 location => $file,
162 no_network => !$self->{Network},
163 10 );
164  
165 10 $self->_read_metadata;
166  
167 10 ▶ if (not defined $self->{Metadata}->format) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Metadata"}->format) 0 10 10
168 0 $self->{Metadata}->set_format('FictionBook2');
169 }
170  
171 10 return $self;
172  
173 }
174  
175 sub html {
176  
177 3 ▶ my $self = shift;
Subroutine html: called CC=4 SCAR=16.4
Pod undocumented
178 3 my $out = shift;
179  
180 3 my $ns = $self->{_dom}->documentElement->namespaceURI;
181  
182 3 my $xpc = XML::LibXML::XPathContext->new($self->{_dom});
183 3 $xpc->registerNs('FictionBook', $ns);
184  
185 3 ▶ my @bodies = $xpc->findnodes(
Branch: 1 branch
BranchTrueFalseTotal
my(@bodies) = $xpc->findnodes("/FictionBook:FictionBook/FictionBook:body") or die qq[Invalid FictionBook2 file $self->{"Source"}\n] 0 3 3
186 '/FictionBook:FictionBook' .
187 '/FictionBook:body'
188 ) or die "Invalid FictionBook2 file $self->{Source}\n";
189 3 prepare_html(@bodies);
190  
191 my $html = join '',
192 9 map { $_->toString }
193 3 map { $_->childNodes }
194 @bodies;
195  
196 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
197 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
198 or die "Failed to open $out for writing: $!\n";
199 0 binmode $fh, ':utf8';
200 0 print { $fh } $html;
201 0 close $fh;
202 0 return $out;
203 } else {
204 3 return $html;
205 }
206  
207 }
208  
209 sub raw {
210  
211 2 ▶ my $self = shift;
Subroutine raw: called CC=4 SCAR=16.7
Pod undocumented
212 2 my $out = shift;
213  
214 2 my $ns = $self->{_dom}->documentElement->namespaceURI;
215  
216 2 my $xpc = XML::LibXML::XPathContext->new($self->{_dom});
217 2 $xpc->registerNs('FictionBook', $ns);
218  
219 2 ▶ my @bodies = $xpc->findnodes(
Branch: 1 branch
BranchTrueFalseTotal
my(@bodies) = $xpc->findnodes("/FictionBook:FictionBook/FictionBook:body") or die qq[Invalid FictionBook2 file $self->{"Source"}\n] 0 2 2
220 '/FictionBook:FictionBook' .
221 '/FictionBook:body'
222 ) or die "Invalid FictionBook2 file $self->{Source}\n";
223 2 prepare_html(@bodies);
224  
225 2 my $raw = join '', map { $_->textContent } @bodies;
226  
227 2 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 2 2
228 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
229 or die "Failed to open $out for writing: $!\n";
230 0 binmode $fh, ':utf8';
231 0 print { $fh } $raw;
232 0 close $fh;
233 0 return $out;
234 } else {
235 2 return $raw;
236 }
237  
238 }
239  
240 sub metadata {
241  
242 4 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
243  
244 4 return $self->{Metadata};
245  
246 }
247  
248 sub has_cover {
249  
250 4 ▶ my $self = shift;
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
251  
252 4 return defined $self->{_cover};
253  
254 }
255  
256 sub cover {
257  
258 2 ▶ my $self = shift;
Subroutine cover: called CC=2 SCAR=7.0
Pod undocumented
259  
260 2 ▶ return (undef, undef) unless $self->has_cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cover 0 2 2
261 2 my $bin = decode_base64($self->{_cover}[0]->textContent);
262 2 return ($bin, $self->{_cover}[1]);
263  
264 }
265  
266 sub image_num {
267  
268 4 ▶ my $self = shift;
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
269  
270 4 return scalar @{ $self->{_images} };
271  
272 }
273  
274 sub image {
275  
276 2 ▶ my $self = shift;
Subroutine image: called CC=2 SCAR=7.2
Pod undocumented
277 2 my $n = shift;
278  
279 2 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 2 2
280 0 return (undef, undef);
281 }
282  
283 2 my $img = decode_base64($self->{_images}[$n][0]->textContent);
284 2 return ($img, $self->{_images}[$n][1]);
285  
286 }
287  
288 17 1;
« blib/lib/EBook/Ishmael/EBook/CHM.pm ↑ Summary blib/lib/EBook/Ishmael/CharDet/EUCJP.pm »