File Coverage

blib/lib/EBook/EPUB/Lite/NCX.pm
Criterion Covered Total %
statement 53 53 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 4 8 50.0
total 68 72 94.4


line stmt bran cond sub pod time code
1             # Copyright (c) 2009, 2010 Oleksandr Tymoshenko
2             # All rights reserved.
3              
4             # Redistribution and use in source and binary forms, with or without
5             # modification, are permitted provided that the following conditions
6             # are met:
7             # 1. Redistributions of source code must retain the above copyright
8             # notice, this list of conditions and the following disclaimer.
9             # 2. Redistributions in binary form must reproduce the above copyright
10             # notice, this list of conditions and the following disclaimer in the
11             # documentation and/or other materials provided with the distribution.
12              
13             # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16             # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17             # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18             # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19             # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20             # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22             # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23             # SUCH DAMAGE.
24             package EBook::EPUB::Lite::NCX;
25 4     4   17 use Moo;
  4         7  
  4         22  
26 4     4   1125 use Types::Standard qw/ArrayRef Object Str/;
  4         14  
  4         37  
27 4     4   4948 use EBook::EPUB::Lite::NCX::NavPoint;
  4         80  
  4         2712  
28              
29             # Very simplified module for generation NCX
30              
31             has uid => ( isa => Str, is => 'rw' );
32             has title => ( isa => Str, is => 'rw' );
33              
34             has navpoints => (
35             is => 'ro',
36             isa => ArrayRef[Object],
37             default => sub { [] },
38             );
39              
40             sub all_navpoints {
41 2     2 1 4 return @{ shift->navpoints};
  2         13  
42             }
43              
44              
45              
46             has authors => (
47             is => 'ro',
48             isa => ArrayRef[Str],
49             default => sub { [] },
50             );
51              
52             sub all_authors {
53 2     2 0 4 return @{ shift->authors };
  2         16  
54             }
55              
56             sub add_author {
57 1     1 1 6 my ($self, @args) = @_;
58 1         2 push @{ shift->authors }, @args;
  1         11  
59             }
60              
61             sub to_xml
62             {
63 2     2 1 5 my ($self) = @_;
64 2         5 my $xml;
65 2         24 my $writer = XML::Writer->new(
66             OUTPUT => \$xml,
67             DATA_MODE => 1,
68             DATA_INDENT => 2,
69             );
70              
71 2         513 $writer->xmlDecl('utf-8');
72 2         151 $writer->doctype('ncx', '-//NISO//DTD ncx 2005-1//EN',
73             'http://www.daisy.org/z3986/2005/ncx-2005-1.dtd');
74            
75 2         71 $writer->startTag('ncx',
76             version => '2005-1',
77             xmlns => 'http://www.daisy.org/z3986/2005/ncx/',
78             );
79             # ..
80 2         244 $self->create_head_element($writer);
81             # Now document data
82 2         136 $self->create_doc_data($writer);
83 2         35 $self->create_navmap($writer);
84              
85 2         51 $writer->endTag('ncx');
86 2         54 $writer->end();
87              
88 2         291 return $xml;
89             }
90              
91             sub create_head_element
92             {
93 2     2 0 7 my ($self, $writer) = @_;
94 2         10 $writer->startTag('head');
95 2         168 $writer->emptyTag('meta',
96             name => 'dtb:uid',
97             content => $self->uid,
98             );
99              
100 2         213 $writer->emptyTag('meta',
101             name => 'dtb:depth',
102             content => '1',
103             );
104              
105 2         181 $writer->emptyTag('meta',
106             name => 'dtb:totalPageCount',
107             content => '0',
108             );
109              
110 2         223 $writer->emptyTag('meta',
111             name => 'dtb:maxPageNumber',
112             content => '0',
113             );
114              
115 2         183 $writer->endTag('head');
116             }
117              
118             sub create_doc_data
119             {
120 2     2 0 5 my ($self, $writer) = @_;
121              
122 2         12 $writer->startTag('docTitle');
123 2         116 $writer->dataElement('text', $self->title);
124 2         1092 $writer->endTag('docTitle');
125              
126 2         64 foreach my $author ($self->all_authors) {
127 1         5 $writer->startTag('docAuthor');
128 1         51 $writer->dataElement('text', $author);
129 1         89 $writer->endTag('docAuthor');
130             }
131             }
132              
133             sub create_navmap
134             {
135 2     2 0 5 my ($self, $writer) = @_;
136 2         8 $writer->startTag('navMap');
137 2         89 foreach my $point ($self->all_navpoints) {
138 1         8 $point->encode($writer);
139             }
140 2         38 $writer->endTag('navMap');
141             }
142              
143             sub add_navpoint
144             {
145 1     1 1 943 my ($self, @args) = @_;
146 1         14 my $point = EBook::EPUB::Lite::NCX::NavPoint->new(@args);
147 1         83 push @{$self->navpoints}, $point;
  1         10  
148 1         13 return $point;
149             }
150              
151             1;
152              
153             __END__