File Coverage

blib/lib/EBook/FB2/Body/Section.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              
25             package EBook::FB2::Body::Section;
26 1     1   1885 use Moose;
  0            
  0            
27             use XML::DOM;
28              
29             has id => ( isa => 'Str', is => 'rw' );
30             has title => ( isa => 'Ref', is => 'rw' );
31             has image => ( isa => 'Ref', is => 'rw' );
32             has data => ( isa => 'Ref', is => 'rw' );
33             has _epigraphs => (
34             isa => 'ArrayRef',
35             is => 'rw',
36             traits => ['Array'],
37             default => sub { [] },
38             handles => {
39             epigraphs => 'elements',
40             add_epigraph => 'push',
41             },
42             );
43              
44             has _subsections => (
45             isa => 'ArrayRef',
46             is => 'ro',
47             traits => ['Array'],
48             default => sub { [] },
49             handles => {
50             subsections => 'elements',
51             add_subsection => 'push',
52             },
53             );
54              
55             sub load
56             {
57             my ($self, $node) = @_;
58              
59             my $anode = $node->getAttribute("id");
60             if (defined($anode)) {
61             $self->id($anode);
62             }
63              
64             my @nodes = $node->findnodes("title");
65             if (@nodes) {
66             $self->title($nodes[0]);
67             # separate title and main body
68             foreach my $kid (@nodes) {
69             $node->removeChild($kid);
70             }
71             }
72              
73             @nodes = $node->findnodes("epigraph");
74             if (@nodes) {
75             # separate epigraphs and main body
76             foreach my $kid (@nodes) {
77             $self->add_epigraph($kid);
78             $node->removeChild($kid);
79             }
80             }
81              
82             @nodes = $node->findnodes("section");
83             if (@nodes) {
84             foreach my $n (@nodes) {
85             my $s = EBook::FB2::Body::Section->new();
86             $s->load($n);
87             $self->add_subsection($s);
88             }
89             }
90             # store raw XML::DOM::Node for collecting ids.
91             $self->data($node);
92             }
93              
94             #
95             # Extract plain text without formatting from XML::DOM node
96             #
97             sub extract_text
98             {
99             my $node = shift;
100             my $text;
101              
102             return $node->getData if ($node->getNodeType() == TEXT_NODE);
103              
104             # get text from children
105             foreach my $kid ($node->getChildNodes) {
106             $text .= extract_text($kid);
107             }
108              
109             return $text;
110             }
111              
112             sub plaintext_title
113             {
114             my $self = shift;
115             my $title = $self->title;
116             my $section_title;
117             if (defined($title)) {
118             $section_title = extract_text($title);
119             $section_title =~ s/\r?\n/ /g;
120             # Cleanup extra-spaces
121             while ($section_title =~ s/\s{2}/ /g) {
122             }
123             $section_title =~ s/^\s+//g;
124             $section_title =~ s/\s+$//g;
125             }
126              
127             return $section_title;
128             }
129              
130             1;
131              
132             __END__
133             =head1 NAME
134              
135             EBook::FB2::Body::Section
136              
137             =head1 SYNOPSIS
138              
139             EBook::FB2::Body::Section - section of body, logical part of document
140              
141             =head1 SUBROUTINES/METHODS
142              
143             =over 4
144              
145             =item data()
146              
147             Returns XML::DOM::Node, parsed content of section
148              
149             =item epigraphs()
150              
151             Returns array of references to XML::DOM::Node objects, parsed epigraphs
152             of body element
153              
154             =item id()
155              
156             Returns id of image associated with section
157              
158             =item image()
159              
160             Return id of image
161              
162             =item plaintext_title()
163              
164             Returns title of section converted to plain text
165              
166             =item subsections()
167              
168             Returns subsections of current section
169              
170             =item title()
171              
172             Returns section title. May contain markup tags
173              
174             =back
175              
176             =head1 AUTHOR
177              
178             Oleksandr Tymoshenko, E<lt>gonzo@bluezbox.comE<gt>
179              
180             =head1 BUGS
181              
182             Please report any bugs or feature requests to E<lt>gonzo@bluezbox.comE<gt>
183              
184             =head1 LICENSE AND COPYRIGHT
185              
186             Copyright 2009, 2010 Oleksandr Tymoshenko.
187              
188             L<http://bluezbox.com>
189              
190             This module is free software; you can redistribute it and/or
191             modify it under the terms of the BSD license. See the F<LICENSE> file
192             included with this distribution.