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