blib/lib/EPUB/Parser.pm

filter: Statementstmt 100% 50 / 50 Branchbran n/a Conditioncond 50.0% 8 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 82.1% 83 / 101 SCAR 19.6
CC
7
Cov
88%
new
3.2
pages_manager
2.3
_load_epub
2.1
CRAP 7.1
« blib/lib/EPUB/Parser/Util/EpubLoad.pm ↑ Summary blib/lib/EPUB/Parser/File/Container.pm »
1 package EPUB::Parser;
2 14 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use EPUB::Parser::Util::EpubLoad;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use EPUB::Parser::Util::Archive;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use EPUB::Parser::File::OPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use EPUB::Parser::File::Navi;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use EPUB::Parser::Manager::Pages;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 use EPUB::Parser::Util::ShortcutMethod qw/
12 title creator language identifier
13 items_by_media items_by_media_type
14 toc_list
15 14 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 14 our $VERSION = "0.06";
19  
20 sub new {
21 15 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.7
Pod documented
22 15 ▶ my $args = shift || {};
Condition50%1/2
shift() || {}
l!l
015
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
23  
24 bless {
25 zip => '',
26 opf => '',
27 15 ▶ epub_version => $args->{epub_version} || '3.0',
Condition50%1/2
$args->{'epub_version'} || '3.0'
l!l
015
MC/DC0%0/1
$args->{'epub_version'} || '3.0'
$args->{'epub_version'}
Truth table50%1/2
A
A= $args->{'epub_version'}
Aresult
11
00
28 } => $class;
29 }
30  
31 sub opf {
32 31 ▶ my $self = shift;
Subroutine opf: called CC=2 SCAR=7.1
Pod documented
33  
34 $self->{opf} ||= EPUB::Parser::File::OPF->new({
35 zip => $self->{zip},
36 epub_version => $self->{epub_version},
37 31 ▶ });
Condition66%2/3
$self->{"opf"} ||= "EPUB::Parser::File::OPF"->new({"zip", $self->{"zip"}, "epub_version", $self->{"epub_version"}})
l!l&&r!l&&!r
21100
MC/DC0%0/2
$self->{"opf"} ||= "EPUB::Parser::File::OPF"->new({"zip", $self->{"zip"}, "epub_version", $self->{"epub_version"}})
$self->{"opf"} "EPUB::Parser::File::OPF"->new({"zip", $self->{"zip"}, "epub_version", $self->{"epub_version"}})
Truth table66%2/3
A ||= B
A= $self->{"opf"}B= "EPUB::Parser::File::OPF"->new({"zip", $self->{"zip"}, "epub_version", $self->{"epub_version"}})
ABresult
1X1
011
000
38 }
39  
40 sub navi {
41 7 ▶ my $self = shift;
Subroutine navi: called CC=2 SCAR=7.1
Pod documented
42  
43 $self->{navi} ||= EPUB::Parser::File::Navi->new({
44 zip => $self->{zip},
45 7 ▶ path => $self->opf->nav_path,
Condition66%2/3
$self->{"navi"} ||= "EPUB::Parser::File::Navi"->new({"zip", $self->{"zip"}, "path", $self->opf->nav_path})
l!l&&r!l&&!r
430
MC/DC0%0/2
$self->{"navi"} ||= "EPUB::Parser::File::Navi"->new({"zip", $self->{"zip"}, "path", $self->opf->nav_path})
$self->{"navi"} "EPUB::Parser::File::Navi"->new({"zip", $self->{"zip"}, "path", $self->opf->nav_path})
Truth table66%2/3
A ||= B
A= $self->{"navi"}B= "EPUB::Parser::File::Navi"->new({"zip", $self->{"zip"}, "path", $self->opf->nav_path})
ABresult
1X1
011
000
46 });
47 }
48  
49 sub data_from_path {
50 1 ▶ my $self = shift;
Subroutine data_from_path: called CC=1 SCAR=0.0
Pod documented
51 1 my $path = shift;
52 1 $self->{zip}->get_member_data({ file_path => $path });
53 }
54  
55 sub pages_manager {
56 1 ▶ my $self = shift;
Subroutine pages_manager: called CC=2 SCAR=8.1
Pod documented
57 1 ▶ $self->{pages_manager} ||= EPUB::Parser::Manager::Pages->new({
Condition33%1/3
$self->{"pages_manager"} ||= "EPUB::Parser::Manager::Pages"->new({"opf", $self->opf, "navi", $self->navi})
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"pages_manager"} ||= "EPUB::Parser::Manager::Pages"->new({"opf", $self->opf, "navi", $self->navi})
$self->{"pages_manager"} "EPUB::Parser::Manager::Pages"->new({"opf", $self->opf, "navi", $self->navi})
Truth table33%1/3
A ||= B
A= $self->{"pages_manager"}B= "EPUB::Parser::Manager::Pages"->new({"opf", $self->opf, "navi", $self->navi})
ABresult
1X1
011
000
58 opf => $self->opf,
59 navi => $self->navi,
60 });
61 }
62  
63  
64 sub _load_epub {
65 14 ▶ my ($self,$args,$method) = @_;
Subroutine _load_epub: called CC=2 SCAR=7.2
66 14 ▶ $self->{zip} ||= do {
Condition33%1/3
$self->{"zip"} ||= do {
l!l&&r!l&&!r
0140
MC/DC0%0/2
$self->{"zip"} ||= do { my $data = "EPUB::Parser::Util::EpubLoad"->$method($args); "EPUB::Parser::Util::Archive"->new({"data", $data}) }
$self->{"zip"} do { my $data = "EPUB::Parser::Util::EpubLoad"->$method($args); "EPUB::Parser::Util::Archive"->new({"data", $data}) }
Truth table33%1/3
A ||= B
A= $self->{"zip"}B= do { my $data = "EPUB::Parser::Util::EpubLoad"->$method($args); "EPUB::Parser::Util::Archive"->new({"data", $data}) }
ABresult
1X1
011
000
67 14 my $data = EPUB::Parser::Util::EpubLoad->$method($args);
68 14 EPUB::Parser::Util::Archive->new({ data => $data });
69 };
70 14 return $self;
71 }
72  
73 13 ▶sub load_file { _load_epub(@_, 'load_file' ) };
Subroutine load_file: called CC=1 SCAR=0.0
Pod documented
74 1 ▶sub load_binary { _load_epub(@_, 'load_binary') };
Subroutine load_binary: called CC=1 SCAR=0.0
Pod documented
75  
76 14 1;
77  
78 __END__
79  
80 =encoding utf-8
81  
82 =head1 NAME
83  
84 EPUB::Parser - EPUB Parser class
85  
86 =head1 SYNOPSIS
87  
88 use EPUB::Parser;
89 my $ep = EPUB::Parser->new;
90  
91 # load epub
92 $ep->load_file({ file_path => 'sample.epub' });
93 # or
94 $ep->load_binary({ data => $binary_data })
95  
96 # get opf version
97 my $version = $ep->opf->guess_version;
98  
99 # get css. Return value is 'EPUB::Parser::Util::Archive::Iterator' object.
100 my $itr = $ep->items_by_media_type({ regexp => qr{text/css}ix });
101 while ( my $zip_member = $itr->next ) {
102 $zip_member->data;
103 $zip_member->path;
104 }
105  
106 # shortcut method. iterator object contain image,audio,video item path.
107 my $itr = $ep->items_by_media;
108  
109 # get list under <nav id="toc" epub:type="toc">
110 # todo: parse nested list
111 for my $chapter ( $ep->toc_list ) {
112 $chapter->{title};
113 $chapter->{href};
114 }
115  
116 # get cover image blob
117 my $cover_img_path = $ep->opf->cover_image_path;
118 $ep->data_from_path($cover_img_path);
119  
120 # get page list from each chapter.
121 my $collect_pages = $ep->pages_manager->get_page_from_each_chapter;
122 # no_chapter_member => [
123 # 'OEBPS/cover.xhtml',
124 # 'OEBPS/nav.xhtml'
125 # ],
126 # chapter_group => [
127 # [
128 # 'OEBPS/0_1.xhtml'
129 # 'OEBPS/0_2.xhtml'
130 # 'OEBPS/0_3.xhtml'
131 # ],
132 # [
133 # 'OEBPS/1_1.xhtml'
134 # 'OEBPS/1_2.xhtml'
135 # 'OEBPS/1_3.xhtml'
136 # ],
137 # ....
138 # ]
139  
140 =head1 DESCRIPTION
141  
142 EPUB::Parser parse EPUB3 and return Perl Data Structure.
143 This module can only parse EPUB3.
144  
145 =head1 METHODS
146  
147 =head2 new(\%opts)
148  
149 Constructor.
150 Creates a new EPUB::Parser instance. Valid options are:
151  
152 =over 4
153  
154 =item epub_version
155  
156 EPUB::Parser->new({ epub_version => '3.0' });
157 epub_version is default 3.0 and current supoprt only 3.0.
158  
159 =back
160  
161 =head2 opf
162  
163 Returns instance of L<EPUB::Parser::File::OPF>.
164  
165 =head2 navi
166  
167 Returns instance of L<EPUB::Parser::File::Navi>.
168  
169 =head2 data_from_path($path)
170  
171 get blob from loaded EPUB with path indicated in $path.
172  
173 =head2 pages_manager
174  
175 Returns instance of L<EPUB::Parser::Manager::Pages>.
176  
177 =head2 load_file({ file_path => 'sample.epub' })
178  
179 load from EPUB file.
180  
181 =head2 load_binary({ data => $binary_data })
182  
183 load from EPUB blob.
184  
185 =head1 LICENSE
186  
187 Copyright (C) tokubass.
188  
189 This library is free software; you can redistribute it and/or modify
190 it under the same terms as Perl itself.
191  
192 =head1 AUTHOR
193  
194 tokubass E<lt>tokubass {at} cpan.orgE<gt>
195  
196 =cut
197  
« blib/lib/EPUB/Parser/Util/EpubLoad.pm ↑ Summary blib/lib/EPUB/Parser/File/Container.pm »