FromMail/Default/Previewers.pm

filter: Statementstmt 29.4% 23 / 78 Branchbran 5.5% 1 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 26.4% 33 / 125 CC 18 SCAR 37.1
CC
18
Cov
22%
previewImage
72.0
previewHtml
56.0
previewText
20.0
CRAP 40.7
« FromMail/Message.pm ↑ Summary FromMail/Default/HTMLifiers.pm »
1 # This code is part of Perl distribution HTML-FromMail version 4.00.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2003-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package HTML::FromMail::Default::Previewers;{
13 1 our $VERSION = '4.00';
14 }
15  
16 1 ▶use base 'HTML::FromMail::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Log::Report 'html-frommail';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use File::Basename qw/basename dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #--------------------
26  
27 1 our @previewers = (
28 'text/plain' => \&previewText,
29 'text/html' => \&previewHtml,
30 'image' => \&previewImage, # added when Image::Magick is installed
31 );
32  
33  
34 sub previewText($$$$$)
35 0 ▶{ my ($page, $message, $part, $attach, $args) = @_;
Subroutine previewText: not called CC=4 SCAR=30.0
Pod documented
36  
37 0 my $decoded = $attach->{decoded}->string;
38 0 for($decoded)
39 0 { s/^\s+//;
40 0 s/\s+/ /gs; # lists of blanks
41 0 s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge;
42 }
43  
44 0 ▶ my $max = $args->{text_max_chars} || 250;
Condition0%0/2
$args->{'text_max_chars'} || 250
l!l
00
MC/DC0%0/1
$args->{'text_max_chars'} || 250
$args->{'text_max_chars'}
Truth table0%0/2
A
A= $args->{'text_max_chars'}
Aresult
11
00
45 0 ▶ substr($decoded, $max) = '' if length $decoded > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $decoded > $max 0 0 0
46  
47 0 +{ %$attach,
48 image => '', # this is not an image
49 html => { text => $decoded },
50 };
51 }
52  
53  
54 sub previewHtml($$$$$)
55 0 ▶{ my ($page, $message, $part, $attach, $args) = @_;
Subroutine previewHtml: not called CC=7 SCAR=40.3
Pod documented
56  
57 0 my $decoded = $attach->{decoded}->string;
58 0 ▶ my $title = $decoded =~ s!\<title\b[^>]*\>(.*?)\</title\>!!i ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$decoded =~ s[\<title\b[^>]*\>(.*?)\</title\>][]i ? : 0 0 0
59 0 for($title)
60 0 { s/\<[^>]*\>//g;
61 0 s/^\s+//;
62 0 s/\s+/ /gs;
63 }
64  
65 0 for($decoded)
66 0 { s!\<\!\-\-.*?\>!!g; # remove comment
67 0 s!\<script.*?script\>!!gsi; # remove script blocks
68 0 s!\<style.*?style\>!!gsi; # remove style-sheets
69 0 s!^.*\<body!<!gi; # remove all before body
70 0 s!\<[^>]*\>!!gs; # remove all tags
71 0 s!\s+! !gs; # unfold lines
72 0 s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge;
73 }
74  
75 0 ▶ my $max = $args->{text_max_chars} || 250;
Condition0%0/2
$args->{'text_max_chars'} || 250
l!l
00
MC/DC0%0/1
$args->{'text_max_chars'} || 250
$args->{'text_max_chars'}
Truth table0%0/2
A
A= $args->{'text_max_chars'}
Aresult
11
00
76 0 ▶ if(length $title)
Branch: 1 branch
BranchTrueFalseTotal
if (length $title) 0 0 0
77 0 { $decoded = "<b>$title</b>, $decoded";
78 0 $max += 7;
79 }
80 0 ▶ substr($decoded, $max) = '' if length $decoded > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $decoded > $max 0 0 0
81  
82 0 +{ %$attach,
83 image => '', # this is not an image
84 html => { text => $decoded },
85 };
86 }
87  
88  
89 BEGIN
90 1 ▶{ eval { require Image::Magick };
Subroutine BEGIN: called CC=2 SCAR=7.4
91 1 ▶ if($@) { warning __x"Image::Magick not installed." }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 0 1
92 0 else { push @previewers, image => \&previewImage }
93 }
94  
95 sub previewImage($$$$$)
96 0 ▶{ my ($page, $message, $part, $attach, $args) = @_;
Subroutine previewImage: not called CC=8 SCAR=42.8
Pod documented
97  
98 0 my $filename = $attach->{filename};
99 0 my $magick = Image::Magick->new;
100 0 my $error = $magick->Read($filename);
101 0 ▶ length $error
Branch: 1 branch
BranchTrueFalseTotal
length $error and &error(&__x("cannot read image from {fn}: {error}", "fn", $filename, "error", $error)) 0 0 0
102 and error __x"cannot read image from {fn}: {error}", fn => $filename, error => $error;
103  
104 0 my %image;
105 0 my ($srcw, $srch) = @image{ qw/width height/ } = $magick->Get( qw/width height/ );
106  
107 0 my $base = basename $filename;
108 0 $base =~ s/\.[^.]+$//;
109  
110 0 my $dirname = dirname $filename;
111  
112 0 ▶ my $reqw = $args->{img_max_width} || 250;
Condition0%0/2
$args->{'img_max_width'} || 250
l!l
00
MC/DC0%0/1
$args->{'img_max_width'} || 250
$args->{'img_max_width'}
Truth table0%0/2
A
A= $args->{'img_max_width'}
Aresult
11
00
113 0 ▶ my $reqh = $args->{img_max_height} || 250;
Condition0%0/2
$args->{'img_max_height'} || 250
l!l
00
MC/DC0%0/1
$args->{'img_max_height'} || 250
$args->{'img_max_height'}
Truth table0%0/2
A
A= $args->{'img_max_height'}
Aresult
11
00
114  
115 0 ▶ if($reqw < $srcw || $reqh < $srch)
Branch: 1 branch
BranchTrueFalseTotal
if ($reqw < $srcw or $reqh < $srch) { } 0 0 0
Condition0%0/3
$reqw < $srcw or $reqh < $srch
l!l&&r!l&&!r
000
MC/DC0%0/2
$reqw < $srcw or $reqh < $srch
$reqw < $srcw $reqh < $srch
Truth table0%0/3
A or B
A= $reqw < $srcwB= $reqh < $srch
ABresult
1X1
011
000
116 { # Size reduction is needed.
117 0 $error = $magick->Resize(width => $reqw, height => $reqh);
118 0 ▶ length $error
Branch: 1 branch
BranchTrueFalseTotal
length $error and &error(&__x("cannot resize image from {fn}: {error}", "fn", $filename, "error", $error)) 0 0 0
119 and error __x"cannot resize image from {fn}: {error}", fn => $filename, error => $error;
120  
121 0 my ($resw, $resh) = @image{ qw/smallwidth smallheight/ } = $magick->Get( qw/width height/ );
122  
123 0 my $outfile = File::Spec->catfile($dirname,"$base-${resw}x${resh}.jpg");
124 0 @image{ qw/smallfile smallurl/ } = ($outfile, basename($outfile));
125  
126 0 $error = $magick->Write($outfile);
127 0 ▶ length $error
Branch: 1 branch
BranchTrueFalseTotal
length $error and &error(&__x("cannot write smaller image from {fn} to {out}: {error}", "in", $filename, "out", $outfile, "error", $error)) 0 0 0
128 and error __x"cannot write smaller image from {fn} to {out}: {error}", in => $filename, out => $outfile, error => $error;
129 }
130 else
131 0 { @image{ qw/smallfile smallurl smallwidth smallheight/ } = ($filename, $attach->{url}, $srcw, $srch);
132 }
133  
134 0 +{ %$attach,
135 image => \%image,
136 html => '', # this is not text
137 };
138 }
139  
140 1 1;
« FromMail/Message.pm ↑ Summary FromMail/Default/HTMLifiers.pm »