File Coverage

blib/lib/Plack/Middleware/Bootstrap.pm
Criterion Covered Total %
statement 43 43 100.0
branch 5 6 83.3
condition 1 2 50.0
subroutine 10 10 100.0
pod 1 1 100.0
total 60 62 96.7


line stmt bran cond sub pod time code
1             package Plack::Middleware::Bootstrap;
2 2     2   1781 use 5.008001;
  2         6  
  2         64  
3 2     2   7 use strict;
  2         2  
  2         51  
4 2     2   15 use warnings;
  2         2  
  2         92  
5              
6             our $VERSION = "0.06";
7              
8 2     2   412 use parent qw(Plack::Middleware);
  2         254  
  2         13  
9 2     2   10224 use Plack::Util ();
  2         3  
  2         26  
10 2     2   1303 use Plack::Response;
  2         14189  
  2         60  
11              
12 2     2   873 use Text::MicroTemplate::DataSection qw();
  2         27003  
  2         39  
13 2     2   1120 use HTML::TreeBuilder::XPath;
  2         102954  
  2         22  
14              
15             sub call {
16 5     5 1 31933 my ($self, $env) = @_;
17              
18             Plack::Util::response_cb(
19             $self->app->($env),
20             sub {
21 5     5   200 my $res = shift;
22              
23 5         26 my $plack_res = Plack::Response->new(@$res);
24 5 100       302 return unless $plack_res->content_type =~ /\Atext\/html/;
25              
26 4         107 my $content;
27 4   50     26 Plack::Util::foreach($res->[2] || [], sub { $content .= $_[0] });
  4         32  
28              
29 4         41 my $tree = HTML::TreeBuilder::XPath->new();
30 4         845 $tree->ignore_unknown(0);
31 4         43 $tree->store_comments(1);
32 4         26 $tree->parse_content($content);
33              
34 4 50       2506 my $head = join "\n", map { ref($_) ? $_->as_HTML(q{&<>'"}, '', {}) : $_ } $tree->findnodes('//head')->[0]->content_list;
  1         494  
35 4 100       2470 my $body = join "\n", map { ref($_) ? $_->as_HTML(q{&<>'"}, '', {}) : $_ } $tree->findnodes('//body')->[0]->content_list;
  6         2308  
36              
37 4         313 my $renderer = Text::MicroTemplate::DataSection->new(
38             escape_func => undef
39             );
40 4         266 $res->[2] = [ $renderer->render_mt('template.mt', $head, $body) ];
41 4         5894 Plack::Util::header_remove($res->[1], 'Content-Length');
42 5         26 });
43             }
44              
45             1;
46             __DATA__