File Coverage

blib/lib/EPUB/Parser/File/OPF.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package EPUB::Parser::File::OPF;
2 14     14   128 use strict;
  14         29  
  14         589  
3 14     14   108 use warnings;
  14         29  
  14         339  
4 14     14   66 use Carp;
  14         27  
  14         866  
5 14     14   9247 use EPUB::Parser::File::Parser::OPF;
  0            
  0            
6             use EPUB::Parser::File::OPF::Context;
7             use EPUB::Parser::File::Container;
8             use Smart::Args;
9              
10             sub new {
11             args(
12             my $class => 'ClassName',
13             my $zip => { isa => 'EPUB::Parser::Util::Archive' },
14             my $epub_version,
15             );
16              
17             my $self = bless {
18             zip => $zip,
19             epub_version => $epub_version,
20             } => $class;
21              
22             return $self;
23             }
24              
25             sub parser {
26             my $self = shift;
27              
28             $self->{parser}
29             ||= EPUB::Parser::File::Parser::OPF->new({ data => $self->data });
30             }
31              
32             sub path {
33             my $self = shift;
34              
35             $self->{path} ||= do {
36             my $container = EPUB::Parser::File::Container->new({ zip => $self->{zip} });
37             $container->opf_path;
38             };
39             }
40              
41             sub dir {
42             my $self = shift;
43             require File::Basename;
44             $self->{dir} ||= File::Basename::dirname($self->path);
45             }
46              
47              
48             sub data {
49             my $self = shift;
50             $self->{data} ||= $self->{zip}->get_member_data({ file_path => $self->path });
51             }
52              
53             sub context {
54             my $self = shift;
55             my $context_name = shift;
56             return $self->{$context_name} if $self->{$context_name};
57              
58             $self->{$context_name} = EPUB::Parser::File::OPF::Context->new({
59             opf => $self,
60             parser => $self->parser,
61             context_name => $context_name,
62             });
63             }
64              
65             sub spine { shift->context('spine' ) }
66             sub manifest { shift->context('manifest') }
67             sub metadata { shift->context('metadata') }
68             sub guide { shift->context('guide' ) }
69              
70             sub nav_path {
71             my $self = shift;
72             $self->{nav_path} ||= sprintf("%s/%s", $self->dir, $self->manifest->nav_path);
73             }
74              
75             sub cover_image_path {
76             shift->manifest->cover_image_path(@_);
77             }
78              
79             sub guess_version {
80             my $self = shift;
81             my $version = $self->parser->single('/pkg:package/@version')->string_value;
82            
83             if ($version) {
84             return $version;
85             }
86             elsif ( $self->nav_path ) {
87             return '3.0';
88             }
89             else {
90             return;
91             }
92             }
93              
94              
95             1;
96              
97              
98             __END__
99              
100             =encoding utf-8
101              
102             =head1 NAME
103              
104             EPUB::Parser::File::OPF - parses opf file
105              
106             =head1 SYNOPSIS
107              
108             use EPUB::Parser;
109             my $ep = EPUB::Parser->new->load_file({ file_path => 'sample.epub' });
110             my $opf = $ep->opf;
111              
112             =head1 METHODS
113              
114             =head2 new(\%opts)
115              
116             Constructor.
117             This method called from L<EPUB::Parser> object.
118             $epub_parser->opf;
119              
120             =head2 parser
121              
122             Returns instance of L<EPUB::Parser::File::Parser::OPF>.
123              
124             =head2 path
125              
126             get opf file path from 'META-INF/container.xml'
127              
128             =head2 dir
129              
130             get directory path of opf file.
131             File::Basename::dirname($self->path);
132              
133             =head2 data
134              
135             get blob of opf file from loaded EPUB
136              
137             =head2 spine
138              
139             Returns instance of L<EPUB::Parser::File::OPF::Context::Spine>.
140              
141             =head2 manifest
142              
143             Returns instance of L<EPUB::Parser::File::OPF::Context::Manifest>.
144              
145             =head2 metadata
146              
147             Returns instance of L<EPUB::Parser::File::OPF::Context::Metadata>.
148              
149             =head2 guide
150              
151             Returns instance of L<EPUB::Parser::File::OPF::Context::Guide>.
152              
153             =head2 nav_path
154              
155             Returns navigation file path from manifest.
156              
157             =head2 cover_image_path(\%opt)
158              
159             Shortcut method.
160             see L<EPUB::Parser::File::OPF::Context::Manifest>.
161              
162             =head2 guess_version
163              
164             get opf version.
165             return '3.0' if version is not found and navigation file exists.
166              
167             =head1 LICENSE
168              
169             Copyright (C) tokubass.
170              
171             This library is free software; you can redistribute it and/or modify
172             it under the same terms as Perl itself.
173              
174             =head1 AUTHOR
175              
176             tokubass E<lt>tokubass {at} cpan.orgE<gt>
177              
178             =cut
179