blib/lib/EBook/Ishmael/EBook/Text.pm

filter: Statementstmt 84.6% 66 / 78 Branchbran 41.6% 5 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 9 totaltotal 75.0% 90 / 120 CC 8 SCAR 11.3
CC
8
Cov
78%
raw
7.8
html
4.9
BEGIN
1.0
CRAP 3.1
« blib/lib/EBook/Ishmael/EBook/KF8.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/Metadata.pm »
1 package EBook::Ishmael::EBook::Text;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.03';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 ▶use EBook::Ishmael::CharDet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use EBook::Ishmael::TextToHtml;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Use -T to check if file is a text file. EBook.pm's ebook_id() makes sure
16 # to use the Text heuristic last, so that it doesn't incorrectly identify other
17 # plain text ebook formats as just text.
18 sub heuristic {
19  
20 12 ▶ my $class = shift;
Subroutine heuristic: called CC=1 SCAR=0.0
Pod undocumented
21 12 my $file = shift;
22  
23 12 return -T $file;
24  
25 }
26  
27 sub new {
28  
29 11 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 11 my $file = shift;
31 11 my $enc = shift;
32  
33 11 my $self = {
34 Source => undef,
35 Metadata => EBook::Ishmael::EBook::Metadata->new,
36 Encode => $enc,
37 };
38  
39 11 bless $self, $class;
40  
41 11 $self->{Source} = File::Spec->rel2abs($file);
42  
43 11 $self->{Metadata}->set_title(basename($self->{Source}));
44 11 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
45 11 $self->{Metadata}->set_format('Text');
46  
47 11 return $self;
48  
49 }
50  
51 sub html {
52  
53 3 ▶ my $self = shift;
Subroutine html: called CC=3 SCAR=16.0
Pod undocumented
54 3 my $out = shift;
55  
56 3 my $html = text2html($self->raw);
57  
58 3 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 3 3
59 0 ▶ open my $wh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $wh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
60 or die "Failed to open $out for writing: $!\n";
61 0 binmode $wh, ':utf8';
62 0 print { $wh } $html;
63 0 close $wh;
64 0 return $out;
65 } else {
66 3 return $html;
67 }
68  
69 }
70  
71 sub raw {
72  
73 6 ▶ my $self = shift;
Subroutine raw: called CC=6 SCAR=20.5
Pod undocumented
74 6 my $out = shift;
75  
76 open my $rh, '<', $self->{Source}
77 6 ▶ or die "Failed to open $self->{Source} for reading: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $rh, "<", $self->{"Source"} or die qq[Failed to open $self->{"Source"} for reading: $!\n] 0 6 6
78 6 binmode $rh;
79 6 my $raw = do { local $/; <$rh> };
80 6 close $rh;
81  
82 6 ▶ if (not defined $self->{Encode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Encode"}) 5 1 6
83 5 ▶ $self->{Encode} = chardet($raw) // 'ASCII';
Condition50%1/2
chardet($raw) // "ASCII"
l!l
50
MC/DC0%0/1
chardet($raw) // "ASCII"
chardet($raw)
Truth table50%1/2
A
A= chardet($raw)
Aresult
11
00
84 }
85 6 say $self->{Encode};
86 6 $raw = decode($self->{Encode}, $raw);
87  
88 6 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 6 6
89 0 ▶ open my $wh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $wh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
90 or die "Failed to open $out for writing: $!\n";
91 0 binmode $wh, ':utf8';
92 0 print { $wh } $raw;
93 0 close $wh;
94 0 return $out;
95 } else {
96 6 return $raw;
97 }
98  
99 }
100  
101 sub metadata {
102  
103 6 ▶ my $self = shift;
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
104  
105 6 return $self->{Metadata};
106  
107 }
108  
109 2 ▶sub has_cover { 0 }
Subroutine has_cover: called CC=1 SCAR=0.0
Pod undocumented
110  
111 1 ▶sub cover { (undef, undef) }
Subroutine cover: called CC=1 SCAR=0.0
Pod undocumented
112  
113 2 ▶sub image_num { 0 }
Subroutine image_num: called CC=1 SCAR=0.0
Pod undocumented
114  
115 1 ▶sub image { (undef, undef) }
Subroutine image: called CC=1 SCAR=0.0
Pod undocumented
116  
117 17 1;
« blib/lib/EBook/Ishmael/EBook/KF8.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/Metadata.pm »