| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Email::Simple::Markdown; | 
| 2 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:YANICK'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: simple email creation with auto text and html multipart body | 
| 4 |  |  |  |  |  |  | $Email::Simple::Markdown::VERSION = '0.7.3'; | 
| 5 | 5 |  |  | 5 |  | 1129285 | use strict; | 
|  | 5 |  |  |  |  | 38 |  | 
|  | 5 |  |  |  |  | 148 |  | 
| 6 | 5 |  |  | 5 |  | 28 | use warnings; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 117 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 5 |  |  | 5 |  | 25 | use Carp; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 249 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 5 |  |  | 5 |  | 2202 | use Email::Abstract; | 
|  | 5 |  |  |  |  | 142868 |  | 
|  | 5 |  |  |  |  | 191 |  | 
| 11 | 5 |  |  | 5 |  | 2888 | use Email::MIME; | 
|  | 5 |  |  |  |  | 112323 |  | 
|  | 5 |  |  |  |  | 166 |  | 
| 12 | 5 |  |  | 5 |  | 44 | use Email::Simple; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 145 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 5 |  |  | 5 |  | 28 | use List::Util 1.29 qw/ first pairmap /; | 
|  | 5 |  |  |  |  | 98 |  | 
|  | 5 |  |  |  |  | 366 |  | 
| 15 | 5 |  |  | 5 |  | 33 | use Module::Runtime qw/ use_module /; | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 36 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 5 |  |  | 5 |  | 2865 | use Moo; | 
|  | 5 |  |  |  |  | 44804 |  | 
|  | 5 |  |  |  |  | 26 |  | 
| 18 |  |  |  |  |  |  | extends 'Email::Simple'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub BUILDARGS { | 
| 21 | 8 |  |  | 8 | 0 | 11999 | return {}; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | around create => sub { | 
| 25 |  |  |  |  |  |  | my ( $orig, $self, %arg ) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | my @local_args = qw/ css markdown_engine pre_markdown_filter charset /; | 
| 28 |  |  |  |  |  |  | my %md_arg; | 
| 29 |  |  |  |  |  |  | @md_arg{@local_args} = delete @arg{@local_args}; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | my $email = $orig->( $self, %arg ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | $email->markdown_engine_set( | 
| 34 |  |  |  |  |  |  | $md_arg{markdown_engine}||'auto' | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | $email->charset_set( $md_arg{charset} ) if $md_arg{charset}; | 
| 38 |  |  |  |  |  |  | $email->css_set($md_arg{css}) if $md_arg{css}; | 
| 39 |  |  |  |  |  |  | $email->pre_markdown_filter_set($md_arg{pre_markdown_filter}) | 
| 40 |  |  |  |  |  |  | if $md_arg{pre_markdown_filter}; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | return $email; | 
| 43 |  |  |  |  |  |  | }; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | our @SUPPORTED_ENGINES = qw/ Text::MultiMarkdown Text::Markdown /; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | has markdown_engine => ( | 
| 48 |  |  |  |  |  |  | is              => 'rw', | 
| 49 |  |  |  |  |  |  | lazy            => 1, | 
| 50 |  |  |  |  |  |  | default         => sub { $_[0]->find_markdown_engine }, | 
| 51 |  |  |  |  |  |  | clearer         => 1, | 
| 52 |  |  |  |  |  |  | coerce          => sub { | 
| 53 |  |  |  |  |  |  | my( $value ) = @_; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | return $value eq 'auto' | 
| 56 |  |  |  |  |  |  | ? __PACKAGE__->find_markdown_engine | 
| 57 |  |  |  |  |  |  | : $value; | 
| 58 |  |  |  |  |  |  | }, | 
| 59 |  |  |  |  |  |  | trigger => sub{ | 
| 60 |  |  |  |  |  |  | my( $self, $engine ) = @_; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | die "engine '$engine' not implementing a 'markdown' method\n" | 
| 63 |  |  |  |  |  |  | unless use_module($engine)->can('markdown'); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | $self->clear_markdown_object; | 
| 66 |  |  |  |  |  |  | }, | 
| 67 |  |  |  |  |  |  | writer => 'markdown_engine_set', | 
| 68 |  |  |  |  |  |  | ); | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | has markdown_object => ( | 
| 71 |  |  |  |  |  |  | is      => 'rw', | 
| 72 |  |  |  |  |  |  | lazy    => 1, | 
| 73 |  |  |  |  |  |  | clearer => 1, | 
| 74 |  |  |  |  |  |  | default => sub { $_[0]->{markdown_engine}->new }, | 
| 75 |  |  |  |  |  |  | handles => { | 
| 76 |  |  |  |  |  |  | _markdown => 'markdown', | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | ); | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | sub find_markdown_engine { | 
| 81 | 10 |  |  | 10 |  | 27 | first { eval { use_module($_) } } @SUPPORTED_ENGINES | 
|  | 10 |  |  |  |  | 57 |  | 
| 82 | 10 | 50 |  | 10 | 0 | 616 | or die "No supported markdown engine found" | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | has markdown_css => ( | 
| 86 |  |  |  |  |  |  | is => 'rw', | 
| 87 |  |  |  |  |  |  | reader => 'css', | 
| 88 |  |  |  |  |  |  | writer => 'css_set', | 
| 89 |  |  |  |  |  |  | coerce => sub { | 
| 90 |  |  |  |  |  |  | my $css = shift; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | if ( ref $css eq 'ARRAY' ) { | 
| 93 |  |  |  |  |  |  | my @css = @$css; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | croak "number of argument is not even" if @css % 2; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | $css = join "\n", pairmap { "$a { $b }" } @css; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | $css; | 
| 101 |  |  |  |  |  |  | }, | 
| 102 |  |  |  |  |  |  | ); | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | has markdown_filter => ( | 
| 105 |  |  |  |  |  |  | is => 'rw', | 
| 106 |  |  |  |  |  |  | writer => 'pre_markdown_filter_set', | 
| 107 |  |  |  |  |  |  | ); | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | has markdown_charset => ( | 
| 110 |  |  |  |  |  |  | is => 'rw', | 
| 111 |  |  |  |  |  |  | writer => 'charset_set', | 
| 112 |  |  |  |  |  |  | ); | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub with_markdown { | 
| 115 | 11 |  |  | 11 | 1 | 2107 | my $self = shift; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 11 |  |  |  |  | 93 | my $body = $self->body; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 11 |  |  |  |  | 151 | my $mail = Email::Abstract->new($self->SUPER::as_string) | 
| 120 |  |  |  |  |  |  | ->cast('Email::MIME'); | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 11 |  |  |  |  | 9927 | $mail->content_type_set('multipart/alternative'); | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 11 |  |  |  |  | 5997 | $mail->parts_set([ | 
| 125 |  |  |  |  |  |  | Email::MIME->create( | 
| 126 |  |  |  |  |  |  | attributes => { | 
| 127 |  |  |  |  |  |  | content_type => 'text/plain', | 
| 128 |  |  |  |  |  |  | charset      => $self->markdown_charset | 
| 129 |  |  |  |  |  |  | }, | 
| 130 |  |  |  |  |  |  | body => $body, | 
| 131 |  |  |  |  |  |  | ), | 
| 132 |  |  |  |  |  |  | Email::MIME->create( | 
| 133 |  |  |  |  |  |  | attributes => { | 
| 134 |  |  |  |  |  |  | content_type => 'text/html', | 
| 135 |  |  |  |  |  |  | charset      => $self->markdown_charset, | 
| 136 |  |  |  |  |  |  | encoding     => 'quoted-printable', | 
| 137 |  |  |  |  |  |  | }, | 
| 138 |  |  |  |  |  |  | body => $self->markdown_part, | 
| 139 |  |  |  |  |  |  | ) | 
| 140 |  |  |  |  |  |  | ]); | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 11 |  |  |  |  | 51033 | return Email::Abstract->new($mail); | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub markdown_part { | 
| 146 | 11 |  |  | 11 | 0 | 17311 | my $self = shift; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 11 |  |  |  |  | 48 | my $markdown = $self->body; | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 11 | 100 |  |  |  | 158 | if( my $filter =  $self->markdown_filter ) { | 
| 151 | 1 |  |  |  |  | 3 | local $_ = $markdown; | 
| 152 | 1 |  |  |  |  | 4 | $filter->(); | 
| 153 | 1 |  |  |  |  | 7 | $markdown = $_; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 11 |  |  |  |  | 400 | $markdown = $self->_markdown($markdown); | 
| 157 | 11 | 100 |  |  |  | 24418 | $markdown = '' | 
| 160 |  |  |  |  |  |  | . $markdown | 
| 161 |  |  |  |  |  |  | if $self->css; | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 11 |  |  |  |  | 74 | return $markdown; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 6 |  |  | 6 | 1 | 1728 | sub as_string { $_[0]->with_markdown->as_string } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | 1; | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | __END__ |