blib/lib/EBook/Ishmael/EBook/PDF.pm

filter: Statementstmt 25.7% 49 / 190 Branchbran 0.0% 0 / 54 Conditioncond 15.0% 3 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 41.9% 13 / 31 Podpod 0.0% 0 / 9 totaltotal 20.7% 65 / 314 CC 33 SCAR 32.6
CC
33
Cov
20%
cover
90.0
_get_metadata
42.0
html
30.0
CRAP 26.0
« blib/lib/EBook/Ishmael/EBook/HTML.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/Epub.pm »
1 package EBook::Ishmael::EBook::PDF;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.04';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶use EBook::Ishmael::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use EBook::Ishmael::EBook::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use EBook::Ishmael::HTML qw(prepare_html);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use EBook::Ishmael::ImageID qw(image_path_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use EBook::Ishmael::ShellQuote qw(safe_qx);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # This module basically delegates the task of processing PDFs to some of
20 # Poppler's utilities. The processing is quite inefficient, and the output is
21 # ugly; PDF isn't really a format suited for plain text rendering.
22  
23 17 my $HAS_PDFTOHTML = defined which('pdftohtml');
24 17 my $HAS_PDFINFO = defined which('pdfinfo');
25 17 my $HAS_PDFTOPNG = defined which('pdftopng');
26 17 my $HAS_CONVERT = defined which('convert');
27 17 my $HAS_PDFIMAGES = defined which('pdfimages');
28  
29 17 ▶our $CAN_TEST = (
Condition25%3/12
$HAS_PDFTOHTML && $HAS_PDFINFO && ($HAS_PDFTOPNG || $HAS_CONVERT) && $HAS_PDFIMAGES
!ll&&!rl&&r
1700
$HAS_PDFTOHTML and $HAS_PDFINFO and $HAS_PDFTOPNG || $HAS_CONVERT
!ll&&!rl&&r
1700
$HAS_PDFTOHTML and $HAS_PDFINFO
!ll&&!rl&&r
1700
$HAS_PDFTOPNG or $HAS_CONVERT
l!l&&r!l&&!r
000
MC/DC0%0/5
$HAS_PDFTOHTML && $HAS_PDFINFO && ($HAS_PDFTOPNG || $HAS_CONVERT) && $HAS_PDFIMAGES
$HAS_PDFTOHTML $HAS_PDFINFO $HAS_PDFTOPNG $HAS_CONVERT $HAS_PDFIMAGES
Truth table0%0/7
A and B and C or D && E
A= $HAS_PDFTOHTMLB= $HAS_PDFINFOC= $HAS_PDFTOPNGD= $HAS_CONVERTE= $HAS_PDFIMAGES
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
30 $HAS_PDFTOHTML and
31 $HAS_PDFINFO and
32 ($HAS_PDFTOPNG or $HAS_CONVERT) and
33 $HAS_PDFIMAGES
34 );
35  
36 17 my $MAGIC = '%PDF';
37  
38 sub heuristic {
39  
40 47 ▶ my $class = shift;
Subroutine heuristic: called CC=1 SCAR=0.0
Pod undocumented
41 47 my $file = shift;
42 47 my $fh = shift;
43  
44 47 read $fh, my ($mag), 4;
45  
46 47 return $mag eq $MAGIC;
47  
48 }
49  
50 sub _parse_pdf_date {
51  
52 0 ▶ my ($date) = @_;
Subroutine _parse_pdf_date: not called CC=4 SCAR=30.0
53 0 $date =~ s/^D://;
54 0 $date =~ s/'//g;
55  
56 0 ▶ my ($year, $month, $day, $hour, $min, $sec, $tz) =
Branch: 1 branch
BranchTrueFalseTotal
() = $date =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(Z|[+-]\d{4})$/u or die "Invalid PDF date\n" 0 0 0
57 $date =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(Z|[+-]\d{4})$/
58 or die "Invalid PDF date\n";
59  
60 0 ▶ if ($tz eq 'Z') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tz eq "Z") 0 0 0
61 0 $tz = '+0000';
62 }
63  
64 0 ▶ my $tp = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $tp = eval { do { "Time::Piece"->strptime("$year $month $day $hour $min $sec $tz", "%Y %m %d %H %M %S %z") } } or die "Failed to parse '${date}'\n" 0 0 0
65 0 Time::Piece->strptime(
66 "$year $month $day $hour $min $sec $tz",
67 '%Y %m %d %H %M %S %z',
68 )
69 } or die "Failed to parse '$date'\n";
70 0 return $tp->epoch;
71  
72 }
73  
74 sub _get_metadata {
75  
76 0 ▶ my $self = shift;
Subroutine _get_metadata: not called CC=6 SCAR=37.4
77  
78 my %meta = (
79 0 ▶ 'Author' => sub { $self->{Metadata}->add_author(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 ▶ 'CreationDate' => sub { $self->{Metadata}->set_created(eval { _parse_pdf_date(shift) }) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 ▶ 'Creator' => sub { $self->{Metadata}->add_contributor(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 ▶ 'ModDate' => sub { $self->{Metadata}->set_modified(eval { _parse_pdf_date(shift) }) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 ▶ 'PDF version' => sub { $self->{Metadata}->set_format('PDF ' . shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 ▶ 'Producer' => sub { $self->{Metadata}->add_contributor(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 ▶ 'Title' => sub { $self->{Metadata}->set_title(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 );
87  
88 0 ▶ if (!$HAS_PDFINFO) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAS_PDFINFO) 0 0 0
89 0 die "Cannot read PDF $self->{Source}: pdfinfo not installed\n";
90 }
91  
92 0 my $info = safe_qx('pdfinfo', '-rawdates', $self->{Source});
93 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
94 0 die "Failed to run 'pdfinfo' on $self->{Source}\n";
95 }
96  
97 0 for my $l (split /\n/, $info) {
98 0 my ($field, $content) = split /:\s*/, $l, 2;
99 0 ▶ unless (exists $meta{ $field } and $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $meta{$field} and $content) 0 0 0
Condition0%0/3
exists $meta{$field} and $content
!ll&&!rl&&r
000
MC/DC0%0/2
exists $meta{$field} and $content
exists $meta{$field} $content
Truth table0%0/3
A and B
A= exists $meta{$field}B= $content
ABresult
0X0
100
111
100 0 next;
101 }
102 0 $meta{$field}->($content);
103 }
104  
105 0 return 1;
106  
107 }
108  
109 sub _images {
110  
111 0 ▶ my $self = shift;
Subroutine _images: not called CC=4 SCAR=30.0
112  
113 0 $self->{_imgdir} = tempdir(CLEANUP => 1);
114  
115 0 my $root = File::Spec->catfile($self->{_imgdir}, 'ishmael');
116  
117 # TODO: Couldn't we just use system?
118 0 safe_qx('pdfimages', '-png', $self->{Source}, $root);
119 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
120 0 die "Failed to run 'pdfimages' on $self->{Source}\n";
121 }
122  
123 0 for my $f (dir($self->{_imgdir})) {
124 0 my $format = image_path_id($f);
125 0 ▶ next if not defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
126 0 push @{ $self->{_images} }, [ $f, $format ];
127 }
128  
129 0 return 1;
130  
131 }
132  
133 sub new {
134  
135 0 ▶ my $class = shift;
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
136 0 my $file = shift;
137 0 my $enc = shift;
138 0 ▶ my $net = shift // 1;
Condition0%0/2
shift() // 1
l!l
00
MC/DC0%0/1
shift() // 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
139  
140 0 my $self = {
141 Source => undef,
142 Metadata => EBook::Ishmael::EBook::Metadata->new,
143 Network => $net,
144 _imgdir => undef,
145 _images => [],
146 };
147  
148 0 bless $self, $class;
149  
150 0 $self->{Source} = File::Spec->rel2abs($file);
151  
152 0 $self->_get_metadata();
153  
154 0 ▶ if (not defined $self->{Metadata}->format) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Metadata"}->format) 0 0 0
155 0 $self->{Metadata}->set_format('PDF');
156 }
157  
158 0 return $self;
159  
160 }
161  
162 sub html {
163  
164 0 ▶ my $self = shift;
Subroutine html: not called CC=5 SCAR=34.0
Pod undocumented
165 0 my $out = shift;
166  
167 0 ▶ if (!$HAS_PDFTOHTML) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAS_PDFTOHTML) 0 0 0
168 0 die "Cannot read PDF $self->{Source}: pdftohtml not installed\n";
169 }
170  
171 0 my $raw = safe_qx('pdftohtml', '-i', '-s', '-stdout', $self->{Source});
172 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
173 0 die "Failed to run 'pdftohtml' on $self->{Source}\n";
174 }
175  
176 my $dom = XML::LibXML->load_html(
177 string => $raw,
178 no_network => !$self->{Network},
179 0 );
180  
181 0 my ($body) = $dom->findnodes('/html/body');
182 0 prepare_html($body);
183  
184 0 my $html = join '', map { $_->toString } $body->childNodes;
185  
186 0 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 0 0
187 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
188 or die "Failed to open $out for writing: $!\n";
189 0 binmode $fh, ':utf8';
190 0 print { $fh } $html;
191 0 close $fh;
192 0 return $out;
193 } else {
194 0 return $html;
195 }
196  
197 }
198  
199 sub raw {
200  
201 0 ▶ my $self = shift;
Subroutine raw: not called CC=5 SCAR=34.0
Pod undocumented
202 0 my $out = shift;
203  
204 0 ▶ if (!$HAS_PDFTOHTML) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAS_PDFTOHTML) 0 0 0
205 0 die "Cannot read PDF $self->{Source}: pdftohtml not installed\n";
206 }
207  
208 0 my $rawml = safe_qx('pdftohtml', '-i', '-s', '-stdout', $self->{Source});
209 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
210 0 die "Failed to run 'pdftohtml' on $self->{Source}\n";
211 }
212  
213 my $dom = XML::LibXML->load_html(
214 string => $rawml,
215 no_network => !$self->{Network},
216 0 );
217  
218 0 my ($body) = $dom->findnodes('/html/body');
219 0 prepare_html($body);
220  
221 0 my $raw = join '', $body->textContent;
222  
223 0 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 0 0 0
224 0 ▶ open my $fh, '>', $out
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or die "Failed to open $out for writing: $!\n" 0 0 0
225 or die "Failed to open $out for writing: $!\n";
226 0 binmode $fh, ':utf8';
227 0 print { $fh } $raw;
228 0 close $fh;
229 0 return $out;
230 } else {
231 0 return $raw;
232 }
233  
234 }
235  
236 sub metadata {
237  
238 0 ▶ my $self = shift;
Subroutine metadata: not called CC=1 SCAR=6.9
Pod undocumented
239  
240 0 return $self->{Metadata};
241  
242 }
243  
244 0 ▶sub has_cover { 1 }
Subroutine has_cover: not called CC=1 SCAR=6.9
Pod undocumented
245  
246 # Convert the first page of the PDF to a png as the cover, using either
247 # xpdf's pdftopng or ImageMagick's convert.
248 sub cover {
249  
250 0 ▶ my $self = shift;
Subroutine cover: not called CC=9 SCAR=45.0
Pod undocumented
251  
252 0 ▶ unless ($HAS_PDFTOPNG or $HAS_CONVERT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAS_PDFTOPNG or $HAS_CONVERT) 0 0 0
Condition0%0/3
$HAS_PDFTOPNG or $HAS_CONVERT
l!l&&r!l&&!r
000
MC/DC0%0/2
$HAS_PDFTOPNG or $HAS_CONVERT
$HAS_PDFTOPNG $HAS_CONVERT
Truth table0%0/3
A or B
A= $HAS_PDFTOPNGB= $HAS_CONVERT
ABresult
1X1
011
000
253 0 die "Cannot dump PDF $self->{Source} cover: pdftopng or convert not installed\n";
254 }
255  
256 0 my $png;
257  
258 0 ▶ if ($HAS_PDFTOPNG) {
Branch: 2 branches
BranchTrueFalseTotal
if ($HAS_PDFTOPNG) { } 0 0 0
elsif ($HAS_CONVERT) { } 0 0 0
259 0 my $tmpdir = tempdir(CLEANUP => 1);
260 0 my $tmproot = File::Spec->catfile($tmpdir, 'tmp');
261  
262 0 safe_qx('pdftopng', '-f', 1, '-l', 1, $self->{Source}, $tmproot);
263 0 ▶ unless ($? >> 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? >> 8 == 0) 0 0 0
264 0 die "Failed to run 'pdftopng' on $self->{Source}\n";
265 }
266  
267 0 $png = (dir($tmpdir))[0];
268 0 ▶ unless (defined $png) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $png) 0 0 0
269 0 die "'pdftopng' could not produce a cover image from $self->{Source}\n";
270 }
271  
272 } elsif ($HAS_CONVERT) {
273 0 my $tmppath = do {
274 0 my ($h, $p) = tempfile(UNLINK => 1);
275 0 close $h;
276 0 $p;
277 };
278  
279 # The '[0]' means the first page only
280 0 safe_qx('convert', "$self->{Source}\[0\]", '-alpha', 'deactivate', "png:$tmppath");
281 0 ▶ if (not -f $tmppath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $tmppath) 0 0 0
282 0 die "'convert' could not produce a cover image from $self->{Source}\n";
283 }
284 0 $png = $tmppath;
285  
286 } else {
287 0 die;
288 }
289  
290 0 ▶ open my $fh, '<', $png
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $png or die "Failed to open $png for reading: $!\n" 0 0 0
291 or die "Failed to open $png for reading: $!\n";
292 0 binmode $fh;
293 0 my $bin = do { local $/ = undef; readline $fh };
294 0 close $fh;
295  
296 0 return ($bin, 'png');
297  
298  
299 }
300  
301 sub image_num {
302  
303 0 ▶ my $self = shift;
Subroutine image_num: not called CC=2 SCAR=17.9
Pod undocumented
304  
305 0 ▶ unless (defined $self->{_imgdir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_imgdir"}) 0 0 0
306 0 $self->_images;
307 }
308  
309 0 return scalar @{ $self->{_images} };
310  
311 }
312  
313 sub image {
314  
315 0 ▶ my $self = shift;
Subroutine image: not called CC=3 SCAR=24.8
Pod undocumented
316 0 my $n = shift;
317  
318 0 ▶ if ($n >= $self->image_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->image_num) 0 0 0
319 0 return (undef, undef);
320 }
321  
322 0 ▶ open my $fh, '<', $self->{_images}[$n][0]
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"_images"}[$n][0] or die qq[Failed to open $self->{"_images"}[$n][0]: $!\n] 0 0 0
323 or die "Failed to open $self->{_images}[$n][0]: $!\n";
324 0 binmode $fh;
325 0 my $img = do { local $/ = undef; readline $fh };
326 0 close $fh;
327  
328 0 return ($img, $self->{_images}[$n][1]);
329  
330 }
331  
332 17 1;
« blib/lib/EBook/Ishmael/EBook/HTML.pm ↑ Summary blib/lib/EBook/Ishmael/EBook/Epub.pm »