Generator/Parser.pm

filter: Statementstmt 8.5% 25 / 293 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 17.7% 8 / 45 Podpod 0.0% 0 / 20 totaltotal 6.3% 33 / 517 CC 73 SCAR 54.5
CC
73
Cov
6%
download_file
756.0
find_content
272.0
node_to_structure
132.0
CRAP 233.9
↑ Summary Generator/Exporter/PDF.pm »
1 package EBook::Generator::Parser;
2  
3 1 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Image::Size;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.01';
14  
15 sub new
16 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
17 0 my ($class, @args) = @_;
18 0 my $self = bless {}, $class;
19 0 return $self->init(@args);
20 }
21  
22 sub init
23 0 ▶{
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my ($self, $browser, $xml_parser, $log) = @_;
25  
26 0 $self->{'tmp-dir'} = '/tmp/';
27  
28 0 $self->{'browser'} = $browser;
29  
30 0 $self->{'xml-parser'} = $xml_parser;
31  
32 0 $self->{'log'} = $log;
33  
34 0 $self->{'class-id-filter-words'} =
35 ['sidebar','respond','nav','menu','social','comment','bookmark',
36 'advert','next','prev','like','footer',' ad ','respost','respond'];
37
38 0 $self->{'ignored-tags'} =
39 ['script','form','input','textarea','fieldset','select','head','meta','link'];
40  
41 0 $self->{'command-convert'} = getCommandPath('convert');
42 $self->{'command-convert'} = '/usr/local/bin/convert' if
43 0 ▶ $self->{'command-convert'} eq 'convert';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"command-convert"} eq "convert" 0 0 0
44  
45 0 return $self;
46 }
47  
48 sub getCommandPath
49 0 ▶{
Subroutine getCommandPath: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my ($command) = @_;
51 0 my $path = `which $command`;
52 0 $path =~ s/[\s\t\n\r]*$//g;
53 0 ▶ return (length $path ? $path : $command);
Branch: 1 branch
BranchTrueFalseTotal
length $path ? : 0 0 0
54 }
55  
56 sub parseContent
57 0 ▶{
Subroutine parseContent: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my ($self, $content, $url, $options) = @_;
59  
60 0 $self->{'options'} = $options;
61  
62 # cleanup html
63 0 my $html = $self->cleanup_html($content);
64  
65 # parse html file into DOM structure
66 0 my $tree = $self->{'xml-parser'}->parse_html_string($html);
67 0 my $dom = $tree->getDocumentElement();
68  
69 # extract meta information
70 0 $self->{'meta'} = {
71 'url' => $url,
72 'title' => $self->find_title($dom),
73 'author' => $self->find_author($dom),
74 'date' => $self->find_date($dom),
75 'abstract' => $self->find_abstract($dom),
76 };
77  
78 # find content container and extract content
79 0 $self->find_content($dom);
80
81 0 $self->{'files'} = [];
82 0 $self->{'file-basename'} = $self->get_filebasename();
83  
84 0 $self->{'data'} = $self->dom_to_structure($dom);
85
86 0 return ($self->{'meta'}, $self->{'data'});
87 }
88  
89 sub cleanup
90 0 ▶{
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my ($self) = @_;
92 0 map { unlink($_) } @{$self->{'files'}};
93 0 $self->{'files'} = [];
94 }
95  
96 sub dom_to_structure
97 0 ▶{
Subroutine dom_to_structure: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my ($self, $dom) = @_;
99 0 return ['document', {}, $self->node_to_structure($dom)];
100 }
101  
102 sub logNode
103 0 ▶{
Subroutine logNode: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my ($self, $node) = @_;
105 0 my $s = $node->toString();
106 0 $s =~ s/</[/g;
107 0 $s =~ s/>/]/g;
108 0 push @{$self->{'log'}}, $s;
109 }
110  
111 # elem = "..." | [type, {options...}, subelem1, subelem2, ...]
112 # types: list, headline, paragraph, text, link, preformatted, picture,
113 # table, footnote?, ...?
114 #
115 sub node_to_structure
116 0 ▶{
Subroutine node_to_structure: not called CC=11 SCAR=48.8
Pod undocumented
117 0 my ($self, $node) = @_;
118 my %handlers = (
119 0 ▶ 'p' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 my ($self, $node) = @_;
121 0 return ['paragraph', {}, $self->children_to_structure($node)];
122 },
123 0 ▶ 'blockquote' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 my ($self, $node) = @_;
125 0 my $text = $self->node_to_text($node);
126 0 $text =~ s/[\n\r]/ /g;
127 0 return ['paragraph', {}, ['quote', {}, $text]];
128 },
129 0 ▶ 'quote' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 my ($self, $node) = @_;
131 0 return ['quote', {}, $self->children_to_structure($node)];
132 },
133 0 ▶ 'h1' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 my ($self, $node) = @_;
135 0 $self->{'has-section'}++;
136 0 my $text = $self->node_to_text($node); #_children_to_latex($node)
137 0 return ['headline', {'level' => 1}, $text];
138 },
139 0 ▶ 'h2' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 my ($self, $node) = @_;
141 0 $self->{'has-section'}++;
142 0 my $text = $self->node_to_text($node); #_children_to_latex($node)
143 0 return ['headline', {'level' => 2}, $text];
144 },
145 0 ▶ 'h3' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 my ($self, $node) = @_;
147 0 $self->{'has-section'}++;
148 0 my $text = $self->node_to_text($node); #_children_to_latex($node)
149 0 return ['headline', {'level' => 3}, $text];
150 },
151 0 ▶ 'h4' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 0 my ($self, $node) = @_;
153 0 $self->{'has-section'}++;
154 0 my $text = $self->node_to_text($node); #_children_to_latex($node)
155 0 return ['headline', {'level' => 4}, $text];
156 },
157 0 ▶ 'h5' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 my ($self, $node) = @_;
159 0 $self->{'has-section'}++;
160 0 my $text = $self->node_to_text($node); #_children_to_latex($node)
161 0 return ['headline', {'level' => 5}, $text];
162 },
163 0 ▶ 'b|strong' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 my ($self, $node) = @_;
165 0 return ['text', {'weight' => 'bold'}, $self->children_to_structure($node)];
166 },
167 0 ▶ 'i|emph' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 my ($self, $node) = @_;
169 0 return ['text', {'style' => 'italic'}, $self->children_to_structure($node)];
170 },
171 0 ▶ 'ul' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 my ($self, $node) = @_;
173 0 return ['list', {'type' => 'unordered'}, $self->children_to_structure($node)];
174 },
175 0 ▶ 'ol' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
176 0 my ($self, $node) = @_;
177 0 return ['list', {'type' => 'ordered'}, $self->children_to_structure($node)];
178 },
179 0 ▶ 'li' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 my ($self, $node) = @_;
181 #push @{$self->{'log'}}, "---";
182 #$self->logNode($node);
183 0 my $res = ['text', {}, $self->children_to_structure($node)];
184 #push @{$self->{'log'}}, Dumper($res);
185 0 return $res;
186 },
187 0 ▶ 'a' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 my ($self, $node) = @_;
189 0 my $href = $node->getAttribute('href');
190 0 ▶ if ($self->is_external_link($href)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_external_link($href)) { } 0 0 0
191 0 return ['link', {'target' => $href}, $self->children_to_structure($node)];
192 } else {
193 0 return ($self->children_to_structure($node));
194 }
195 },
196 0 ▶ 'code' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 0 my ($self, $node) = @_;
198 0 return ['text', {'preformatted' => 1}, $self->children_to_structure($node)];
199 },
200 0 ▶ 'pre' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 0 my ($self, $node) = @_;
202 0 return ['preformatted', {}, $self->children_to_structure($node)];
203 },
204 0 ▶ 'img' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 my ($self, $node) = @_;
206 0 my ($filename, $url) = $self->download_file($self->{'meta'}->{'url'}, $node);
207 0 ▶ return () unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 0 0
208 0 my $filetype = $filename;
209 0 $filetype =~ s/^.*\.([^\.]*)$/$1/;
210 0 return ['media', {'type' => $filetype, 'filename' => $filename, 'url' => $url}];
211 },
212 0 );
213  
214 0 my @data = ();
215 0 my $type = $node->nodeType;
216 0 ▶ if ($type == XML_ELEMENT_NODE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 1) { } 0 0 0
elsif ($type == 3) { } 0 0 0
217 0 my $name = lc $node->tagName;
218 0 ▶ my $class = ($node->hasAttribute('class') ? $node->getAttribute('class') : '');
Branch: 1 branch
BranchTrueFalseTotal
$node->hasAttribute('class') ? : 0 0 0
219 0 ▶ my $id = ($node->hasAttribute('id') ? $node->getAttribute('id') : '');
Branch: 1 branch
BranchTrueFalseTotal
$node->hasAttribute('id') ? : 0 0 0
220
221 #print "[$name]\n";
222
223 0 my $ignored_tags = join '|', map { quotemeta } @{$self->{'ignored-tags'}};
224 0 ▶ if ($name !~ /($ignored_tags)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /($ignored_tags)/) 0 0 0
225 0 my $classid = $class.$id;
226 0 my $filter_words = join '|', map { quotemeta } @{$self->{'class-id-filter-words'}};
227 0 ▶ if ($classid =~ /($filter_words)/im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classid =~ /($filter_words)/im) { } 0 0 0
228 # bad
229 } else {
230 # let the specific function handle the tag
231 0 my $found = 0;
232 0 foreach my $pattern (keys %handlers) {
233 0 ▶ if ($name =~ /^($pattern)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^($pattern)$/) 0 0 0
234 0 push @data, $handlers{$pattern}->($self, $node);
235 0 $found = 1;
236 0 last;
237 }
238 }
239 0 ▶ push @data, $self->children_to_structure($node)
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 0 0
240 unless $found;
241 }
242 }
243 }
244 elsif ($type == XML_TEXT_NODE) {
245 0 my $value = $node->nodeValue();
246 #push @{$self->{'log'}}, $node->nodeValue();
247 #$self->logNode($node);
248 0 $value =~ s/^[\s\t\n\r]*//g;
249 0 $value =~ s/[\s\t\n\r]*$//g;
250 0 ▶ push @data, $value
Branch: 1 branch
BranchTrueFalseTotal
if length $value 0 0 0
251 if length $value;
252 }
253 #else {
254 # push @{$self->{'log'}}, $type;
255 #}
256 0 return (@data);
257 }
258  
259 sub children_to_structure
260 0 ▶{
Subroutine children_to_structure: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my ($self, $node) = @_;
262 0 my @data;
263 0 map { push @data, $self->node_to_structure($_); }
264 $node->childNodes()->get_nodelist();
265 0 return @data;
266 }
267  
268 sub is_external_link
269 0 ▶{
Subroutine is_external_link: not called CC=5 SCAR=34.0
Pod undocumented
270 0 my ($self, $href) = @_;
271 0 ▶ return 0 unless defined $href;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $href 0 0 0
272 0 ▶ return 0 if $href =~ /javascript/;
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ /javascript/ 0 0 0
273 0 ▶ return 0 if $href !~ /^((ht|f)tp|mailto):\/\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $href =~ m[^((ht|f)tp|mailto)://] 0 0 0
274 0 my $baseurl = $self->{'meta'}->{'url'};
275 0 $baseurl =~ s/^(((ht|f)tp|mailto):\/\/[a-zA-Z0-9\.]+).*$/$1/;
276 0 $baseurl = quotemeta $baseurl;
277 #print "[$baseurl]\n[$href]\n\n";
278 0 ▶ return 0 if $href =~ /^$baseurl/;
Branch: 1 branch
BranchTrueFalseTotal
if $href =~ /^$baseurl/ 0 0 0
279 0 return 1;
280 }
281  
282 sub get_filebasename
283 0 ▶{
Subroutine get_filebasename: not called CC=1 SCAR=6.9
Pod undocumented
284 0 return time();
285 }
286  
287 sub download_file
288 0 ▶{
Subroutine download_file: not called CC=27 SCAR=66.3
Pod undocumented
289 0 my ($self, $url, $node) = @_;
290
291 return (undef, undef)
292 if $node->hasAttribute('width') &&
293 $node->getAttribute('width') !~ /\%$/ &&
294 0 ▶ $node->getAttribute('width') < $self->{'options'}->{'min-img-width'};
Branch: 1 branch
BranchTrueFalseTotal
if $node->hasAttribute("width") and not $node->getAttribute("width") =~ /\%$/ and $node->getAttribute("width") < $self->{"options"}{"min-img-width"} 0 0 0
Condition0%0/6
$node->hasAttribute("width") and not $node->getAttribute("width") =~ /\%$/ and $node->getAttribute("width") < $self->{"options"}{"min-img-width"}
!ll&&!rl&&r
000
$node->hasAttribute("width") and not $node->getAttribute("width") =~ /\%$/
!ll&&!rl&&r
000
MC/DC0%0/3
$node->hasAttribute("width") and not $node->getAttribute("width") =~ /\%$/ and $node->getAttribute("width") < $self->{"options"}{"min-img-width"}
$node->hasAttribute("width") not $node->getAttribute("width") =~ /\%$/ $node->getAttribute("width") < $self->{"options"}{"min-img-width"}
Truth table0%0/4
A and not(B) and C
A= $node->hasAttribute("width")B= not $node->getAttribute("width") =~ /\%$/C= $node->getAttribute("width") < $self->{"options"}{"min-img-width"}
ABCresult
0XX0
10X0
1100
1111
295
296 return (undef, undef)
297 if $node->hasAttribute('height') &&
298 $node->getAttribute('height') !~ /\%$/ &&
299 0 ▶ $node->getAttribute('height') < $self->{'options'}->{'min-img-height'};
Branch: 1 branch
BranchTrueFalseTotal
if $node->hasAttribute("height") and not $node->getAttribute("height") =~ /\%$/ and $node->getAttribute("height") < $self->{"options"}{"min-img-height"} 0 0 0
Condition0%0/6
$node->hasAttribute("height") and not $node->getAttribute("height") =~ /\%$/ and $node->getAttribute("height") < $self->{"options"}{"min-img-height"}
!ll&&!rl&&r
000
$node->hasAttribute("height") and not $node->getAttribute("height") =~ /\%$/
!ll&&!rl&&r
000
MC/DC0%0/3
$node->hasAttribute("height") and not $node->getAttribute("height") =~ /\%$/ and $node->getAttribute("height") < $self->{"options"}{"min-img-height"}
$node->hasAttribute("height") not $node->getAttribute("height") =~ /\%$/ $node->getAttribute("height") < $self->{"options"}{"min-img-height"}
Truth table0%0/4
A and not(B) and C
A= $node->hasAttribute("height")B= not $node->getAttribute("height") =~ /\%$/C= $node->getAttribute("height") < $self->{"options"}{"min-img-height"}
ABCresult
0XX0
10X0
1100
1111
300
301 # determine complete URL
302 0 ▶ if ($node->hasAttribute('src') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttribute('src') and $node->getAttribute('src') =~ m[^/] || $node->getAttribute('src') =~ m[^(ht|f)tp\://]) { } 0 0 0
Condition0%0/6
$node->hasAttribute("src") and $node->getAttribute("src") =~ m[^/] || $node->getAttribute("src") =~ m[^(ht|f)tp\://]
!ll&&!rl&&r
000
$node->getAttribute("src") =~ m[^/] or $node->getAttribute("src") =~ m[^(ht|f)tp\://]
l!l&&r!l&&!r
000
MC/DC0%0/3
$node->hasAttribute("src") and $node->getAttribute("src") =~ m[^/] || $node->getAttribute("src") =~ m[^(ht|f)tp\://]
$node->hasAttribute("src") $node->getAttribute("src") =~ m[^/] $node->getAttribute("src") =~ m[^(ht|f)tp\://]
Truth table0%0/4
A and B or C
A= $node->hasAttribute("src")B= $node->getAttribute("src") =~ m[^/]C= $node->getAttribute("src") =~ m[^(ht|f)tp\://]
ABCresult
0XX0
1000
11X1
1011
303 ($node->getAttribute('src') =~ /^\// ||
304 $node->getAttribute('src') =~ /^(ht|f)tp\:\/\//)) {
305 # absolute URL
306 0 $url = $node->getAttribute('src');
307 }
308 else {
309 # relative URL
310 0 $url =~ s/[^\/]*$//;
311 0 $url = $url.'/'.$node->getAttribute('src');
312 }
313
314 # determine filetype
315 0 my ($filetype) = $url =~ /^.*\.([a-z0-9A-Z]+)$/;
316 0 $filetype = lc $filetype;
317
318 return (undef, undef)
319 0 ▶ unless $filetype =~ /^(jpe?g|png|gif)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filetype =~ /^(jpe?g|png|gif)$/ 0 0 0
320
321 # download file
322 0 my $data = '';
323 0 ▶ if ($url =~ /^file:\/\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^file://]) { } 0 0 0
324 # local file -> fetch with File::Slurp
325 0 my ($filename) = $url =~ /^file:\/\/(.*)$/;
326 0 $data = read_file($filename);
327 }
328 else {
329 0 my $request = HTTP::Request->new(GET => $url);
330 0 my $response = $self->{'browser'}->request($request);
331 0 $data = $response->content();
332 }
333
334 # write file to img file
335 0 my $filename = $self->{'tmp-dir'}.$self->{'file-basename'}.'-'.scalar(@{$self->{'files'}}).'.'.$filetype;
336 0 my $fh = IO::File->new('> '.$filename);
337 0 print $fh $data;
338  
339 0 push @{$self->{'files'}}, $filename;
340  
341 0 ▶ if (lc $filetype eq 'gif') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $filetype eq "gif") 0 0 0
342 # convert to png
343 0 my $filename_png = $filename;
344 0 $filename_png =~ s/\.gif$/\.png/i;
345 0 system($self->{'command-convert'}, $filename, $filename_png);
346
347 0 push @{$self->{'files'}}, $filename;
348 0 $filename = $filename_png;
349 0 $url =~ s/\.gif$/\.png/i;
350 0 $filetype = 'png';
351 }
352
353 0 my ($width, $height) = imgsize($filename);
354 0 ▶ return (undef, undef) if !defined $width || !defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width and defined $height 0 0 0
Condition0%0/3
defined $width and defined $height
!ll&&!rl&&r
000
MC/DC0%0/2
defined $width and defined $height
defined $width defined $height
Truth table0%0/3
A and B
A= defined $widthB= defined $height
ABresult
0X0
100
111
355 0 ▶ return (undef, undef) if $width <= 0 || $height <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 or $height <= 0 0 0 0
Condition0%0/3
$width <= 0 or $height <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$width <= 0 or $height <= 0
$width <= 0 $height <= 0
Truth table0%0/3
A or B
A= $width <= 0B= $height <= 0
ABresult
1X1
011
000
356 0 ▶ return (undef, undef) if defined $width && $width < $self->{'options'}->{'min-img-width'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $width and $width < $self->{"options"}{"min-img-width"} 0 0 0
Condition0%0/3
defined $width and $width < $self->{"options"}{"min-img-width"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $width and $width < $self->{"options"}{"min-img-width"}
defined $width $width < $self->{"options"}{"min-img-width"}
Truth table0%0/3
A and B
A= defined $widthB= $width < $self->{"options"}{"min-img-width"}
ABresult
0X0
100
111
357 0 ▶ return (undef, undef) if defined $height && $height < $self->{'options'}->{'min-img-height'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $height and $height < $self->{"options"}{"min-img-height"} 0 0 0
Condition0%0/3
defined $height and $height < $self->{"options"}{"min-img-height"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $height and $height < $self->{"options"}{"min-img-height"}
defined $height $height < $self->{"options"}{"min-img-height"}
Truth table0%0/3
A and B
A= defined $heightB= $height < $self->{"options"}{"min-img-height"}
ABresult
0X0
100
111
358 #print "w=$width h=$height w/h=".($width/$height)." h/w=".($height/$width)."\n";
359 0 ▶ if (defined $width && defined $height && $width > $height) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $width and defined $height and $width > $height) { } 0 0 0
Condition0%0/6
defined $width and defined $height and $width > $height
!ll&&!rl&&r
000
defined $width and defined $height
!ll&&!rl&&r
000
MC/DC0%0/3
defined $width and defined $height and $width > $height
defined $width defined $height $width > $height
Truth table0%0/4
A and B and C
A= defined $widthB= defined $heightC= $width > $height
ABCresult
0XX0
10X0
1100
1111
360 return (undef, undef)
361 0 ▶ if $width/$height > $self->{'options'}->{'image-max-length-aspect-ratio'};
Branch: 1 branch
BranchTrueFalseTotal
if $width / $height > $self->{"options"}{"image-max-length-aspect-ratio"} 0 0 0
362 } else {
363 return (undef, undef)
364 0 ▶ if $height/$width > $self->{'options'}->{'image-max-length-aspect-ratio'};
Branch: 1 branch
BranchTrueFalseTotal
if $height / $width > $self->{"options"}{"image-max-length-aspect-ratio"} 0 0 0
365 }
366
367 # convert image to grayscale
368 0 ▶ unless ($self->{'options'}->{'use-color-images'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"options"}{"use-color-images"}) 0 0 0
369 0 my $filename2 = $self->{'tmp-dir'}.$self->{'file-basename'}.'-'.scalar(@{$self->{'files'}}).'_gray.'.$filetype;
370 0 system($self->{'command-convert'}, '-type', 'Grayscale', $filename, $filename2);
371 0 push @{$self->{'files'}}, $filename2;
372 0 $filename = $filename2;
373 }
374
375 0 return ($filename, $url);
376 }
377  
378 sub cleanup_html
379 0 ▶{
Subroutine cleanup_html: not called CC=1 SCAR=6.9
Pod undocumented
380 0 my ($self, $h) = @_;
381 #$h =~ s/\&\#13\;/ /g;
382 0 $h =~ s/\&nbsp\;/ /g;
383 0 return $h;
384 }
385  
386 sub find_abstract
387 0 ▶{
Subroutine find_abstract: not called CC=1 SCAR=6.9
Pod undocumented
388 0 my ($self, $dom) = @_;
389 0 my $abstract = '';
390 # ...
391 #print "($abstract)\n";
392 0 return $abstract;
393 }
394  
395 sub find_author
396 0 ▶{
Subroutine find_author: not called CC=3 SCAR=24.8
Pod undocumented
397 0 my ($self, $dom) = @_;
398 0 my $author = '';
399 0 my $text = $self->node_to_text($dom);
400 0 my $regex = 'by\s*([ a-zA-Z0-9\-]+)';
401 0 ▶ if ($text =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$regex/) 0 0 0
402 0 ($author) = $text =~ /$regex/;
403 }
404 0 ▶ $author = substr($author,0,100) if length($author) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $author > 100 0 0 0
405 0 return $author;
406 }
407  
408 sub find_date
409 0 ▶{
Subroutine find_date: not called CC=3 SCAR=24.8
Pod undocumented
410 0 my ($self, $dom) = @_;
411 0 my $date = ' \today ';
412 0 my $text = $self->node_to_text($dom);
413 0 my $regex = '(\d+\.\d+\.\d+|\d+\/\d+\/\d+)';
414 0 ▶ if ($text =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$regex/) 0 0 0
415 0 ($date) = $text =~ /$regex/;
416 }
417 0 ▶ $date = substr($date,0,20) if length($date) > 20;
Branch: 1 branch
BranchTrueFalseTotal
if length $date > 20 0 0 0
418 #print "($date)\n";
419 0 return $date;
420 }
421  
422 sub find_title
423 0 ▶{
Subroutine find_title: not called CC=2 SCAR=17.9
Pod undocumented
424 0 my ($self, $dom) = @_;
425 0 my $title = 'Untitled';
426 0 ▶ if (scalar @{$dom->getElementsByTagName('title')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$dom->getElementsByTagName("title");}) 0 0 0
427 0 $title = ($dom->getElementsByTagName('title'))->[0]->textContent();
428 0 $title =~ s/[^A-Za-z0-9\.\-\s\t].*$//g;
429 0 $title =~ s/^(.*) - .*$/$1/g;
430 0 $title =~ s/^[\s\t\n\r]*//g;
431 0 $title =~ s/[\s\t\n\r]*$//g;
432 }
433 #print "($title)\n";
434 0 return $title;
435 }
436  
437 sub find_content
438 0 ▶{
Subroutine find_content: not called CC=16 SCAR=56.1
Pod undocumented
439 0 my ($self, $node) = @_;
440 0 $self->{'content'} = undef;
441
442 # [div] > h1:first-child
443 0 foreach my $div (get_child_nodes_by_tagname($node,'div',0)) {
444 #print "[div] >\n";
445 0 ▶ if (scalar get_child_nodes_by_tagname($div,'h1',1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar get_child_nodes_by_tagname($div, "h1", 1)) 0 0 0
446 #print "h1:first-child\n";
447 0 $self->{'content'} = $div;
448 0 last;
449 }
450 }
451 0 ▶ return 1 if defined $self->{'content'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"content"} 0 0 0
452 #exit;
453
454 # [article] > header:first-child > hgroup:first-child > h1:first-child
455 #print "[article] >\n";
456 OUTER:
457 0 foreach my $article (get_child_nodes_by_tagname($node,'article',0)) {
458 #print "article...\n";
459 0 foreach my $header (get_child_nodes_by_tagname($article,'header',1)) {
460 #print "header...\n";
461 0 foreach my $hgroup (get_child_nodes_by_tagname($header,'hgroup',1)) {
462 #print "hgroup...\n";
463 0 ▶ if (scalar get_child_nodes_by_tagname($hgroup,'h1',1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar get_child_nodes_by_tagname($hgroup, "h1", 1)) 0 0 0
464 #print "h1!\n";
465 0 $self->{'content'} = $article;
466 0 last OUTER;
467 }
468 }
469 }
470 }
471 0 ▶ return 1 if defined $self->{'content'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"content"} 0 0 0
472  
473 # [div] > div > h1|h2
474 0 my $xpc = XML::LibXML::XPathContext->new($node);
475 OUTER:
476 0 foreach my $div ($xpc->findnodes('//div', $node)) {
477 0 foreach my $subdiv ($xpc->findnodes('child::div', $div)) {
478 0 ▶ if (scalar $xpc->findnodes('child::h1', $subdiv) ||
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $xpc->findnodes("child::h1", $subdiv) or scalar $xpc->findnodes("child::h2", $subdiv)) 0 0 0
Condition0%0/2
scalar $xpc->findnodes("child::h1", $subdiv) or scalar $xpc->findnodes("child::h2", $subdiv)
l!l
00
MC/DC0%0/1
scalar $xpc->findnodes("child::h1", $subdiv) or scalar $xpc->findnodes("child::h2", $subdiv)
scalar $xpc->findnodes("child::h1", $subdiv)
Truth table0%0/2
A
A= scalar $xpc->findnodes("child::h1", $subdiv)
Aresult
11
00
479 scalar $xpc->findnodes('child::h2', $subdiv)) {
480 0 $self->{'content'} = $div;
481 0 last OUTER;
482 }
483 }
484 }
485 0 ▶ return 1 if defined $self->{'content'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"content"} 0 0 0
486
487 # [body]
488 0 foreach my $body ($xpc->findnodes('//body', $node)) {
489 0 $self->{'content'} = $body;
490 0 last;
491 }
492 0 ▶ return 1 if defined $self->{'content'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"content"} 0 0 0
493
494 0 $self->{'content'} = $node;
495 }
496  
497 sub get_child_nodes_by_tagname
498 0 ▶{
Subroutine get_child_nodes_by_tagname: not called CC=9 SCAR=45.0
Pod undocumented
499 0 my ($node, $tagname, $must_be_first, $level) = @_;
500 0 ▶ return () unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
501 0 ▶ $must_be_first = 0 unless defined $must_be_first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $must_be_first 0 0 0
502 0 ▶ $level = ' ' unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 0 0
503
504 0 my @children;
505 0 ▶ if (lc($node->nodeName) eq lc($tagname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $node->nodeName eq lc $tagname) 0 0 0
506 0 push @children, $node;
507 }
508 0 foreach my $child (@{$node->childNodes()}) {
509 0 ▶ if ($child->nodeType == XML_ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->nodeType == 1) 0 0 0
510 0 ▶ if (lc($child->nodeName) eq lc($tagname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $child->nodeName eq lc $tagname) 0 0 0
511 0 push @children, $child;
512 }
513 0 ▶ if ($must_be_first != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($must_be_first != 0) 0 0 0
514 0 last;
515 }
516 }
517 push @children,
518 0 map { get_child_nodes_by_tagname($_,$tagname,$must_be_first,$level.' ') }
519 0 @{$child->childNodes()};
520 }
521 0 return @children;
522 }
523  
524 sub node_to_text
525 0 ▶{
Subroutine node_to_text: not called CC=1 SCAR=6.9
Pod undocumented
526 0 my ($self, $node) = @_;
527 0 my $s = $node->toString();
528 0 $s =~ s/<\/?[a-zA-Z0-9\-]+[^>]*>//img;
529 0 $s =~ s/[\s\t\n\r]+/ /g;
530 0 return $s;
531 }
532  
533 1 1;
534 __END__
↑ Summary Generator/Exporter/PDF.pm »