blib/lib/EPUB/Parser/File/OPF/Context/Manifest.pm

filter: Statementstmt 100% 66 / 66 Branchbran 71.4% 10 / 14 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 17 / 17 Podpod 88.8% 8 / 9 totaltotal 91.9% 103 / 112 SCAR 24.0
CC
11
Cov
93%
items_path_by_media_type
4.0
items_path
3.1
cover_image_path
3.0
CRAP 11.0
« blib/lib/EPUB/Parser/Util/AttributePacker.pm ↑ Summary blib/lib/EPUB/Parser/File/Parser.pm »
1 package EPUB::Parser::File::OPF::Context::Manifest;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Smart::Args;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use parent 'EPUB::Parser::File::OPF::Context';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use EPUB::Parser::Util::AttributePacker;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub nav_path {
10 4 ▶ my $self = shift;
Subroutine nav_path: called CC=2 SCAR=7.0
Pod documented
11 4 my $nav_path = $self->parser->single('pkg:item[@properties="nav"]/@href')->string_value;
12  
13 4 ▶ croak '<item properties="nav" /> is required for epub3' unless $nav_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $nav_path 0 4 4
14  
15 4 return $nav_path;
16 }
17  
18 sub cover_image_path {
19 2 ▶ args(
Subroutine cover_image_path: called CC=3 SCAR=11.0
Pod documented
20 my $self,
21 my $abs => { optional => 1 },
22 );
23 2 my $cover_img_path = $self->parser->single('pkg:item[@properties="cover-image"]/@href');
24  
25 2 ▶ return unless $cover_img_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $cover_img_path 0 2 2
26  
27 2 ▶ if ($abs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abs) { } 1 1 2
28 1 return $self->{opf}->dir . '/' . $cover_img_path->string_value;
29 }else{
30 1 return $cover_img_path->string_value;
31 }
32 }
33  
34 sub attr_by_media_type {
35 8 ▶ my $self = shift;
Subroutine attr_by_media_type: called CC=1 SCAR=0.0
Pod undocumented
36 8 my $nodes = $self->parser->find('pkg:item');
37 8 EPUB::Parser::Util::AttributePacker->grouped_list($nodes, { group => 'media-type'});
38 }
39  
40 sub attr_by_id {
41 7 ▶ my $self = shift;
Subroutine attr_by_id: called CC=1 SCAR=0.0
Pod documented
42 7 my $nodes = $self->parser->find('pkg:item');
43 7 EPUB::Parser::Util::AttributePacker->by_uniq_key($nodes, { key => 'id'});
44 }
45  
46  
47 sub _items_path {
48 12 ▶ args(
Subroutine _items_path: called CC=2 SCAR=6.9
49 my $self,
50 my $href => 'ArrayRef',
51 my $abs => { optional => 1 },
52 );
53  
54 12 my $base_dir = '';
55 12 ▶ if ($abs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abs) 11 1 12
56 11 $base_dir = $self->{opf}->dir . '/';
57 }
58  
59 12 [map { $base_dir . $_ } @$href];
60 }
61  
62 sub items_path {
63 1 ▶ my $self = shift;
Subroutine items_path: called CC=3 SCAR=11.2
Pod documented
64 1 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
65  
66 1 ▶ my @href = map { $_->{href} } values %{ $self->attr_by_id || {} };
Branch: 1 branch
BranchTrueFalseTotal
$self->attr_by_id or {} 0 1 1
67  
68 1 $self->_items_path({ %$args, href => \@href });
69 }
70  
71 sub items_path_by_media_type {
72 7 ▶ args(
Subroutine items_path_by_media_type: called CC=4 SCAR=13.9
Pod documented
73 my $self,
74 my $abs => { optional => 1 },
75 my $regexp,
76 );
77  
78 7 ▶ my $attr = $self->attr_by_media_type || {};
Condition50%1/2
$self->attr_by_media_type || {}
l!l
70
MC/DC0%0/1
$self->attr_by_media_type || {}
$self->attr_by_media_type
Truth table50%1/2
A
A= $self->attr_by_media_type
Aresult
11
00
79 7 my @href;
80 7 for my $media_type ( keys %$attr ) {
81 28 ▶ next unless $media_type =~ $regexp;
Branch: 1 branch
BranchTrueFalseTotal
unless $media_type =~ /$regexp/ 20 8 28
82 8 push @href, map { $_->{href} } @{$attr->{$media_type}};
83 }
84  
85 7 $self->_items_path({ abs => $abs, href => \@href });
86 }
87  
88  
89 sub _items {
90 10 ▶ my $self = shift;
Subroutine _items: called CC=2 SCAR=7.0
91 10 my $paths = shift;
92 my $it = $self->{opf}->{zip}->get_members({
93 10 files_path => $paths,
94 });
95 10 ▶ return wantarray ? $it->all : $it;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 10 10
96 }
97  
98 sub items {
99 1 ▶ my $self = shift;
Subroutine items: called CC=1 SCAR=0.0
Pod documented
100 1 $self->_items( $self->items_path({ abs => 1 }) );
101 }
102  
103  
104 sub items_by_media {
105 3 ▶ my $self = shift;
Subroutine items_by_media: called CC=1 SCAR=0.0
Pod documented
106 3 $self->_items( $self->items_path_by_media_type({ abs => 1, regexp => qr{image/ | video/ | audio/}ix }) );
107 }
108  
109 sub items_by_media_type {
110 4 ▶ args(
Subroutine items_by_media_type: called CC=1 SCAR=0.0
Pod documented
111 my $self,
112 my $regexp,
113 );
114 4 $self->_items( $self->items_path_by_media_type({ abs => 1, regexp => $regexp }) );
115 }
116  
117  
118 8 1;
119  
120 __END__
121  
122 =encoding utf-8
123  
124 =head1 NAME
125  
126 EPUB::Parser::File::OPF::Context::Manifest - parses manifest node in opf file
127  
128 =head1 METHODS
129  
130 =head2 nav_path
131  
132 Get navigation file path from item element with the property 'nav'.
133  
134 =head2 cover_image_path(\%opt)
135  
136 Get cover image path from item element with the property 'cover-image'.
137 Valid options are:
138  
139 =over 4
140  
141 =item abs
142  
143 $manifest->cover_image_path({ abs => 1 });
144 Get absolute path.
145  
146 =back
147  
148 =head2 attr_by_media_tyep({ regexp => qr{ ... }ix })
149  
150 Retrun in the following format.
151  
152 {
153  
154 'image/png' => [{
155 href => "cover.png", id => "_cover.png", properties => "cover-image"
156 },{
157 href => "fig01.png", id => "_fig01.png"
158 }],
159 'text/css' => [{
160 .....
161 }],
162 }
163  
164 =head2 attr_by_id
165  
166 Retrun in the following format.
167  
168 {
169 "_cover.png" => { "media-type" => "image/png", href => "cover.png", properties => "cover-image" },
170 "_style.css" => { "media-type" => "text/css", href => "style.css" },
171 "_toc.ncx" => { "media-type" => "application/x-dtbncx+xml", href => "toc.ncx" },
172 "_cover.xhtml" => { "media-type" => "application/xhtml+xml", href => "cover.xhtml" },
173 "_nav.xhtml" => { "media-type" => "application/xhtml+xml", href => "nav.xhtml", properties => "nav" },
174 ....
175 }
176  
177  
178 =head2 items_path
179  
180 Returns all item path.
181  
182 =head2 items_path_by_media_type
183  
184 Returns items path with media-type specified by Regular expression.
185  
186 =head2 items
187  
188 Returns all item.
189 The item is instance of L<EPUB::Parser::Util::Archive::Iterator>.
190  
191 =head2 items_by_media
192  
193 Returns items with specified media-type. image/*, video/* ,audio/* .
194 The item is instance of L<EPUB::Parser::Util::Archive::Iterator>.
195  
196 =head2 items_by_media_type({ regexp => qr{ ... }ix })
197  
198 Returns items with media-type specified by Regular expression.
199 The item is instance of L<EPUB::Parser::Util::Archive::Iterator>.
200  
201 =head1 LICENSE
202  
203 Copyright (C) tokubass.
204  
205 This library is free software; you can redistribute it and/or modify
206 it under the same terms as Perl itself.
207  
208 =head1 AUTHOR
209  
210 tokubass E<lt>tokubass {at} cpan.orgE<gt>
211  
212 =cut
213  
« blib/lib/EPUB/Parser/Util/AttributePacker.pm ↑ Summary blib/lib/EPUB/Parser/File/Parser.pm »