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

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