File Coverage

blib/lib/EBook/Ishmael/EBook/Text.pm
Criterion Covered Total %
statement 60 72 83.3
branch 4 12 33.3
condition 2 2 100.0
subroutine 17 17 100.0
pod 0 9 0.0
total 83 112 74.1


line stmt bran cond sub pod time code
1             package EBook::Ishmael::EBook::Text;
2 17     17   356 use 5.016;
  17         89  
3             our $VERSION = '2.01';
4 17     17   117 use strict;
  17         64  
  17         627  
5 17     17   98 use warnings;
  17         36  
  17         1250  
6              
7 17     17   146 use Encode qw(decode);
  17         65  
  17         1294  
8 17     17   115 use File::Basename;
  17         37  
  17         1500  
9 17     17   132 use File::Spec;
  17         67  
  17         550  
10              
11 17     17   116 use EBook::Ishmael::EBook::Metadata;
  17         53  
  17         479  
12 17     17   87 use EBook::Ishmael::TextToHtml;
  17         87  
  17         15133  
13              
14             # Use -T to check if file is a text file. EBook.pm's ebook_id() makes sure
15             # to use the Text heuristic last, so that it doesn't incorrectly identify other
16             # plain text ebook formats as just text.
17             sub heuristic {
18              
19 12     12 0 31 my $class = shift;
20 12         28 my $file = shift;
21              
22 12         863 return -T $file;
23              
24             }
25              
26             sub new {
27              
28 11     11 0 25 my $class = shift;
29 11         26 my $file = shift;
30 11   100     49 my $enc = shift // 'UTF-8';
31              
32 11         107 my $self = {
33             Source => undef,
34             Metadata => EBook::Ishmael::EBook::Metadata->new,
35             Encode => $enc,
36             };
37              
38 11         26 bless $self, $class;
39              
40 11         520 $self->{Source} = File::Spec->rel2abs($file);
41              
42 11         672 $self->{Metadata}->set_title(basename($self->{Source}));
43 11         346 $self->{Metadata}->set_modified((stat $self->{Source})[9]);
44 11         61 $self->{Metadata}->set_format('Text');
45              
46 11         115 return $self;
47              
48             }
49              
50             sub html {
51              
52 3     3 0 10 my $self = shift;
53 3         8 my $out = shift;
54              
55             open my $rh, '<', $self->{Source}
56 3 50       211 or die "Failed to open $self->{Source} for reading: $!\n";
57             my $html = text2html(
58             decode(
59             $self->{Encode},
60 3         16 do { local $/ = undef; <$rh> }
  3         24  
  3         319  
61             )
62             );
63 3         77 close $rh;
64              
65 3 50       16 if (defined $out) {
66 0 0       0 open my $wh, '>', $out
67             or die "Failed to open $out for writing: $!\n";
68 0         0 binmode $wh, ':utf8';
69 0         0 print { $wh } $html;
  0         0  
70 0         0 close $wh;
71 0         0 return $out;
72             } else {
73 3         41 return $html;
74             }
75              
76             }
77              
78             sub raw {
79              
80 3     3 0 8 my $self = shift;
81 3         7 my $out = shift;
82              
83             open my $rh, '<', $self->{Source}
84 3 50       222 or die "Failed to open $self->{Source} for reading: $!\n";
85             my $raw = decode(
86             $self->{Encode},
87 3         14 do { local $/ = undef; <$rh> }
  3         21  
  3         202  
88             );
89 3         362 close $rh;
90              
91 3 50       16 if (defined $out) {
92 0 0       0 open my $wh, '>', $out
93             or die "Failed to open $out for writing: $!\n";
94 0         0 binmode $wh, ':utf8';
95 0         0 print { $wh } $raw;
  0         0  
96 0         0 close $wh;
97 0         0 return $out;
98             } else {
99 3         28 return $raw;
100             }
101              
102             }
103              
104             sub metadata {
105              
106 6     6 0 1483 my $self = shift;
107              
108 6         35 return $self->{Metadata};
109              
110             }
111              
112 2     2 0 13 sub has_cover { 0 }
113              
114 1     1 0 6 sub cover { undef }
115              
116 2     2 0 10 sub image_num { 0 }
117              
118 1     1 0 6 sub image { undef }
119              
120             1;