| 1 | package FB3; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use OPC; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::ShareDir qw/dist_dir/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | FB3 - API for manipulating FB3 files | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | use FB3; | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | # load FB3 from file | ||||||||||||||||||||||||||||||||
| 21 | my $FB3 = FB3->new( from_zip => 'path/to/file.fb3'); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | # or load FB3 from directory where it had been unpacked | ||||||||||||||||||||||||||||||||
| 24 | my $FB3 = FB3->new( from_dir => 'path/to/unpacked_fb3_dir'); | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | # navigate through FB3 and read XML content of it's main parts | ||||||||||||||||||||||||||||||||
| 27 | $Meta = $FB3->Meta; | ||||||||||||||||||||||||||||||||
| 28 | $MetaXML = $Meta->Content; | ||||||||||||||||||||||||||||||||
| 29 | $BodyXML = $FB3->Body->Content; | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # get path to cover | ||||||||||||||||||||||||||||||||
| 32 | $PathToCover = $FB3->Cover->PhysicalName; | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | Litres.ru Team | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | use constant { | ||||||||||||||||||||||||||||||||
| 41 | 1 | RELATION_TYPE_CORE_PROP => | |||||||||||||||||||||||||||||||
| 42 | 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties' | ||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | # Returns path to FB3 schemas common directory | ||||||||||||||||||||||||||||||||
| 46 | sub SchemasDirPath { | ||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return dist_dir('FB3'); | ||||||||||||||||||||||||||||||
|
Subroutine
SchemasDirPath: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # Returns path to requested FB3 scheme | ||||||||||||||||||||||||||||||||
| 51 | sub SchemaPath { | ||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $SchemaName = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
SchemaPath: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 53 | 0 | my $SchemaPath = FB3::SchemasDirPath().'/'.$SchemaName; | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | die "$SchemaName doesn't exist" unless -e $SchemaPath; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | return $SchemaPath; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | sub new { | ||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my( $Class, %Params ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 0 | my $OPCPackage; | |||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if( exists $Params{from_zip} && defined $Params{from_zip} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
exists $Params{"from_zip"} and defined $Params{"from_zip"}
MC/DC0%0/2
exists $Params{"from_zip"} and defined $Params{"from_zip"}
exists $Params{"from_zip"}
defined $Params{"from_zip"}
Truth table0%0/3
A and B
A= exists $Params{"from_zip"}B= defined $Params{"from_zip"}
| |||||||||||||||||||||||||||||||||
| 63 | 0 | $OPCPackage = OPC->FromZip( $Params{from_zip} ); | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | ▶ | } elsif( exists $Params{from_dir} && defined $Params{from_dir} ) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
exists $Params{"from_dir"} and defined $Params{"from_dir"}
MC/DC0%0/2
exists $Params{"from_dir"} and defined $Params{"from_dir"}
exists $Params{"from_dir"}
defined $Params{"from_dir"}
Truth table0%0/3
A and B
A= exists $Params{"from_dir"}B= defined $Params{"from_dir"}
| |||||||||||||||||||||||||||||||||
| 66 | 0 | $OPCPackage = OPC->FromDir( $Params{from_dir} ); | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||||||
| 69 | 0 | Carp::confess 'Must pass from_zip or from_dir argument'; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 0 | my $self = { | |||||||||||||||||||||||||||||||
| 73 | opc => $OPCPackage, | ||||||||||||||||||||||||||||||||
| 74 | root => $OPCPackage->Root, | ||||||||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | return bless $self, $Class; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | sub Cover { | ||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
Cover: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 82 | 0 | return $self->{root}->Thumbnail; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | sub Meta { | ||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
Meta: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 87 | 0 | return $self->{root}->RelatedPart( type => 'http://www.fictionbook.org/FictionBook3/relationships/Book' ); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | sub Body { | ||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
Body: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 92 | 0 | return $self->Meta->RelatedPart( type => 'http://www.fictionbook.org/FictionBook3/relationships/body' ); | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | sub Root { | ||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return shift->{root}; | ||||||||||||||||||||||||||||||
|
Subroutine
Root: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | sub Part { | ||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my( $self, $PartName ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
Part: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 101 | 0 | return $self->{opc}->Part( name => $PartName ) | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | sub HasPart { | ||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my( $self, $PartName ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
HasPart: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 106 | 0 | return $self->{opc}->HasPart( $PartName ); | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | sub SetPartContents { | ||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my( $self, $PartName, $PartContents ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
SetPartContents: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 0 | $self->{opc}->SetContents( $PartName, $PartContents ); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | sub DirPath { | ||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
DirPath: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($self->{opc}->{_is_zip}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 0 | die "FB3 is ZIP archive, not directory"; | |||||||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||||||
| 120 | 0 | return $self->{opc}->{_physical}; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | Copyright (C) 2018 Litres.ru | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | The GNU Lesser General Public License version 3.0 | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | FB3 is free software: you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 131 | under the terms of the GNU Lesser General Public License as published by | ||||||||||||||||||||||||||||||||
| 132 | the Free Software Foundation, either version 3.0 of the License. | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | FB3 is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||||||
| 135 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||||||
| 136 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||||||||||||||||||||||||||||||||
| 137 | License for more details. | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | Full text of License L<http://www.gnu.org/licenses/lgpl-3.0.en.html>. | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 1 | 1; | |||||||||||||||||||||||||||||||