AdventCalendar/Article.pm untested

filter: Statementstmt 0.0% 0 / 76 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 / 103 CC 4 SCAR 11.6
CC
4
Cov
0%
author_email
6.0
author_name
6.0
_build_body_html
6.0
CRAP 3.2
1 package WWW::AdventCalendar::Article;
2 # ABSTRACT: one article in an advent calendar
3 $WWW::AdventCalendar::Article::VERSION = '1.112';
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.112
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 ATTRIBUTES
191
192 =head2 date
193
194 This is the date (a DateTime object) on which the article is to be published.
195
196 =head2 title
197
198 This is the title of the article.
199
200 =head2 topic
201
202 This is the topic of the article. This attribute is required, for now, but may
203 become optional in the future.
204
205 =head2 author
206
207 This is the author of the article. This attribute is required. It should be
208 given in mailbox format:
209
210 John Smith <jsmith@example.com>
211
212 =head2 body
213
214 This is the body of the document, as a string. It is expected to be Pod.
215
216 =head2 calendar
217
218 This is the WWW::AdventCalendar object in which the article is found.
219
220 =head2 body_html
221
222 This is the body represented as HTML. It is generated as required by a private
223 builder method.
224
225 =head1 METHODS
226
227 =head2 author_email
228
229 This returns the email portion of the author. If none is present, it returns
230 an email-like string unique to the author's name.
231
232 =head2 author_name
233
234 This returns the name portion of the author. If the author value doesn't
235 appear to be a mailbox string, the whole value is returned.
236
237 =head1 AUTHOR
238
239 Ricardo SIGNES <rjbs@cpan.org>
240
241 =head1 COPYRIGHT AND LICENSE
242
243 This software is copyright (c) 2015 by Ricardo SIGNES.
244
245 This is free software; you can redistribute it and/or modify it under
246 the same terms as the Perl 5 programming language system itself.
247
248 =cut