AdventCalendar/Article.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 102 SCAR 30.0
CC
4
Cov
0%
author_email
6.0
author_name
6.0
_build_body_html
6.0
CRAP 20.0
1 package WWW::AdventCalendar::Article 1.113;
2 # ABSTRACT: one article in an advent calendar
3  
4 use Moose;
5  
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod Objects of this class represent a single article in a L<WWW::AdventCalendar>.
9 #pod They have a very limited set of attributes. The primary task of this class is
10 #pod the production of an HTML version of the article's body.
11 #pod
12 #pod =cut
13  
14 use autodie;
15 use Digest::MD5 qw(md5_hex);
16 use Email::Address;
17 use Pod::Elemental;
18 use Pod::Elemental::Transformer::Pod5;
19 use Pod::Elemental::Transformer::SynMux;
20 use Pod::Elemental::Transformer::Codebox;
21 use Pod::Elemental::Transformer::PPIHTML;
22 use Pod::Elemental::Transformer::VimHTML;
23 use Pod::Elemental::Transformer::List;
24 use Pod::Simple::XHTML 3.13;
25  
26 use namespace::autoclean;
27  
28 #pod =attr date
29 #pod
30 #pod This is the date (a DateTime object) on which the article is to be published.
31 #pod
32 #pod =attr title
33 #pod
34 #pod This is the title of the article.
35 #pod
36 #pod =attr topic
37 #pod
38 #pod This is the topic of the article. This attribute is required, for now, but may
39 #pod become optional in the future.
40 #pod
41 #pod =attr author
42 #pod
43 #pod This is the author of the article. This attribute is required. It should be
44 #pod given in mailbox format:
45 #pod
46 #pod John Smith <jsmith@example.com>
47 #pod
48 #pod =attr body
49 #pod
50 #pod This is the body of the document, as a string. It is expected to be Pod.
51 #pod
52 #pod =cut
53  
54 has date => (is => 'ro', isa => 'DateTime', required => 1);
55 has [ qw(author title topic body) ] => (
56   is => 'ro',
57   isa => 'Str',
58   required => 1,
59 );
60  
61 #pod =method author_email
62 #pod
63 #pod This returns the email portion of the author. If none is present, it returns
64 #pod an email-like string unique to the author's name.
65 #pod
66 #pod =cut
67  
68 sub author_email {
69   my ($self) = @_;
70   my ($addr) = Email::Address->parse($self->author);
71   return($addr
72         ? $addr->address
73         : md5_hex($self->author) . q{@advcal.example.com});
74 }
75  
76 #pod =method author_name
77 #pod
78 #pod This returns the name portion of the author. If the author value doesn't
79 #pod appear to be a mailbox string, the whole value is returned.
80 #pod
81 #pod =cut
82  
83 sub author_name {
84   my ($self) = @_;
85   my ($addr) = Email::Address->parse($self->author);
86   return($addr ? $addr->name : $self->author);
87 }
88  
89  
90 #pod =attr calendar
91 #pod
92 #pod This is the WWW::AdventCalendar object in which the article is found.
93 #pod
94 #pod =cut
95  
96 has calendar => (
97   is => 'ro',
98   isa => 'WWW::AdventCalendar',
99   required => 1,
100   weak_ref => 1,
101 );
102  
103 #pod =attr body_html
104 #pod
105 #pod This is the body represented as HTML. It is generated as required by a private
106 #pod builder method.
107 #pod
108 #pod =cut
109  
110 has body_html => (
111   is => 'ro',
112   lazy => 1,
113   init_arg => undef,
114   builder => '_build_body_html',
115 );
116  
117 sub _build_body_html {
118   my ($self) = @_;
119  
120   my $body = $self->body;
121   $body = Encode::encode('utf-8', $body);
122  
123   $body = "\n=encoding utf-8\n\n$body" unless $body =~ /^=encoding/s;
124  
125   my $document = Pod::Elemental->read_string($body);
126  
127   Pod::Elemental::Transformer::Pod5->new->transform_node($document);
128   Pod::Elemental::Transformer::List->new->transform_node($document);
129  
130   my $mux = Pod::Elemental::Transformer::SynMux->new({
131     transformers => [
132       Pod::Elemental::Transformer::Codebox->new,
133       Pod::Elemental::Transformer::PPIHTML->new,
134       Pod::Elemental::Transformer::VimHTML->new,
135     ],
136   });
137  
138   $mux->transform_node($document);
139  
140   $body = $document->as_pod_string;
141  
142   my $parser = Pod::Simple::XHTML->new;
143   $parser->perldoc_url_prefix('https://metacpan.org/module/');
144   $parser->output_string(\my $html);
145   $parser->html_h_level(2);
146   $parser->html_header('');
147   $parser->html_footer('');
148  
149   $parser->parse_string_document( Encode::encode('utf-8', $body) );
150  
151   $html = "<div class='pod'>$html</div>";
152  
153   $html =~ s{
154 \s*(<pre>)\s*
155 (<table\sclass='code-listing'>.+?
156 \s*</table>)\s*(?:<!--\shack\s-->)?\s*(</pre>)\s*
157 }{my $str = $2; $str =~ s/\G^\s\s[^\$]*$//gm; $str}gesmx;
158  
159   return $html;
160 }
161  
162 sub atom_id {
163   my ($self) = @_;
164  
165   return $self->calendar->uri . $self->date->ymd . '.html';
166 }
167  
168 1;
169  
170 __END__
171
172 =pod
173
174 =encoding UTF-8
175
176 =head1 NAME
177
178 WWW::AdventCalendar::Article - one article in an advent calendar
179
180 =head1 VERSION
181
182 version 1.113
183
184 =head1 DESCRIPTION
185
186 Objects of this class represent a single article in a L<WWW::AdventCalendar>.
187 They have a very limited set of attributes. The primary task of this class is
188 the production of an HTML version of the article's body.
189
190 =head1 PERL VERSION
191
192 This module should work on any version of perl still receiving updates from
193 the Perl 5 Porters. This means it should work on any version of perl released
194 in the last two to three years. (That is, if the most recently released
195 version is v5.40, then this module should work on both v5.40 and v5.38.)
196
197 Although it may work on older versions of perl, no guarantee is made that the
198 minimum required version will not be increased. The version may be increased
199 for any reason, and there is no promise that patches will be accepted to lower
200 the minimum required perl.
201
202 =head1 ATTRIBUTES
203
204 =head2 date
205
206 This is the date (a DateTime object) on which the article is to be published.
207
208 =head2 title
209
210 This is the title of the article.
211
212 =head2 topic
213
214 This is the topic of the article. This attribute is required, for now, but may
215 become optional in the future.
216
217 =head2 author
218
219 This is the author of the article. This attribute is required. It should be
220 given in mailbox format:
221
222 John Smith <jsmith@example.com>
223
224 =head2 body
225
226 This is the body of the document, as a string. It is expected to be Pod.
227
228 =head2 calendar
229
230 This is the WWW::AdventCalendar object in which the article is found.
231
232 =head2 body_html
233
234 This is the body represented as HTML. It is generated as required by a private
235 builder method.
236
237 =head1 METHODS
238
239 =head2 author_email
240
241 This returns the email portion of the author. If none is present, it returns
242 an email-like string unique to the author's name.
243
244 =head2 author_name
245
246 This returns the name portion of the author. If the author value doesn't
247 appear to be a mailbox string, the whole value is returned.
248
249 =head1 AUTHOR
250
251 Ricardo SIGNES <cpan@semiotic.systems>
252
253 =head1 COPYRIGHT AND LICENSE
254
255 This software is copyright (c) 2022 by Ricardo SIGNES.
256
257 This is free software; you can redistribute it and/or modify it under
258 the same terms as the Perl 5 programming language system itself.
259
260 =cut