File Coverage

blib/lib/Email/MIME/Kit/Assembler/TextifyHTML.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: textify some HTML arguments to assembly
2              
3             use Moose;
4 1     1   181406 extends 'Email::MIME::Kit::Assembler::Standard';
  1         5  
  1         8  
5              
6             #pod =head1 SYNOPSIS
7             #pod
8             #pod In your F<manifest.yaml>:
9             #pod
10             #pod alteratives:
11             #pod - type: text/plain
12             #pod path: body.txt
13             #pod assembler:
14             #pod - TextifyHTML
15             #pod - html_args: [ body ]
16             #pod - type: text/html
17             #pod path: body.html
18             #pod
19             #pod Then:
20             #pod
21             #pod my $email = $kit->assemble({
22             #pod body => '<div><p> ... </p></div>',
23             #pod });
24             #pod
25             #pod The C<body> argument will be rendered intact in the the HTML part, but will
26             #pod converted to plaintext before the plaintext part is rendered.
27             #pod
28             #pod This will be done by
29             #pod L<HTML::FormatText::WithLinks|HTML::FormatText::WithLinks>, using the arguments
30             #pod provided in the C<formatter_args> assembler attribute.
31             #pod
32             #pod =head1 BY THE WAY
33             #pod
34             #pod There will probably exist a TextifyHTML renderer, someday, which will first
35             #pod render the part with the parent part's renderer, and then convert the produced
36             #pod HTML to text. This would allow you to use one template for both HTML and text.
37             #pod
38             #pod =cut
39              
40             use HTML::FormatText::WithLinks;
41 1     1   7639  
  1         48878  
  1         236  
42             has html_args => (
43             is => 'ro',
44             isa => 'ArrayRef',
45             default => sub { [] },
46             );
47              
48             has formatter_args => (
49             is => 'ro',
50             isa => 'HashRef',
51             default => sub {
52             return {
53             before_link => '',
54             after_link => ' [%l]',
55             footnote => '',
56             leftmargin => 0,
57             };
58             },
59             );
60              
61             has formatter => (
62             is => 'ro',
63             isa => 'HTML::FormatText::WithLinks',
64             lazy => 1,
65             init_arg => undef,
66             default => sub {
67             my ($self) = @_;
68             HTML::FormatText::WithLinks->new(
69             %{ $self->formatter_args },
70             );
71             }
72             );
73              
74             around assemble => sub {
75             my ($orig, $self, $arg) = @_;
76             my $local_arg = { %$arg };
77              
78             for my $key (@{ $self->html_args }) {
79             next unless defined $local_arg->{ $key };
80             $local_arg->{ $key } = $self->formatter->parse($local_arg->{ $key });
81             }
82              
83             return $self->$orig($local_arg);
84             };
85              
86             no Moose;
87 1     1   10 1;
  1         4  
  1         10  
88              
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             Email::MIME::Kit::Assembler::TextifyHTML - textify some HTML arguments to assembly
97              
98             =head1 VERSION
99              
100             version 1.004
101              
102             =head1 SYNOPSIS
103              
104             In your F<manifest.yaml>:
105              
106             alteratives:
107             - type: text/plain
108             path: body.txt
109             assembler:
110             - TextifyHTML
111             - html_args: [ body ]
112             - type: text/html
113             path: body.html
114              
115             Then:
116              
117             my $email = $kit->assemble({
118             body => '<div><p> ... </p></div>',
119             });
120              
121             The C<body> argument will be rendered intact in the the HTML part, but will
122             converted to plaintext before the plaintext part is rendered.
123              
124             This will be done by
125             L<HTML::FormatText::WithLinks|HTML::FormatText::WithLinks>, using the arguments
126             provided in the C<formatter_args> assembler attribute.
127              
128             =head1 PERL VERSION
129              
130             This library should run on perls released even a long time ago. It should work
131             on any version of perl released in the last five years.
132              
133             Although it may work on older versions of perl, no guarantee is made that the
134             minimum required version will not be increased. The version may be increased
135             for any reason, and there is no promise that patches will be accepted to lower
136             the minimum required perl.
137              
138             =head1 BY THE WAY
139              
140             There will probably exist a TextifyHTML renderer, someday, which will first
141             render the part with the parent part's renderer, and then convert the produced
142             HTML to text. This would allow you to use one template for both HTML and text.
143              
144             =head1 AUTHOR
145              
146             Ricardo Signes <rjbs@semiotic.systems>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is copyright (c) 2022 by Ricardo Signes.
151              
152             This is free software; you can redistribute it and/or modify it under
153             the same terms as the Perl 5 programming language system itself.
154              
155             =cut