Tools/Output/HTML.pm untested

filter: Statementstmt 0.0% 0 / 229 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 53 Podpod 0.0% 0 / 47 totaltotal 0.0% 0 / 406 CC 22 SCAR 15.4
CC
22
Cov
0%
scrub_strip_divs
12.0
expand_uri
12.0
process
6.0
CRAP 4.6
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::Output::HTML;
4 use Moose;
5  
6 use HTML::Element;
7 use Text::Markdown ();
8 use HTML::Entities;
9  
10 use utf8;
11  
12 has body_only => (
13 isa => "Bool",
14 is => "rw",
15 default => 0,
16 );
17  
18 has strip_divs => (
19 isa => "Bool",
20 is => "rw",
21 default => 0,
22 );
23  
24 has lang => (
25 isa => "Str",
26 is => "rw",
27 default => "en",
28 );
29  
30 has summary => (
31 isa => "Mail::Summary::Tools::Summary",
32 is => "rw",
33 required => 1,
34 );
35  
36 has description => (
37 isa => "Str",
38 is => "rw",
39 default => "Mailing list summary",
40 );
41  
42 has generator => (
43 isa => "Str",
44 is => "rw",
45 lazy => 1,
46 default => sub {
47 my $self = shift;
48 require Mail::Summary::Tools;
49 return __PACKAGE__ . " version $Mail::Summary::Tools::VERSION";
50 },
51 );
52  
53 sub process {
54 my $self = shift;
55  
56 my @tree = HTML::Element->new_from_lol( $self->body_only ? $self->body : $self->document_structure );
57  
58 @tree = $self->scrub(@tree);
59  
60 $self->emit(@tree);
61  
62 }
63  
64 sub document_structure {
65 my $self = shift;
66  
67 return (
68 [html => { xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => $self->lang },
69 [head =>
70 [title => $self->summary->title ],
71 [meta => { 'http-equiv' => "Content-Type", content => "text/html; charset=utf-8" }],
72 [meta => { name => "description", content => $self->description }],
73 [meta => { name => "generator", content => $self->generator }],
74 $self->css,
75 ],
76 [body =>
77 $self->toc,
78 $self->body,
79 ],
80
81 ]
82 );
83 }
84  
85 sub scrub {
86 my ( $self, @tree ) = @_;
87  
88 if ( $self->strip_divs ) {
89 @tree = $self->scrub_strip_divs(@tree);
90 }
91  
92 return @tree;
93 }
94  
95 sub scrub_strip_divs {
96 my ( $self, @tree ) = @_;
97  
98 foreach my $subtree ( @tree ) {
99 foreach my $div ( $subtree->find_by_tag_name('div') ) {
100 $div->replace_with_content if defined $div->parent;
101 }
102 }
103  
104 map { ($_->tag eq "div") ? $_->content_list : $_ } @tree;
105 }
106  
107 sub emit {
108 my ( $self, @tree ) = @_;
109 return @tree;
110 }
111  
112 sub template_snippet {
113 my ( $self, $snippet, %vars ) = @_;
114
115 my $out;
116  
117 my $tt = $self->template_obj;
118  
119 $tt->process(
120 \$snippet,
121 {
122 %vars,
123 html => $self,
124 },
125 \$out,
126 ) || warn $tt->error . " in $snippet";
127  
128 return $out;
129 }
130  
131 sub markdown {
132 my ( $self, $text ) = @_;
133  
134 $text =~ s/<((?:msgid|rt):\S+?)>/$self->expand_uri($1)/ge;
135 $text =~ s/\[(.*?)\]\(((?:msgid|rt):\S+?)\)/$self->expand_uri($2, $1)/ge;
136  
137 my $html = Text::Markdown::markdown( $text );
138  
139 # non ascii stuff gets escaped (accents, etc), but not punctuation, which
140 # markdown will handle for us
141 ['~literal' => { text => $self->escape_unicode($html) } ];
142 }
143  
144 sub rt_uri {
145 my ( $self, $rt, $id ) = @_;
146
147 if ( $rt eq "perl" ) {
148 return "http://rt.perl.org/rt3/Public/Bug/Display.html?id=$id";
149 } else {
150 die "unknown rt installation: $rt";
151 }
152 }
153  
154 sub link_to_message {
155 my ( $self, $message_id, $text ) = @_;
156  
157 my $thread = $self->summary->get_thread_by_id( $message_id )
158 || die "The link to <$message_id> could not be resolved, because no thread with that message ID is in the summary data";
159  
160 my $uri;
161
162 if ( $thread->hidden ) {
163 $uri = $thread->archive_link->thread_uri;
164 } else {
165 $uri = URI->new;
166 $uri->fragment($message_id);
167 }
168  
169 $text ||= $thread->subject;
170  
171 "[$text]($uri)";
172 }
173  
174 sub expand_uri {
175 my ( $self, $uri_string, $text ) = @_;
176  
177 my $uri = URI->new($uri_string);
178  
179 if ( $uri->scheme eq 'rt' ) {
180 my ( $rt, $id ) = ( $uri->authority, substr($uri->path, 1) );
181 my $rt_uri = $self->rt_uri($rt, $id);
182 $text ||= "[$rt #$id]";
183 return "[$text]($rt_uri)";
184 } elsif ( $uri->scheme eq 'msgid' ) {
185 return $self->link_to_message( join("", grep { defined } $uri->authority, $uri->path), $text );
186 } else {
187 die "unknown uri scheme: $uri";
188 }
189 }
190  
191 sub escape_unicode {
192 my ( $self, $text ) = @_;
193 $self->escape_html($text, '^\p{IsASCII}');
194 }
195  
196 sub escape_html {
197 my ( $self, $text, @extra ) = @_;
198 HTML::Entities::encode_entities($text, @extra);
199 }
200  
201 sub div {
202 my ( $self, $class_spec, @elems ) = @_;
203  
204 my $class_attr = (ref $class_spec
205 ? join(" ", @$class_spec)
206 : $class_spec );
207  
208 [ div => { class => $class_attr }, @elems ];
209 }
210  
211 has h1_tag => (
212 isa => "ArrayRef",
213 is => "rw",
214 auto_deref => 1,
215 default => sub { ["h1"] },
216 );
217  
218 sub wrap_tags {
219 my ( $self, $tags, @elems ) = @_;
220  
221 if ( @$tags ) {
222 my ( $outer, @inner ) = @$tags;
223 return [ $outer => $self->wrap_tags( \@inner, @elems ) ];
224 } else {
225 return @elems;
226 }
227 }
228  
229 sub h1 {
230 my ( $self, @inner ) = @_;
231 my $tag = $self->h1_tag;
232 $self->wrap_tags( $tag, @inner );
233 }
234  
235 has h2_tag => (
236 isa => "ArrayRef",
237 is => "rw",
238 auto_deref => 1,
239 default => sub { ["h2"] },
240 );
241  
242 sub h2 {
243 my ( $self, @inner ) = @_;
244 my $tag = $self->h2_tag;
245 $self->wrap_tags( $tag, @inner );
246 }
247  
248 has h3_tag => (
249 isa => "ArrayRef",
250 is => "rw",
251 auto_deref => 1,
252 default => sub { ["h3"] },
253 );
254  
255 sub h3 {
256 my ( $self, @inner ) = @_;
257 my $tag = $self->h3_tag;
258 $self->wrap_tags( $tag, @inner );
259 }
260  
261 sub toc {
262 my $self = shift;
263 return ();
264 }
265  
266 sub body {
267 my $self = shift;
268  
269 return [ div => { id => "summary_container" },
270 $self->header,
271 $self->lists,
272 $self->footer,
273 ];
274 }
275  
276 sub header {
277 my $self = shift;
278 my @parts;
279
280 return [ div => { id => "summary_header" },
281 $self->h1( $self->summary->title || "Mailing list summary" ),
282 $self->custom_header,
283 ];
284 }
285  
286 sub custom_header {
287 my $self = shift;
288  
289 if ( my $header = eval { $self->summary->extra->{header} } ) {
290 return ( map { $self->custom_header_section( $_ ) } @$header );
291 } else {
292 return;
293 }
294 }
295  
296 sub custom_header_section {
297 my ( $self, $section ) = @_;
298  
299 return $self->div( header_section => $self->generic_custom_section( $section ) );
300 }
301  
302 sub footer {
303 my $self = shift;
304
305 return [ div => { id => "summary_footer" },
306 $self->custom_footer,
307 $self->see_also,
308 ];
309 }
310  
311 sub custom_footer {
312 my $self = shift;
313  
314 if ( my $footer = eval { $self->summary->extra->{footer} } ) {
315 return ( map { $self->custom_footer_section( $_ ) } @$footer );
316 } else {
317 return;
318 }
319 }
320  
321 sub custom_footer_section {
322 my ( $self, $section ) = @_;
323 return $self->div( footer_section =>
324 $self->generic_custom_section( $section ),
325 );
326 }
327  
328 sub generic_custom_section {
329 my ( $self, $section ) = @_;
330  
331 my $title = $section->{title} || return;
332  
333 my $heading = $self->h2( $title );
334  
335 if ( my $body = $section->{body} ) {
336 return (
337 $heading,
338 $self->markdown( $section->{body} ),
339 );
340 } else {
341 return $heading;
342 }
343 }
344  
345 sub see_also {
346 my $self = shift;
347  
348 if ( my $see_also = eval { $self->summary->extra->{see_also} } ) {
349 return [ div => { id => "see_also", class => "footer_section" },
350 $self->see_also_heading($see_also),
351 $self->see_also_links($see_also),
352 ];
353 } else {
354 return;
355 }
356 }
357  
358 sub see_also_heading {
359 my ( $self, $see_also ) = @_;
360 $self->h2("See Also");
361 }
362  
363 sub see_also_links {
364 my ( $self, $see_also ) = @_;
365 [ ul => map { [ li => $self->see_also_link($_) ] } @$see_also ];
366 }
367  
368 sub see_also_link {
369 my ( $self, $item ) = @_;
370 [a => { href => $item->{uri} }, $item->{name} ];
371 }
372  
373 sub lists {
374 my $self = shift;
375  
376 return $self->div( summary_container_body =>
377 map { $self->list($_) } $self->summary->lists
378 );
379 }
380  
381 sub list {
382 my ( $self, $list ) = @_;
383  
384 ( my $id = $list->name ) =~ s/[^\w]+/_/g;
385
386 my @body = $self->list_body($list);
387  
388 if ( @body ) {
389 return [ div => { id => "summay_list_$id", class => 'summary_list' },
390 $self->list_header($list),
391 @body,
392 $self->list_footer($list),
393 ];
394 } else {
395 return;
396 }
397 }
398  
399 sub list_header {
400 my ( $self, $list ) = @_;
401  
402 return (
403 $self->list_heading($list),
404 $self->list_description($list),
405 );
406 }
407  
408 sub list_heading {
409 my ( $self, $list ) = @_;
410  
411 my $title = $self->list_title($list) || return;
412  
413 $self->h2( $title, $self->list_heading_extra($list) );
414 }
415  
416 sub list_heading_extra {
417 my ( $self, $list ) = @_;
418 # e.g. " (perl6-compiler)"... maybe $list->extra->{remark} || $list->name
419 return;
420 }
421  
422 sub list_title {
423 my ( $self, $list ) = @_;
424  
425 my $title = $list->title || $list->name || return;
426  
427 if ( my $uri = eval { $list->extra->{uri} } ) {
428 return [a => { href => $uri }, $title ],
429 } else {
430 return $title,
431 }
432 }
433  
434 sub list_description {
435 my ( $self, $list ) = @_;
436  
437 if ( my $description = eval { $list->extra->{description} } ) {
438 $self->markdown( $description );
439 } else {
440 return;
441 }
442 }
443  
444 sub list_body {
445 my ( $self, $list ) = @_;
446  
447 ( my $id = $list->name ) =~ s/[^\w]+/_/g;
448  
449 if ( my @threads = map { $self->thread($_) } $list->threads ) {
450 return [ div => { id => "summary_list_body_$id", class => 'summary_list_body' },
451 @threads,
452 ];
453 } else {
454 return;
455 }
456 }
457  
458 sub list_footer {
459 my ( $self, $list ) = @_;
460 return ();
461 }
462  
463 sub thread {
464 my ( $self, $thread ) = @_;
465  
466 return if $thread->hidden;
467  
468 return $self->div( thread_summary =>
469 $self->thread_header($thread),
470 $self->thread_body($thread),
471 $self->thread_footer($thread),
472 );
473 }
474  
475 sub thread_header {
476 my ( $self, $thread ) = @_;
477 $self->h3( $self->thread_link($thread) );
478 }
479  
480 sub thread_link {
481 my ( $self, $thread ) = @_;
482  
483 my $uri = $thread->archive_link->thread_uri;
484  
485 [a => { href => $uri, name=> $thread->message_id }, $thread->subject ],
486 }
487  
488 sub thread_body {
489 my ( $self, $thread ) = @_;
490  
491 if ( my $summary = $thread->summary ) {
492 return $self->div( thread_summary_body => $self->markdown($summary) );
493 } else {
494 return $self->div(
495 [qw/thread_summary_body empty_thread_summary_body/],
496 $self->thread_body_no_summary($thread),
497 );
498 }
499 }
500  
501 sub thread_body_no_summary {
502 my ( $self, $thread ) = @_;
503  
504 my $posters = eval { $thread->extra->{posters} };
505  
506 return (
507 [p => 'No summary provided.' ],
508 ($posters ? $self->thread_posters($posters) : ()),
509 );
510 }
511  
512 sub thread_posters {
513 my ( $self, $posters ) = @_;
514  
515 return (
516 [p => "The following people participated in this thread:" ],
517 [ul => map { [li => ['~literal' => { text => $self->escape_unicode($_->{name} || $_->{email}) } ] ] } @$posters ],
518 );
519 }
520  
521 sub thread_footer {
522 my ( $self, $thread ) = @_;
523 return ();
524 }
525  
526 sub css {
527 my $self = shift;
528 return ();
529 }
530  
531  
532 __PACKAGE__;
533  
534 __END__