File Coverage

blib/lib/EBook/EPUB/NCX.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2009, 2010 Oleksandr Tymoshenko <gonzo@bluezbox.com>
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::NCX;
25 1     1   1930 use Moose;
  0            
  0            
26             use EBook::EPUB::NCX::NavPoint;
27              
28             # Very simplified module for generation NCX
29              
30             has [qw/uid title/] => ( isa => 'Str', is => 'rw' );
31              
32             has navpoints => (
33             traits => ['Array'],
34             is => 'ro',
35             isa => 'ArrayRef[Object]',
36             default => sub { [] },
37             handles => {
38             all_navpoints => 'elements',
39             },
40             );
41              
42             has authors => (
43             traits => ['Array'],
44             is => 'ro',
45             isa => 'ArrayRef[Str]',
46             default => sub { [] },
47             handles => {
48             all_authors => 'elements',
49             add_author => 'push',
50             },
51             );
52              
53             sub to_xml
54             {
55             my ($self) = @_;
56             my $xml;
57             my $writer = new XML::Writer(
58             OUTPUT => \$xml,
59             DATA_MODE => 1,
60             DATA_INDENT => 2,
61             );
62              
63             $writer->xmlDecl('utf-8');
64             $writer->doctype('ncx', '-//NISO//DTD ncx 2005-1//EN',
65             'http://www.daisy.org/z3986/2005/ncx-2005-1.dtd');
66            
67             $writer->startTag('ncx',
68             version => '2005-1',
69             xmlns => 'http://www.daisy.org/z3986/2005/ncx/',
70             );
71             # <head>..</head>
72             $self->create_head_element($writer);
73             # Now document data
74             $self->create_doc_data($writer);
75             $self->create_navmap($writer);
76              
77             $writer->endTag('ncx');
78             $writer->end();
79              
80             return $xml;
81             }
82              
83             sub create_head_element
84             {
85             my ($self, $writer) = @_;
86             $writer->startTag('head');
87             $writer->emptyTag('meta',
88             name => 'dtb:uid',
89             content => $self->uid,
90             );
91              
92             $writer->emptyTag('meta',
93             name => 'dtb:depth',
94             content => '1',
95             );
96              
97             $writer->emptyTag('meta',
98             name => 'dtb:totalPageCount',
99             content => '0',
100             );
101              
102             $writer->emptyTag('meta',
103             name => 'dtb:maxPageNumber',
104             content => '0',
105             );
106              
107             $writer->endTag('head');
108             }
109              
110             sub create_doc_data
111             {
112             my ($self, $writer) = @_;
113              
114             $writer->startTag('docTitle');
115             $writer->dataElement('text', $self->title);
116             $writer->endTag('docTitle');
117              
118             foreach my $author ($self->all_authors) {
119             $writer->startTag('docAuthor');
120             $writer->dataElement('text', $author);
121             $writer->endTag('docAuthor');
122             }
123             }
124              
125             sub create_navmap
126             {
127             my ($self, $writer) = @_;
128             $writer->startTag('navMap');
129             foreach my $point ($self->all_navpoints) {
130             $point->encode($writer);
131             }
132             $writer->endTag('navMap');
133             }
134              
135             sub add_navpoint
136             {
137             my ($self, @args) = @_;
138             my $point = EBook::EPUB::NCX::NavPoint->new(@args);
139             push @{$self->navpoints}, $point;
140             return $point;
141             }
142              
143             no Moose;
144             __PACKAGE__->meta->make_immutable;
145              
146             1;
147              
148             __END__
149              
150             =head1 NAME
151              
152             EBook::EPUB::NCX
153              
154             =head1 SYNOPSIS
155              
156             Class that "Navigation Center eXtended" file of OPF document
157              
158             =head1 DESCRIPTION
159              
160             The Navigation Control file for XML applications (NCX) exposes the hierarchical
161             structure of a Publication to allow the user to navigate through it. The NCX is
162             similar to a table of contents in that it enables the reader to jump directly
163             to any of the major structural elements of the document, i.e. part, chapter, or
164             section, but it will often contain more elements of the document than the
165             publisher chooses to include in the original print table of contents. It can be
166             visualized as a collapsible tree familiar to PC users.
167              
168             =head1 SUBROUTINES/METHODS
169              
170             =over 4
171              
172             =item add_navpoint(%opts)
173              
174             Add refrence to an OPS Content Document that is a part of publication. %opts is
175             an anonymous hash, for possible key values see L<EBook::EPUB::NCX::NavPoint>.
176             Method returns created EBook::EPUB::NCX::NavPoint object that could be used
177             later for adding subsections.
178              
179             =item all_navpoints()
180              
181             Returns array of EBook::EPUB::NCX::NavPoint objects, current content of NCX
182              
183             =item add_author([$author)
184              
185             Add $author to authors list
186              
187             =item new(%opts)
188              
189             Create new object. %opts is anonymous hash with possible key values:
190             author
191             title
192             uid
193              
194             =item title([$title)
195              
196             Get/set book title
197              
198             =item to_xml()
199              
200             Returns XML representation of NCX
201              
202             =item uid([$uid)
203              
204             Get/set unique identfier for book
205              
206             =back
207              
208             =head1 AUTHOR
209              
210             Oleksandr Tymoshenko, E<lt>gonzo@bluezbox.comE<gt>
211              
212             =head1 BUGS
213              
214             Please report any bugs or feature requests to E<lt>gonzo@bluezbox.comE<gt>
215              
216             =head1 LICENSE AND COPYRIGHT
217              
218             Copyright 2009, 2010 Oleksandr Tymoshenko.
219              
220             L<http://bluezbox.com>
221              
222             This module is free software; you can redistribute it and/or
223             modify it under the terms of the BSD license. See the F<LICENSE> file
224             included with this distribution.