File Coverage

blib/lib/EBook/Ishmael/EBook/Text.pm
Criterion Covered Total %
statement 63 75 84.0
branch 5 12 41.6
condition 1 2 50.0
subroutine 18 18 100.0
pod 0 9 0.0
total 87 116 75.0


line stmt bran cond sub pod time code
1             package EBook::Ishmael::EBook::Text;
2 17     17   260 use 5.016;
  17         48  
3             our $VERSION = '2.05';
4 17     17   100 use strict;
  17         28  
  17         298  
5 17     17   51 use warnings;
  17         21  
  17         804  
6              
7 17     17   114 use Encode qw(decode);
  17         28  
  17         811  
8 17     17   74 use File::Basename;
  17         21  
  17         895  
9 17     17   63 use File::Spec;
  17         28  
  17         299  
10              
11 17     17   51 use EBook::Ishmael::CharDet;
  17         24  
  17         549  
12 17     17   53 use EBook::Ishmael::EBook::Metadata;
  17         21  
  17         360  
13 17     17   76 use EBook::Ishmael::HTML qw(text2html);
  17         21  
  17         8335  
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     12 0 37 my $class = shift;
21 12         17 my $file = shift;
22              
23 12         499 return -T $file;
24              
25             }
26              
27             sub new {
28              
29 11     11 0 16 my $class = shift;
30 11         19 my $file = shift;
31 11         22 my $enc = shift;
32              
33 11         83 my $self = {
34             Source => undef,
35             Metadata => EBook::Ishmael::EBook::Metadata->new,
36             Encode => $enc,
37             };
38              
39 11         17 bless $self, $class;
40              
41 11         336 $self->{Source} = File::Spec->rel2abs($file);
42              
43 11         515 $self->{Metadata}->set_title(basename($self->{Source}));
44 11         224 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
45 11         36 $self->{Metadata}->set_format('Text');
46              
47 11         24 return $self;
48              
49             }
50              
51             sub html {
52              
53 3     3 0 8 my $self = shift;
54 3         8 my $out = shift;
55              
56 3         12 my $html = text2html($self->raw);
57              
58 3 50       14 if (defined $out) {
59 0 0       0 open my $wh, '>', $out
60             or die "Failed to open $out for writing: $!\n";
61 0         0 binmode $wh, ':utf8';
62 0         0 print { $wh } $html;
  0         0  
63 0         0 close $wh;
64 0         0 return $out;
65             } else {
66 3         29 return $html;
67             }
68              
69             }
70              
71             sub raw {
72              
73 6     6 0 11 my $self = shift;
74 6         11 my $out = shift;
75              
76             open my $rh, '<', $self->{Source}
77 6 50       201 or die "Failed to open $self->{Source} for reading: $!\n";
78 6         15 binmode $rh;
79 6         8 my $raw = do { local $/; <$rh> };
  6         33  
  6         325  
80 6         56 close $rh;
81              
82 6 100       25 if (not defined $self->{Encode}) {
83 5   50     25 $self->{Encode} = chardet($raw) // 'ASCII';
84             }
85 6         87 $raw = decode($self->{Encode}, $raw);
86              
87 6 50       2031 if (defined $out) {
88 0 0       0 open my $wh, '>', $out
89             or die "Failed to open $out for writing: $!\n";
90 0         0 binmode $wh, ':utf8';
91 0         0 print { $wh } $raw;
  0         0  
92 0         0 close $wh;
93 0         0 return $out;
94             } else {
95 6         74 return $raw;
96             }
97              
98             }
99              
100             sub metadata {
101              
102 6     6 0 1158 my $self = shift;
103              
104 6         172 return $self->{Metadata};
105              
106             }
107              
108 2     2 0 9 sub has_cover { 0 }
109              
110 1     1 0 4 sub cover { (undef, undef) }
111              
112 2     2 0 8 sub image_num { 0 }
113              
114 1     1 0 3 sub image { (undef, undef) }
115              
116             1;