File Coverage

blib/lib/Mail/Message/Convert/Html.pm
Criterion Covered Total %
statement 69 71 97.1
branch 20 34 58.8
condition 3 4 75.0
subroutine 11 11 100.0
pod 5 6 83.3
total 108 126 85.7


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Mail-Message version 4.04.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2001-2026 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 Mail::Message::Convert::Html;{
13             our $VERSION = '4.04';
14             }
15              
16 1     1   1119 use parent 'Mail::Message::Convert';
  1         3  
  1         10  
17              
18 1     1   57 use strict;
  1         3  
  1         26  
19 1     1   5 use warnings;
  1         2  
  1         93  
20              
21 1     1   8 use Log::Report 'mail-message', import => [ qw/__x error/ ];
  1         2  
  1         7  
22              
23             #--------------------
24              
25             sub init($)
26 2     2 0 6 { my ($self, $args) = @_;
27              
28 2         14 $self->SUPER::init($args);
29              
30 2   100     9 my $produce = $args->{produce} || 'HTML';
31              
32             $self->{MMCH_tail}
33 2 50       11 = $produce eq 'HTML' ? '>'
    100          
34             : $produce eq 'XHTML' ? ' />'
35             : error __x"produce XHTML or HTML, not {what UNKNOWN}.", what => $produce;
36              
37 2         7 $self;
38             }
39              
40             #--------------------
41              
42             sub textToHtml(@)
43 52     52 1 110 { my $self = shift;
44              
45 52         115 my @lines = @_; # copy is required
46 52         113 foreach (@lines)
47 52         91 { s/\&/&/gs; s/\
  52         89  
48 52         78 s/\>/>/gs; s/\"/"/gs;
  52         108  
49             }
50 52 50       257 wantarray ? @lines : join('', @lines);
51             }
52              
53              
54             sub fieldToHtml($;$)
55 2     2 1 13 { my ($self, $field, $subject) = @_;
56 2         10 ''. $self->textToHtml($field->wellformedName) . ': ' . $self->fieldContentsToHtml($field, $subject);
57             }
58              
59              
60             sub headToHtmlTable($;$)
61 2     2 1 956 { my ($self, $head) = (shift, shift);
62 2 50       10 my $tp = @_ ? ' '.shift : '';
63              
64 2         5 my $subject;
65 2 50       8 if($self->{MMHC_mailto_subject})
66 0         0 { my $s = $head->get('subject');
67              
68 1     1   634 use Mail::Message::Construct;
  1         3  
  1         1184  
69 0 0       0 $subject = Mail::Message::Construct->replySubject($s) if defined $s;
70             }
71              
72 2         7 my @lines = "\n"; \n), \n);
73 2         12 foreach my $f ($self->selectedFields($head))
74 6         21 { my $name_html = $self->textToHtml($f->wellformedName);
75 6         18 my $cont_html = $self->fieldContentsToHtml($f, $subject);
76              
77 6         27 push @lines,
78             qq(
$name_html:
79             qq( $cont_html
80             }
81              
82 2         7 push @lines, "
\n";
83 2 50       19 wantarray ? @lines : join('',@lines);
84             }
85              
86              
87             sub headToHtmlHead($@)
88 4     4 1 3513 { my ($self, $head) = (shift,shift);
89 4         7 my %meta;
90 4         14 while(@_) { my $k = shift; $meta{lc $k} = shift }
  7         14  
  7         24  
91              
92 4   50     27 my $title = delete $meta{title} || $head->get('subject') || '';
93              
94 4         12 my @lines = (
95             "\n",
96             "".$self->textToHtml($title) . "\n",
97             );
98              
99 4         10 my $author = delete $meta{author};
100 4 50       11 unless(defined $author)
101 4         17 { my $from = $head->get('from');
102 4 50       24 my @addr = defined $from ? $from->addresses : ();
103 4 50       1805 $author = @addr ? $addr[0]->format : undef;
104             }
105              
106 4 50       121 push @lines, '{MMCH_tail}\n"
107             if defined $author;
108              
109 4         15 foreach my $f (map {lc} keys %meta)
  5         13  
110 5 100       14 { next if $meta{$f} eq ''; # empty is skipped.
111             push @lines,
112             ' 113 4         11 . '" content="'. $self->textToHtml($meta{$f})
114             ."\"$self->{MMCH_tail}\n";
115             }
116              
117 4         22 foreach my $f ($self->selectedFields($head))
118 12 100       34 { next if exists $meta{$f->name};
119 10         33 push @lines,
120             ' 121             . '" content="' . $self->textToHtml($f->content)
122             . "\"$self->{MMCH_tail}\n";
123             }
124              
125 4         8 push @lines, "\n";
126 4 50       29 wantarray ? @lines : join('',@lines);
127             }
128              
129              
130             my $atom = qr/[^()<>@,;:\\".\[\]\s[:cntrl:]]+/;
131             my $email_address = qr/(($atom(?:\.$atom)*)\@($atom(?:\.$atom)+))/o;
132              
133             sub fieldContentsToHtml($;$)
134 8     8 1 18 { my ($self, $field, $subj) = @_;
135              
136 8 50       19 my $subject = defined $subj ? '?subject='.$self->textToHtml($subj) : '';
137 8         26 my $body = $self->textToHtml($field->body);
138 8         31 my $comment = $field->comment;
139              
140 8 100       20 $body =~ s#$email_address#$1#gx
141             if $field->name =~ m/^(resent-)?(to|from|cc|bcc|reply\-to)$/;
142              
143 8 50       45 $body . ($comment ? '; '.$self->textToHtml($comment) : '');
144             }
145              
146             1;