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 |
line | stmt | bran | cond | sub | pod | time | code | |
---|---|---|---|---|---|---|---|---|
1 | package EBook::MOBI::Converter; | |||||||
2 | ||||||||
3 | our $VERSION = '0.71'; # TRIAL VERSION (hook for Dist::Zilla::Plugin::OurPkgVersion) | |||||||
4 | ||||||||
5 | 11 | 11 | 839 | use strict; | ||||
11 | 18 | |||||||
11 | 262 | |||||||
6 | 11 | 11 | 49 | use warnings; | ||||
11 | 18 | |||||||
11 | 282 | |||||||
7 | ||||||||
8 | 11 | 11 | 1463 | use HTML::Entities; | ||||
11 | 11452 | |||||||
11 | 12307 | |||||||
9 | ||||||||
10 | ############################# | |||||||
11 | # Constructor of this class # | |||||||
12 | ############################# | |||||||
13 | ||||||||
14 | sub new { | |||||||
15 | 37 | 37 | 1 | 446 | my $self = shift; | |||
16 | 37 | 73 | my $ref = {}; | |||||
17 | ||||||||
18 | 37 | 72 | bless($ref, $self); | |||||
19 | ||||||||
20 | 37 | 110 | return $ref; | |||||
21 | } | |||||||
22 | ||||||||
23 | sub text { | |||||||
24 | 12 | 12 | 1 | 324 | my $self = shift; | |||
25 | 12 | 16 | my $txt = shift; | |||||
26 | ||||||||
27 | #we have to make sure that HTML entities get encoded | |||||||
28 | 12 | 35 | my $mhtml = encode_entities($txt); | |||||
29 | ||||||||
30 | 12 | 207 | return $mhtml; | |||||
31 | } | |||||||
32 | ||||||||
33 | sub title { | |||||||
34 | 67 | 67 | 1 | 1058 | my $self = shift; | |||
35 | 67 | 94 | my $txt = shift; | |||||
36 | 67 | 91 | my $lvl = shift; | |||||
37 | 67 | 87 | my $toc = shift; | |||||
38 | ||||||||
39 | # default values for lvl and toc (issue #31 on github) | |||||||
40 | 67 | 100 | 158 | unless( defined $lvl ) { | ||||
41 | 3 | 4 | $lvl = 1; | |||||
42 | } | |||||||
43 | 67 | 100 | 138 | unless( defined $toc ) { | ||||
44 | 66 | 80 | $toc = 1; | |||||
45 | } | |||||||
46 | ||||||||
47 | 67 | 100 | 155 | die("Titles can't be higher than level 6\n") if ($lvl > 6); | ||||
48 | ||||||||
49 | 66 | 84 | my $mhtml = ''; | |||||
50 | ||||||||
51 | 66 | 100 | 123 | if ($toc) { | ||||
52 | 65 | 185 | $mhtml = " |
|||||
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 | 8 | $mhtml = " |
|||||
59 | } | |||||||
60 | ||||||||
61 | 66 | 256 | return $mhtml; | |||||
62 | } | |||||||
63 | ||||||||
64 | sub paragraph { | |||||||
65 | 2 | 2 | 1 | 219 | my $self = shift; | |||
66 | 2 | 4 | my $txt = shift; | |||||
67 | ||||||||
68 | 2 | 8 | my $mhtml = " $txt \n"; |
|||||
69 | ||||||||
70 | 2 | 5 | return $mhtml; | |||||
71 | } | |||||||
72 | ||||||||
73 | sub newline { | |||||||
74 | 8 | 8 | 1 | 328 | my $self = shift; | |||
75 | ||||||||
76 | 8 | 10 | my $mhtml = " \n"; |
|||||
77 | ||||||||
78 | 8 | 41 | return $mhtml; | |||||
79 | } | |||||||
80 | ||||||||
81 | sub pagebreak { | |||||||
82 | 6 | 6 | 1 | 319 | my $self = shift; | |||
83 | ||||||||
84 | 6 | 10 | my $mhtml = " |
|||||
85 | ||||||||
86 | 6 | 25 | return $mhtml; | |||||
87 | } | |||||||
88 | ||||||||
89 | sub italic { | |||||||
90 | 2 | 2 | 1 | 326 | my $self = shift; | |||
91 | 2 | 4 | my $txt = shift; | |||||
92 | ||||||||
93 | 2 | 6 | my $mhtml = "$txt"; | |||||
94 | ||||||||
95 | 2 | 5 | return $mhtml; | |||||
96 | } | |||||||
97 | ||||||||
98 | sub bold { | |||||||
99 | 3 | 3 | 1 | 318 | my $self = shift; | |||
100 | 3 | 6 | my $txt = shift; | |||||
101 | ||||||||
102 | 3 | 9 | my $mhtml = "$txt"; | |||||
103 | ||||||||
104 | 3 | 15 | return $mhtml; | |||||
105 | } | |||||||
106 | ||||||||
107 | sub code { | |||||||
108 | 1 | 1 | 1 | 308 | my $self = shift; | |||
109 | 1 | 3 | my $txt = shift; | |||||
110 | ||||||||
111 | 1 | 2 | my $enc_txt = _nbsp($txt); # whitespaces | |||||
112 | 1 | 3 | $enc_txt =~ s/\n/ \n/g; # line breaks |
|||||
113 | ||||||||
114 | 1 | 4 | my $mhtml = "$enc_txt \n"; |
|||||
115 | ||||||||
116 | 1 | 17 | return $mhtml; | |||||
117 | } | |||||||
118 | ||||||||
119 | sub small { | |||||||
120 | 1 | 1 | 1 | 306 | my $self = shift; | |||
121 | 1 | 2 | my $txt = shift; | |||||
122 | ||||||||
123 | 1 | 3 | my $mhtml = "$txt"; | |||||
124 | ||||||||
125 | 1 | 3 | return $mhtml; | |||||
126 | } | |||||||
127 | ||||||||
128 | sub big { | |||||||
129 | 1 | 1 | 1 | 303 | my $self = shift; | |||
130 | 1 | 3 | my $txt = shift; | |||||
131 | ||||||||
132 | 1 | 2 | my $mhtml = "$txt"; | |||||
133 | ||||||||
134 | 1 | 3 | return $mhtml; | |||||
135 | } | |||||||
136 | ||||||||
137 | sub emphasize { | |||||||
138 | 1 | 1 | 1 | 304 | my $self = shift; | |||
139 | 1 | 2 | my $txt = shift; | |||||
140 | ||||||||
141 | 1 | 3 | my $mhtml = "$txt"; | |||||
142 | ||||||||
143 | 1 | 2 | return $mhtml; | |||||
144 | } | |||||||
145 | ||||||||
146 | sub list { | |||||||
147 | 2 | 2 | 1 | 648 | my $self = shift; | |||
148 | 2 | 4 | my $list_ref = shift; | |||||
149 | 2 | 100 | 9 | my $list_type = shift || 'ul'; | ||||
150 | ||||||||
151 | 2 | 3 | my $mhtml = "<$list_type>\n"; | |||||
152 | 2 | 3 | foreach my $item (@{$list_ref}) { | |||||
2 | 4 | |||||||
153 | ||||||||
154 | 8 | 15 | $mhtml .= " |
|||||
155 | } | |||||||
156 | 2 | 4 | $mhtml .= "\n"; | |||||
157 | ||||||||
158 | 2 | 5 | return $mhtml; | |||||
159 | } | |||||||
160 | ||||||||
161 | sub table { | |||||||
162 | 2 | 2 | 1 | 676 | my $self = shift; | |||
163 | 2 | 9 | my %table_data = @_; | |||||
164 | ||||||||
165 | 2 | 3 | my $table_args = ''; | |||||
166 | 2 | 100 | 5 | if (exists $table_data{border}) { | ||||
167 | 1 | 3 | $table_args .= ' border="'. $table_data{border} .'"'; | |||||
168 | } | |||||||
169 | 2 | 100 | 5 | if (exists $table_data{cellspacing}) { | ||||
170 | 1 | 3 | $table_args .= ' cellspacing="'. $table_data{cellspacing} .'"'; | |||||
171 | } | |||||||
172 | 2 | 100 | 5 | if (exists $table_data{cellpadding}) { | ||||
173 | 1 | 2 | $table_args .= ' cellpadding="'. $table_data{cellpadding} .'"'; | |||||
174 | } | |||||||
175 | ||||||||
176 | 2 | 2 | my $mhtml; | |||||
177 | 2 | 4 | $mhtml = " |
|||||
178 | ||||||||
179 | 2 | 50 | 5 | if (exists $table_data{th}) { | ||||
180 | 2 | 2 | my @table_header = @{$table_data{th}}; | |||||
2 | 4 | |||||||
181 | ||||||||
182 | 2 | 4 | $mhtml .= " | |||||
183 | 2 | 3 | foreach my $head (@table_header) { | |||||
184 | 6 | 10 | $mhtml .= " | $head | ||||
185 | } | |||||||
186 | 2 | 4 | $mhtml .= " | |||||
187 | } | |||||||
188 | ||||||||
189 | 2 | 50 | 6 | if (exists $table_data{td}) { | ||||
190 | 2 | 2 | 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 | 12 | |||||||
194 | ||||||||
195 | 6 | 9 | $mhtml .= " | |||||
196 | 6 | 13 | foreach my $data (@table_data) { | |||||
197 | 18 | 33 | $mhtml .= " | $data | ||||
198 | } | |||||||
199 | 6 | 15 | $mhtml .= " | |||||
200 | } | |||||||
201 | } | |||||||
202 | ||||||||
203 | 2 | 100 | 6 | if (exists $table_data{caption}) { | ||||
204 | 1 | 5 | $mhtml .= " | |||||
205 | } | |||||||
206 | ||||||||
207 | 2 | 3 | $mhtml .= " |
$description
\n"; 232 } 233 234 4 13 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 7 $string =~ s/\ / /g; 244 245 1 2 return $string; 246 } 247 248 1; 249 250 __END__