File Coverage

blib/lib/App/Smarkmail.pm
Criterion Covered Total %
statement 44 46 95.6
branch 8 14 57.1
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 62 70 88.5


line stmt bran cond sub pod time code
1 1     1   619 use strict;
  1         2  
  1         33  
2 1     1   6 use warnings;
  1         1  
  1         56  
3             package App::Smarkmail;
4             {
5             $App::Smarkmail::VERSION = '0.004';
6             }
7             # ABSTRACT: pipemailer that changes plaintext to multi/alt with Markdown
8              
9 1     1   1032 use Email::MIME;
  1         90700  
  1         31  
10 1     1   13 use Email::MIME::Creator;
  1         2  
  1         23  
11 1     1   7 use Email::MIME::Modifier;
  1         3  
  1         19  
12 1     1   1022 use HTML::Entities ();
  1         7599  
  1         77  
13 1     1   1311 use Text::Markdown;
  1         36175  
  1         569  
14              
15              
16             sub markdown_email {
17 2     2 1 164 my ($self, $msg, $arg) = @_;
18              
19 2 50       6 my $to_send = eval { ref $msg and $msg->isa('Email::MIME') }
  2 50       26  
20             ? $msg
21             : Email::MIME->new($msg);
22              
23 2 100       2238 if ($to_send->content_type =~ m{^text/plain}) {
    50          
24 1         53 my ($text, $html) = $self->_parts_from_text($to_send);
25              
26 1         5 $to_send->content_type_set('multipart/alternative');
27 1         137 $to_send->parts_set([ $text, $html ]);
28             } elsif ($to_send->content_type =~ m{^multipart/(?:related|mixed)}) {
29 1         99 my @parts = $to_send->subparts;
30 1 50       15 if ($parts[0]->content_type =~ m{^text/plain}) {
31 1         40 my ($text, $html) = $self->_parts_from_text(shift @parts);
32              
33 1         7 my $alt = Email::MIME->create(
34             attributes => { content_type => 'multipart/alternative' },
35             parts => [ $text, $html ],
36             );
37              
38 1         1900 $to_send->parts_set([ $alt, @parts ]);
39             }
40             }
41              
42 2         7277 return $to_send;
43             }
44              
45             sub _parts_from_text {
46 2     2   5 my ($self, $email) = @_;
47              
48 2         9 my $text = $email->body;
49 2         108 my ($body, $sig) = split /^-- $/m, $text, 2;
50              
51 2 50       14 if (($sig =~ tr/\n/\n/) > 5) {
52 0         0 $body = $text;
53 0         0 $sig = '';
54             }
55              
56 2         19 my $html = Text::Markdown::markdown($body, { tab_width => 2 });
57              
58 2 50       3823 if ($sig) {
59 2         75 $html .= sprintf "
-- %s
",
60             HTML::Entities::encode_entities($sig);
61             }
62              
63 2         62 my $html_part = Email::MIME->create(
64             attributes => { content_type => 'text/html', },
65             body => $html,
66             );
67              
68 2         6556 my $text_part = Email::MIME->create(
69             attributes => { content_type => 'text/plain', },
70             body => $text,
71             );
72              
73 2         1358 return ($text_part, $html_part);
74             }
75              
76             1;
77              
78             __END__