| 1 | package Email::Simple::Markdown; | ||||||||||||
| 2 | 7 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||
| 3 | # ABSTRACT: simple email creation with auto text and html multipart body | ||||||||||||
| 4 | 7 | $Email::Simple::Markdown::VERSION = '0.7.2'; | |||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 7 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 7 | ▶ | use Email::Abstract; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 7 | ▶ | use Email::MIME; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 7 | ▶ | use Email::Simple; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 7 | ▶ | use List::Util 1.29 qw/ first pairmap /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 7 | ▶ | use Module::Runtime qw/ use_module /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 7 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 7 | extends 'Email::Simple'; | |||||||||||
| 19 | |||||||||||||
| 20 | sub BUILDARGS { | ||||||||||||
| 21 | 8 | ▶ | return {}; | ||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | around create => sub { | ||||||||||||
| 25 | 8 | ▶ | my ( $orig, $self, %arg ) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||
| 26 | |||||||||||||
| 27 | 8 | my @local_args = qw/ css markdown_engine pre_markdown_filter charset /; | |||||||||||
| 28 | 8 | my %md_arg; | |||||||||||
| 29 | 8 | @md_arg{@local_args} = delete @arg{@local_args}; | |||||||||||
| 30 | |||||||||||||
| 31 | 8 | my $email = $orig->( $self, %arg ); | |||||||||||
| 32 | |||||||||||||
| 33 | $email->markdown_engine_set( | ||||||||||||
| 34 | 8 | ▶ | $md_arg{markdown_engine}||'auto' | ||||||||||
|
Condition100%2/2
$md_arg{'markdown_engine'} || 'auto'
MC/DC100%1/1
$md_arg{'markdown_engine'} || 'auto'
$md_arg{'markdown_engine'}
Truth table100%2/2
A
A= $md_arg{'markdown_engine'}
| |||||||||||||
| 35 | ); | ||||||||||||
| 36 | |||||||||||||
| 37 | 8 | ▶ | $email->charset_set( $md_arg{charset} ) if $md_arg{charset}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 8 | ▶ | $email->css_set($md_arg{css}) if $md_arg{css}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | $email->pre_markdown_filter_set($md_arg{pre_markdown_filter}) | ||||||||||||
| 40 | 8 | ▶ | if $md_arg{pre_markdown_filter}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | |||||||||||||
| 42 | 8 | return $email; | |||||||||||
| 43 | 7 | }; | |||||||||||
| 44 | |||||||||||||
| 45 | 7 | our @SUPPORTED_ENGINES = qw/ Text::MultiMarkdown Text::Markdown /; | |||||||||||
| 46 | |||||||||||||
| 47 | has markdown_engine => ( | ||||||||||||
| 48 | is => 'rw', | ||||||||||||
| 49 | lazy => 1, | ||||||||||||
| 50 | 0 | ▶ | default => sub { $_[0]->find_markdown_engine }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 51 | clearer => 1, | ||||||||||||
| 52 | coerce => sub { | ||||||||||||
| 53 | 8 | ▶ | my( $value ) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 54 | |||||||||||||
| 55 | 8 | ▶ | return $value eq 'auto' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | ? __PACKAGE__->find_markdown_engine | ||||||||||||
| 57 | : $value; | ||||||||||||
| 58 | }, | ||||||||||||
| 59 | trigger => sub{ | ||||||||||||
| 60 | 8 | ▶ | my( $self, $engine ) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||
| 61 | |||||||||||||
| 62 | 8 | ▶ | die "engine '$engine' not implementing a 'markdown' method\n" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | unless use_module($engine)->can('markdown'); | ||||||||||||
| 64 | |||||||||||||
| 65 | 8 | $self->clear_markdown_object; | |||||||||||
| 66 | }, | ||||||||||||
| 67 | 7 | writer => 'markdown_engine_set', | |||||||||||
| 68 | ); | ||||||||||||
| 69 | |||||||||||||
| 70 | has markdown_object => ( | ||||||||||||
| 71 | is => 'rw', | ||||||||||||
| 72 | lazy => 1, | ||||||||||||
| 73 | clearer => 1, | ||||||||||||
| 74 | 8 | ▶ | default => sub { $_[0]->{markdown_engine}->new }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 75 | 7 | handles => { | |||||||||||
| 76 | _markdown => 'markdown', | ||||||||||||
| 77 | } | ||||||||||||
| 78 | ); | ||||||||||||
| 79 | |||||||||||||
| 80 | sub find_markdown_engine { | ||||||||||||
| 81 | 10 | ▶ | first { eval { use_module($_) } } @SUPPORTED_ENGINES | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 10 | ▶ | or die "No supported markdown engine found" | ||||||||||
|
Subroutine
find_markdown_engine: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | has markdown_css => ( | ||||||||||||
| 86 | is => 'rw', | ||||||||||||
| 87 | reader => 'css', | ||||||||||||
| 88 | writer => 'css_set', | ||||||||||||
| 89 | coerce => sub { | ||||||||||||
| 90 | 2 | ▶ | my $css = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 91 | |||||||||||||
| 92 | 2 | ▶ | if ( ref $css eq 'ARRAY' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 1 | my @css = @$css; | |||||||||||
| 94 | |||||||||||||
| 95 | 1 | ▶ | croak "number of argument is not even" if @css % 2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | |||||||||||||
| 97 | 1 | ▶ | $css = join "\n", pairmap { "$a { $b }" } @css; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 2 | $css; | |||||||||||
| 101 | }, | ||||||||||||
| 102 | 7 | ); | |||||||||||
| 103 | |||||||||||||
| 104 | 7 | has markdown_filter => ( | |||||||||||
| 105 | is => 'rw', | ||||||||||||
| 106 | writer => 'pre_markdown_filter_set', | ||||||||||||
| 107 | ); | ||||||||||||
| 108 | |||||||||||||
| 109 | 7 | has markdown_charset => ( | |||||||||||
| 110 | is => 'rw', | ||||||||||||
| 111 | writer => 'charset_set', | ||||||||||||
| 112 | ); | ||||||||||||
| 113 | |||||||||||||
| 114 | sub with_markdown { | ||||||||||||
| 115 | 11 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
with_markdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 116 | |||||||||||||
| 117 | 11 | my $body = $self->body; | |||||||||||
| 118 | |||||||||||||
| 119 | 11 | my $mail = Email::Abstract->new($self->SUPER::as_string) | |||||||||||
| 120 | ->cast('Email::MIME'); | ||||||||||||
| 121 | |||||||||||||
| 122 | 11 | $mail->content_type_set('multipart/alternative'); | |||||||||||
| 123 | |||||||||||||
| 124 | 11 | $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 | return Email::Abstract->new($mail); | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | sub markdown_part { | ||||||||||||
| 146 | 11 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
markdown_part: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 147 | |||||||||||||
| 148 | 11 | my $markdown = $self->body; | |||||||||||
| 149 | |||||||||||||
| 150 | 11 | ▶ | if( my $filter = $self->markdown_filter ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 151 | 1 | local $_ = $markdown; | |||||||||||
| 152 | 1 | $filter->(); | |||||||||||
| 153 | 1 | $markdown = $_; | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 11 | $markdown = $self->_markdown($markdown); | |||||||||||
| 157 | 11 | ▶ | $markdown = '<style type="text/css">' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | . $self->css | ||||||||||||
| 159 | . '</style>' | ||||||||||||
| 160 | . $markdown | ||||||||||||
| 161 | if $self->css; | ||||||||||||
| 162 | |||||||||||||
| 163 | 11 | return $markdown; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | 6 | ▶ | sub as_string { $_[0]->with_markdown->as_string } | ||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 167 | |||||||||||||
| 168 | 7 | 1; | |||||||||||
| 169 | |||||||||||||
| 170 | __END__ | ||||||||||||