File Coverage

blib/lib/Plack/Middleware/Bootstrap.pm
Criterion Covered Total %
statement 43 43 100.0
branch 7 8 87.5
condition 1 2 50.0
subroutine 10 10 100.0
pod 1 1 100.0
total 62 64 96.8


line stmt bran cond sub pod time code
1             package Plack::Middleware::Bootstrap;
2 2     2   2786 use 5.008001;
  2         6  
3 2     2   13 use strict;
  2         5  
  2         43  
4 2     2   18 use warnings;
  2         5  
  2         128  
5              
6             our $VERSION = "0.07";
7              
8 2     2   714 use parent qw(Plack::Middleware);
  2         273  
  2         15  
9 2     2   13753 use Plack::Util ();
  2         4  
  2         37  
10 2     2   1366 use Plack::Response;
  2         18742  
  2         56  
11              
12 2     2   1456 use Text::MicroTemplate::DataSection qw();
  2         40441  
  2         82  
13 2     2   2325 use HTML::TreeBuilder::XPath;
  2         141069  
  2         26  
14              
15             sub call {
16 6     6 1 54365 my ($self, $env) = @_;
17              
18             Plack::Util::response_cb(
19             $self->app->($env),
20             sub {
21 6     6   225 my $res = shift;
22              
23 6         43 my $plack_res = Plack::Response->new(@$res);
24 6 100       491 return unless $plack_res->content_type =~ /\Atext\/html/;
25 5 100       162 return if $plack_res->content_encoding;
26              
27 4         65 my $content;
28 4   50     41 Plack::Util::foreach($res->[2] || [], sub { $content .= $_[0] });
  4         41  
29              
30 4         103 my $tree = HTML::TreeBuilder::XPath->new();
31 4         1003 $tree->ignore_unknown(0);
32 4         50 $tree->store_comments(1);
33 4         40 $tree->parse_content($content);
34              
35 4 50       2916 my $head = join "\n", map { ref($_) ? $_->as_HTML(q{&<>'"}, '', {}) : $_ } $tree->findnodes('//head')->[0]->content_list;
  1         709  
36 4 100       3493 my $body = join "\n", map { ref($_) ? $_->as_HTML(q{&<>'"}, '', {}) : $_ } $tree->findnodes('//body')->[0]->content_list;
  6         2780  
37              
38 4         380 my $renderer = Text::MicroTemplate::DataSection->new(
39             escape_func => undef
40             );
41 4         306 $res->[2] = [ $renderer->render_mt('template.mt', $head, $body) ];
42 4         8180 Plack::Util::header_remove($res->[1], 'Content-Length');
43 6         38 });
44             }
45              
46             1;
47             __DATA__