File Coverage

blib/lib/EBook/Ishmael/EBook/Text.pm
Criterion Covered Total %
statement 64 76 84.2
branch 5 12 41.6
condition 1 2 50.0
subroutine 18 18 100.0
pod 0 9 0.0
total 88 117 75.2


line stmt bran cond sub pod time code
1             package EBook::Ishmael::EBook::Text;
2 17     17   355 use 5.016;
  17         68  
3             our $VERSION = '2.03';
4 17     17   134 use strict;
  17         35  
  17         658  
5 17     17   94 use warnings;
  17         47  
  17         1151  
6              
7 17     17   120 use Encode qw(decode);
  17         42  
  17         1129  
8 17     17   116 use File::Basename;
  17         35  
  17         1297  
9 17     17   101 use File::Spec;
  17         45  
  17         478  
10              
11 17     17   105 use EBook::Ishmael::CharDet;
  17         71  
  17         1032  
12 17     17   106 use EBook::Ishmael::EBook::Metadata;
  17         48  
  17         486  
13 17     17   84 use EBook::Ishmael::TextToHtml;
  17         55  
  17         13011  
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 28 my $class = shift;
21 12         25 my $file = shift;
22              
23 12         763 return -T $file;
24              
25             }
26              
27             sub new {
28              
29 11     11 0 22 my $class = shift;
30 11         22 my $file = shift;
31 11         24 my $enc = shift;
32              
33 11         114 my $self = {
34             Source => undef,
35             Metadata => EBook::Ishmael::EBook::Metadata->new,
36             Encode => $enc,
37             };
38              
39 11         26 bless $self, $class;
40              
41 11         425 $self->{Source} = File::Spec->rel2abs($file);
42              
43 11         691 $self->{Metadata}->set_title(basename($self->{Source}));
44 11         376 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
45 11         50 $self->{Metadata}->set_format('Text');
46              
47 11         48 return $self;
48              
49             }
50              
51             sub html {
52              
53 3     3 0 9 my $self = shift;
54 3         9 my $out = shift;
55              
56 3         16 my $html = text2html($self->raw);
57              
58 3 50       21 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         38 return $html;
67             }
68              
69             }
70              
71             sub raw {
72              
73 6     6 0 14 my $self = shift;
74 6         15 my $out = shift;
75              
76             open my $rh, '<', $self->{Source}
77 6 50       449 or die "Failed to open $self->{Source} for reading: $!\n";
78 6         28 binmode $rh;
79 6         13 my $raw = do { local $/; <$rh> };
  6         247  
  6         372  
80 6         78 close $rh;
81              
82 6 100       27 if (not defined $self->{Encode}) {
83 5   50     29 $self->{Encode} = chardet($raw) // 'ASCII';
84             }
85 6         222 say $self->{Encode};
86 6         103 $raw = decode($self->{Encode}, $raw);
87              
88 6 50       2946 if (defined $out) {
89 0 0       0 open my $wh, '>', $out
90             or die "Failed to open $out for writing: $!\n";
91 0         0 binmode $wh, ':utf8';
92 0         0 print { $wh } $raw;
  0         0  
93 0         0 close $wh;
94 0         0 return $out;
95             } else {
96 6         93 return $raw;
97             }
98              
99             }
100              
101             sub metadata {
102              
103 6     6 0 1525 my $self = shift;
104              
105 6         32 return $self->{Metadata};
106              
107             }
108              
109 2     2 0 11 sub has_cover { 0 }
110              
111 1     1 0 6 sub cover { (undef, undef) }
112              
113 2     2 0 9 sub image_num { 0 }
114              
115 1     1 0 5 sub image { (undef, undef) }
116              
117             1;