| 1 | package EBook::Ishmael::EBook::Text; | ||||||||||||
| 2 | 17 | ▶ | use 5.016; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 17 | our $VERSION = '2.04'; | |||||||||||
| 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::HTML qw(text2html); | ||||||||||
|
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
| |||||||||||||
| 59 | 0 | ▶ | open my $wh, '>', $out | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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.8
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
| |||||||||||||
| 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
| |||||||||||||
| 83 | 5 | ▶ | $self->{Encode} = chardet($raw) // 'ASCII'; | ||||||||||
|
Condition50%1/2
chardet($raw) // "ASCII"
MC/DC0%0/1
chardet($raw) // "ASCII"
chardet($raw)
Truth table50%1/2
A
A= chardet($raw)
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | 6 | $raw = decode($self->{Encode}, $raw); | |||||||||||
| 86 | |||||||||||||
| 87 | 6 | ▶ | if (defined $out) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | ▶ | open my $wh, '>', $out | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | or die "Failed to open $out for writing: $!\n"; | ||||||||||||
| 90 | 0 | binmode $wh, ':utf8'; | |||||||||||
| 91 | 0 | print { $wh } $raw; | |||||||||||
| 92 | 0 | close $wh; | |||||||||||
| 93 | 0 | return $out; | |||||||||||
| 94 | } else { | ||||||||||||
| 95 | 6 | return $raw; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | sub metadata { | ||||||||||||
| 101 | |||||||||||||
| 102 | 6 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
metadata: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 103 | |||||||||||||
| 104 | 6 | return $self->{Metadata}; | |||||||||||
| 105 | |||||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 2 | ▶ | sub has_cover { 0 } | ||||||||||
|
Subroutine
has_cover: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 109 | |||||||||||||
| 110 | 1 | ▶ | sub cover { (undef, undef) } | ||||||||||
|
Subroutine
cover: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 111 | |||||||||||||
| 112 | 2 | ▶ | sub image_num { 0 } | ||||||||||
|
Subroutine
image_num: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 113 | |||||||||||||
| 114 | 1 | ▶ | sub image { (undef, undef) } | ||||||||||
|
Subroutine
image: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 115 | |||||||||||||
| 116 | 17 | 1; | |||||||||||