| 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 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 0 | ▶ | use XML::DOM; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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__ | ||