File Coverage

blib/lib/Plack/Middleware/Xslate.pm
Criterion Covered Total %
statement 60 60 100.0
branch 5 10 50.0
condition 2 5 40.0
subroutine 16 16 100.0
pod 1 1 100.0
total 84 92 91.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::Xslate;
2             BEGIN {
3 1     1   1123 $Plack::Middleware::Xslate::AUTHORITY = 'cpan:DOY';
4             }
5             {
6             $Plack::Middleware::Xslate::VERSION = '0.03';
7             }
8 1     1   8 use strict;
  1         3  
  1         35  
9 1     1   6 use warnings;
  1         2  
  1         33  
10             # ABSTRACT: serve static templates with Plack
11              
12 1     1   5 use base 'Plack::Middleware::Static';
  1         2  
  1         937  
13              
14 1     1   11798 use Plack::Util::Accessor 'xslate_args', 'xslate_vars';
  1         4  
  1         5  
15              
16 1     1   1231 use Text::Xslate;
  1         13852  
  1         272  
17              
18              
19             sub prepare_app {
20 1     1 1 104 my $self = shift;
21 1   50     8 $self->{file} = Plack::App::File::Xslate->new({ root => $self->root || '.', encoding => $self->encoding, xslate_args => $self->xslate_args, xslate_vars => $self->xslate_vars });
22 1         181 $self->{file}->prepare_app;
23             }
24              
25             # XXX copied and pasted from Plack::Middleware::Static just so i can override
26             # with Plack::App::File::Xslate instead of Plack::App::File - submit a patch
27             # upstream to make this more configurable
28             sub _handle_static {
29 2     2   41618 my($self, $env) = @_;
30              
31 2 50       20 my $path_match = $self->path or return;
32 2         27 my $path = $env->{PATH_INFO};
33              
34 2         5 for ($path) {
35 2 50       20 my $matched = 'CODE' eq ref $path_match ? $path_match->($_) : $_ =~ $path_match;
36 2 50       12 return unless $matched;
37             }
38              
39 2         8 local $env->{PATH_INFO} = $path; # rewrite PATH
40 2         20 return $self->{file}->call($env);
41             }
42              
43             package # hide from PAUSE
44             Plack::App::File::Xslate;
45 1     1   11 use strict;
  1         2  
  1         40  
46 1     1   6 use warnings;
  1         2  
  1         34  
47              
48 1     1   5 use base 'Plack::App::File';
  1         2  
  1         110  
49              
50 1     1   6 use Plack::Util::Accessor 'xslate_args', 'xslate_vars';
  1         1  
  1         11  
51              
52 1     1   54 use File::Spec;
  1         2  
  1         34  
53 1     1   6 use Cwd 'cwd';
  1         2  
  1         324  
54              
55             sub prepare_app {
56 1     1   2 my $self = shift;
57              
58 1         9 $self->SUPER::prepare_app(@_);
59              
60 1         10 $self->content_type('text/html');
61              
62 1 50       4 $self->xslate_args({
63 1         103 %{ $self->xslate_args || {} },
64             path => [ $self->root ],
65             });
66 1   33     74 $self->{xslate} = Text::Xslate->new($self->xslate_args || ());
67             }
68              
69             sub serve_path {
70 1     1   119 my $self = shift;
71 1         3 my ($env, $file) = @_;
72              
73 1         7 my $res = $self->SUPER::serve_path(@_);
74              
75 1         191 my $filename = $res->[2]->path;
76 1 50       48 if (File::Spec->file_name_is_absolute($filename)) {
77 1         5 $filename = File::Spec->abs2rel($filename, $self->root);
78             }
79              
80 1         145 $res->[2] = [
81             $self->{xslate}->render($filename, $self->xslate_vars)
82             ];
83              
84 1         669 Plack::Util::header_set($res->[1], 'Content-Length', length($res->[2][0]));
85              
86 1         47 return $res;
87             }
88              
89              
90             1;
91              
92             __END__