| 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::Lite::Metadata; | ||||||||||
| 25 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 4 | ▶ | use Types::Standard qw/ArrayRef Int Ref Object/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 4 | ▶ | use EBook::EPUB::Lite::Metadata::DCItem; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 4 | ▶ | use EBook::EPUB::Lite::Metadata::Item; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | has items => ( | ||||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | isa => ArrayRef[Object], | ||||||||||
| 34 | 2 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 4 | ); | |||||||||
| 36 | |||||||||||
| 37 | has id_counter => ( | ||||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | isa => Int, | ||||||||||
| 40 | 2 | ▶ | default => sub { 0 }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 4 | ); | |||||||||
| 42 | |||||||||||
| 43 | 4 | has _book_id_item => ( | |||||||||
| 44 | is => 'rw', | ||||||||||
| 45 | isa => Ref, | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | sub encode | ||||||||||
| 49 | 2 | ▶ | { | ||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 50 | 2 | my ($self, $writer) = @_; | |||||||||
| 51 | 2 | $writer->startTag("metadata", | |||||||||
| 52 | 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', | ||||||||||
| 53 | 'xmlns:opf' => 'http://www.idpf.org/2007/opf', | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | 2 | foreach my $item (@{$self->items()}) { | |||||||||
| 57 | 6 | $item->encode($writer); | |||||||||
| 58 | } | ||||||||||
| 59 | 2 | my $id_item = $self->_book_id_item; | |||||||||
| 60 | 2 | ▶ | croak('No BookId specified by set_book_id') if (!defined($id_item)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 2 | $id_item->encode($writer); | |||||||||
| 62 | 2 | $writer->endTag("metadata"); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | sub add_title | ||||||||||
| 66 | 1 | ▶ | { | ||||||||
|
Subroutine
add_title: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 1 | my ($self, $title) = @_; | |||||||||
| 68 | 1 | $self->add_meta_dcitem('title', $title); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | sub add_contributor | ||||||||||
| 72 | 0 | ▶ | { | ||||||||
|
Subroutine
add_contributor: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 73 | 0 | my ($self, $name, %opts) = @_; | |||||||||
| 74 | 0 | my @args = (); | |||||||||
| 75 | 0 | my $formal = $opts{'fileas'}; | |||||||||
| 76 | 0 | my $role = $opts{'role'}; | |||||||||
| 77 | 0 | ▶ | if (defined($formal)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | push @args, 'opf:file-as', $formal; | |||||||||
| 79 | } | ||||||||||
| 80 | 0 | ▶ | if (defined($role)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | push @args, 'opf:role', $role; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | $self->add_meta_dcitem('contributor', $name, @args); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub add_creator | ||||||||||
| 88 | 1 | ▶ | { | ||||||||
|
Subroutine
add_creator: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 89 | 1 | my ($self, $name, %opts) = @_; | |||||||||
| 90 | 1 | my @args = (); | |||||||||
| 91 | 1 | my $formal = $opts{'fileas'}; | |||||||||
| 92 | 1 | my $role = $opts{'role'}; | |||||||||
| 93 | 1 | ▶ | if (defined($formal)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | push @args, 'opf:file-as', $formal; | |||||||||
| 95 | } | ||||||||||
| 96 | 1 | ▶ | if (defined($role)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 1 | push @args, 'opf:role', $role; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 1 | $self->add_meta_dcitem('creator', $name, @args); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | sub add_author | ||||||||||
| 104 | 1 | ▶ | { | ||||||||
|
Subroutine
add_author: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 105 | 1 | my ($self, $author, $formal) = @_; | |||||||||
| 106 | 1 | $self->add_creator($author, fileas => $formal, role => 'aut'); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | sub add_translator | ||||||||||
| 110 | 0 | ▶ | { | ||||||||
|
Subroutine
add_translator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 111 | 0 | my ($self, $name, $formal) = @_; | |||||||||
| 112 | 0 | $self->add_creator($name, fileas => $formal, role => 'trl'); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | sub add_subject | ||||||||||
| 116 | 0 | ▶ | { | ||||||||
|
Subroutine
add_subject: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 117 | 0 | my ($self, $subject) = @_; | |||||||||
| 118 | 0 | $self->add_meta_dcitem('subject', $subject); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | sub add_description | ||||||||||
| 122 | 1 | ▶ | { | ||||||||
|
Subroutine
add_description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 123 | 1 | my ($self, $description) = @_; | |||||||||
| 124 | 1 | $self->add_meta_dcitem('description', $description); | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | sub add_date | ||||||||||
| 128 | 1 | ▶ | { | ||||||||
|
Subroutine
add_date: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 129 | # Todo: handle date format here | ||||||||||
| 130 | # http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html#Section2.2.7 | ||||||||||
| 131 | 1 | my ($self, $date, $event) = @_; | |||||||||
| 132 | 1 | my @attr; | |||||||||
| 133 | 1 | ▶ | if (defined($event)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | push @attr, "opf:event", $event; | |||||||||
| 135 | } | ||||||||||
| 136 | 1 | $self->add_meta_dcitem('date', $date, @attr); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | sub add_type | ||||||||||
| 140 | 0 | ▶ | { | ||||||||
|
Subroutine
add_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 141 | 0 | my ($self, $type) = @_; | |||||||||
| 142 | 0 | $self->add_meta_dcitem('type', $type); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | sub add_format | ||||||||||
| 146 | 0 | ▶ | { | ||||||||
|
Subroutine
add_format: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 147 | 0 | my ($self, $format) = @_; | |||||||||
| 148 | 0 | $self->add_meta_dcitem('format', $format); | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | sub add_publisher | ||||||||||
| 152 | 0 | ▶ | { | ||||||||
|
Subroutine
add_publisher: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 153 | 0 | my ($self, $publisher) = @_; | |||||||||
| 154 | 0 | $self->add_meta_dcitem('publisher', $publisher); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | sub add_coverage | ||||||||||
| 158 | 0 | ▶ | { | ||||||||
|
Subroutine
add_coverage: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 159 | 0 | my ($self, $coverage) = @_; | |||||||||
| 160 | 0 | $self->add_meta_dcitem('coverage', $coverage); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | sub set_book_id | ||||||||||
| 164 | 2 | ▶ | { | ||||||||
|
Subroutine
set_book_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 165 | 2 | my ($self, $book_id) = @_; | |||||||||
| 166 | 2 | my @attr = ('id', 'BookId'); | |||||||||
| 167 | 2 | my $dcitem = EBook::EPUB::Lite::Metadata::DCItem->new( | |||||||||
| 168 | name => "dc:identifier", | ||||||||||
| 169 | value => $book_id, | ||||||||||
| 170 | attributes => \@attr); | ||||||||||
| 171 | |||||||||||
| 172 | 2 | $self->_book_id_item($dcitem); | |||||||||
| 173 | |||||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | sub add_identifier | ||||||||||
| 177 | 0 | ▶ | { | ||||||||
|
Subroutine
add_identifier: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 178 | 0 | my ($self, $ident, $scheme) = @_; | |||||||||
| 179 | 0 | my $id = 'BookId'; | |||||||||
| 180 | # if it's urn:uuid - it should be BookId, otherwise include counter | ||||||||||
| 181 | 0 | ▶ | if ($ident !~ /^urn:uuid/i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | 0 | $id .= $self->id_counter; | |||||||||
| 183 | 0 | $self->id_counter($self->id_counter + 1); | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | 0 | my @attr = ('id', $id); | |||||||||
| 187 | 0 | ▶ | if (defined($scheme)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 0 | push @attr, "opf:scheme", $scheme; | |||||||||
| 189 | } | ||||||||||
| 190 | 0 | $self->add_meta_dcitem('identifier', $ident, @attr); | |||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | sub add_source | ||||||||||
| 194 | 1 | ▶ | { | ||||||||
|
Subroutine
add_source: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 195 | 1 | my ($self, $source) = @_; | |||||||||
| 196 | 1 | $self->add_meta_dcitem('source', $source); | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | sub add_language | ||||||||||
| 200 | 1 | ▶ | { | ||||||||
|
Subroutine
add_language: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 201 | # TODO: filter language? | ||||||||||
| 202 | 1 | my ($self, $lang) = @_; | |||||||||
| 203 | 1 | $self->add_meta_dcitem('language', $lang); | |||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | sub add_relation | ||||||||||
| 207 | 0 | ▶ | { | ||||||||
|
Subroutine
add_relation: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 208 | 0 | my ($self, $relation) = @_; | |||||||||
| 209 | 0 | $self->add_meta_dcitem('relation', $relation); | |||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | sub add_rights | ||||||||||
| 213 | 0 | ▶ | { | ||||||||
|
Subroutine
add_rights: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 214 | 0 | my ($self, $rights) = @_; | |||||||||
| 215 | 0 | $self->add_meta_dcitem('rights', $rights); | |||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | sub add_meta_dcitem | ||||||||||
| 219 | 6 | ▶ | { | ||||||||
|
Subroutine
add_meta_dcitem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 220 | 6 | my ($self, $name, $value, @attributes) = @_; | |||||||||
| 221 | 6 | my $dcitem = EBook::EPUB::Lite::Metadata::DCItem->new( | |||||||||
| 222 | name => "dc:$name", | ||||||||||
| 223 | value => $value, | ||||||||||
| 224 | attributes => \@attributes); | ||||||||||
| 225 | 6 | push @{$self->items()}, $dcitem; | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | sub add_meta_item | ||||||||||
| 229 | 0 | ▶ | { | ||||||||
|
Subroutine
add_meta_item: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 230 | 0 | my ($self, $name, $value) = @_; | |||||||||
| 231 | 0 | my $item = EBook::EPUB::Lite::Metadata::Item->new( | |||||||||
| 232 | name => $name, | ||||||||||
| 233 | value => $value, | ||||||||||
| 234 | ); | ||||||||||
| 235 | 0 | push @{$self->items()}, $item; | |||||||||
| 236 | } | ||||||||||
| 237 | |||||||||||
| 238 | 4 | 1; | |||||||||
| 239 | |||||||||||
| 240 | __END__ | ||||||||||