File Coverage

blib/lib/EBook/MOBI/Converter.pm
Criterion Covered Total %
statement 120 120 100.0
branch 19 22 86.3
condition 3 4 75.0
subroutine 19 19 100.0
pod 15 15 100.0
total 176 180 97.7


"; "; \n"; "; " \n"; \n";
line stmt bran cond sub pod time code
1             package EBook::MOBI::Converter;
2              
3             our $VERSION = '0.7'; # VERSION (hook for Dist::Zilla::Plugin::OurPkgVersion)
4              
5 11     11   1052 use strict;
  11         22  
  11         424  
6 11     11   93 use warnings;
  11         23  
  11         355  
7              
8 11     11   2023 use HTML::Entities;
  11         31390  
  11         15548  
9              
10             #############################
11             # Constructor of this class #
12             #############################
13              
14             sub new {
15 37     37 1 583 my $self = shift;
16 37         68 my $ref = {};
17              
18 37         83 bless($ref, $self);
19              
20 37         127 return $ref;
21             }
22              
23             sub text {
24 12     12 1 364 my $self = shift;
25 12         67 my $txt = shift;
26              
27             #we have to make sure that HTML entities get encoded
28 12         73 my $mhtml = encode_entities($txt);
29              
30 12         248 return $mhtml;
31             }
32              
33             sub title {
34 67     67 1 1205 my $self = shift;
35 67         108 my $txt = shift;
36 67         87 my $lvl = shift;
37 67         82 my $toc = shift;
38              
39             # default values for lvl and toc (issue #31 on github)
40 67 100       171 unless( defined $lvl ) {
41 3         7 $lvl = 1;
42             }
43 67 100       145 unless( defined $toc ) {
44 66         90 $toc = 1;
45             }
46              
47 67 100       158 die("Titles can't be higher than level 6\n") if ($lvl > 6);
48              
49 66         84 my $mhtml = '';
50            
51 66 100       127 if ($toc) {
52 65         329 $mhtml = "$txt\n";
53             }
54             else {
55             # If the h1 (or any other level) should not appear in the TOC
56             # we put a whitespace in front of it.
57             # Like this the regex of the TOC generator does not find it.
58 1         9 $mhtml = " $txt \n";
59             }
60              
61 66         321 return $mhtml;
62             }
63              
64             sub paragraph {
65 2     2 1 6452 my $self = shift;
66 2         6 my $txt = shift;
67              
68 2         11 my $mhtml = "

$txt

\n";
69              
70 2         11 return $mhtml;
71             }
72              
73             sub newline {
74 8     8 1 467 my $self = shift;
75              
76 8         13 my $mhtml = "
\n";
77              
78 8         89 return $mhtml;
79             }
80              
81             sub pagebreak {
82 6     6 1 458 my $self = shift;
83              
84 6         15 my $mhtml = "\n";
85              
86 6         27 return $mhtml;
87             }
88              
89             sub italic {
90 2     2 1 484 my $self = shift;
91 2         12 my $txt = shift;
92              
93 2         8 my $mhtml = "$txt";
94              
95 2         8 return $mhtml;
96             }
97              
98             sub bold {
99 3     3 1 395 my $self = shift;
100 3         8 my $txt = shift;
101              
102 3         14 my $mhtml = "$txt";
103              
104 3         11 return $mhtml;
105             }
106              
107             sub code {
108 1     1 1 385 my $self = shift;
109 1         3 my $txt = shift;
110              
111 1         4 my $enc_txt = _nbsp($txt); # whitespaces
112 1         7 $enc_txt =~ s/\n/
\n/g; # line breaks
113              
114 1         5 my $mhtml = "$enc_txt\n";
115              
116 1         23 return $mhtml;
117             }
118              
119             sub small {
120 1     1 1 811 my $self = shift;
121 1         2 my $txt = shift;
122              
123 1         5 my $mhtml = "$txt";
124              
125 1         4 return $mhtml;
126             }
127              
128             sub big {
129 1     1 1 2058 my $self = shift;
130 1         3 my $txt = shift;
131              
132 1         4 my $mhtml = "$txt";
133              
134 1         4 return $mhtml;
135             }
136              
137             sub emphasize {
138 1     1 1 1291 my $self = shift;
139 1         3 my $txt = shift;
140              
141 1         4 my $mhtml = "$txt";
142              
143 1         4 return $mhtml;
144             }
145              
146             sub list {
147 2     2 1 3056 my $self = shift;
148 2         7 my $list_ref = shift;
149 2   100     14 my $list_type = shift || 'ul';
150              
151 2         6 my $mhtml = "<$list_type>\n";
152 2         5 foreach my $item (@{$list_ref}) {
  2         7  
153              
154 8         17 $mhtml .= "
  • $item
  • \n";
    155             }
    156 2         5 $mhtml .= "\n";
    157              
    158 2         8 return $mhtml;
    159             }
    160              
    161             sub table {
    162 2     2 1 2145 my $self = shift;
    163 2         9 my %table_data = @_;
    164              
    165 2         5 my $table_args = '';
    166 2 100       89 if (exists $table_data{border}) {
    167 1         6 $table_args .= ' border="'. $table_data{border} .'"';
    168             }
    169 2 100       9 if (exists $table_data{cellspacing}) {
    170 1         4 $table_args .= ' cellspacing="'. $table_data{cellspacing} .'"';
    171             }
    172 2 100       6 if (exists $table_data{cellpadding}) {
    173 1         16 $table_args .= ' cellpadding="'. $table_data{cellpadding} .'"';
    174             }
    175              
    176 2         3 my $mhtml;
    177 2         7 $mhtml = "\n";
    178              
    179 2 50       7 if (exists $table_data{th}) {
    180 2         4 my @table_header = @{$table_data{th}};
      2         6  
    181              
    182 2         6 $mhtml .= "
    183 2         4 foreach my $head (@table_header) {
    184 6         13 $mhtml .= "$head
    185             }
    186 2         5 $mhtml .= "
    187             }
    188              
    189 2 50       8 if (exists $table_data{td}) {
    190 2         5 my @table_datarow = @{$table_data{td}};
      2         6  
    191              
    192 2         5 foreach my $row (@table_datarow) {
    193 6         7 my @table_data= @{$row};
      6         14  
    194              
    195 6         7 $mhtml .= "
    196 6         7 foreach my $data (@table_data) {
    197 18         33 $mhtml .= "$data
    198             }
    199 6         12 $mhtml .= "
    200             }
    201             }
    202              
    203 2 100       8 if (exists $table_data{caption}) {
    204 1         4 $mhtml .= "
    $table_data{caption}
    205             }
    206              
    207 2         5 $mhtml .= "
    \n"; 208               209 2         9 return $mhtml; 210             } 211               212             sub image { 213 4     4 1 1699 my $self = shift; 214 4         7 my $path = shift; 215 4   50     14 my $description = shift || ''; 216               217             # We count the pictures, so that each has a number 218 4         20 $self->{img_count} ++; 219               220 4         6 my $mhtml; 221               222             # e.g.: 223             # recindex is MOBI specific, its the number of the picture, 224             # pointing into the picture records of the Mobi-format 225 4         16 $mhtml = ' 226             . ' recindex="' . $self->{img_count} .'" >' 227             . "\n"; 228               229             # Then we print out the image description 230 4 50       11 if ($description) { 231 4         15 $mhtml .= "

    $description

    \n"; 232             } 233               234 4         17 return $mhtml; 235             } 236               237             ################################################################### 238               239             ## replaces whitespace with html entitie 240             sub _nbsp { 241 1     1   2 my $string = shift; 242               243 1         9 $string =~ s/\ / /g; 244               245 1         3 return $string; 246             } 247               248             1; 249               250             __END__