blib/lib/Plack/Middleware/File/Less.pm

filter: Statementstmt 84.7% 50 / 59 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 50.0% 2 / 4 totaltotal 81.8% 72 / 88 CC 7 SCAR 9.1
CC
7
Cov
82%
call
5.0
prepare_app
3.3
less_command
2.0
CRAP 2.5
↑ Summary
1 package Plack::Middleware::File::Less;
2  
3 # ABSTRACT: LESS CSS support
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our $VERSION = '0.04';
9  
10 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Plack::Util::Accessor qw(less);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶sub prepare_app {
Subroutine prepare_app: called CC=3 SCAR=12.0
Pod documented
17 1 my $self = shift;
18 1 $self->less(\&less_command);
19 1 my $less = `lessc -v`;
20 1 ▶ if ($less) {
Branch: 2 branches
BranchTrueFalseTotal
if ($less) { } 0 1 1
elsif (eval { do { require CSS::LESSp } }) { } 1 0 1
21 0 $self->less(\&less_command);
22 1 } elsif (eval { require CSS::LESSp }) {
23 1 $self->less(\&less_perl);
24 } else {
25 0 Carp::croak("Can't find lessc command nor CSS::LESSp module");
26 }
27 }
28  
29 0 ▶sub less_command {
Subroutine less_command: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my $less = shift;
31 0 my $pid = open3(my $in, my $out, my $err, "lessc", "-");
32 0 print $in $less;
33 0 close $in;
34  
35 0 my $buf = join '', <$out>;
36 0 waitpid $pid, 0;
37  
38 0 return $buf;
39 }
40  
41 1 ▶sub less_perl {
Subroutine less_perl: called CC=1 SCAR=0.0
Pod undocumented
42 1 return join "", CSS::LESSp->parse(shift);
43 }
44  
45 3 ▶sub call {
Subroutine call: called CC=5 SCAR=16.1
Pod documented
46 3 my ($self, $env) = @_;
47  
48 3 my $orig_path_info = $env->{PATH_INFO};
49 3 ▶ if ($env->{PATH_INFO} =~ s/\.css$/.less/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"PATH_INFO"} =~ s/\.css$/.less/i) 2 1 3
50 2 my $res = $self->app->($env);
51  
52 2 ▶ return $res unless ref $res eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res eq "ARRAY" 0 2 2
53  
54 2 ▶ if ($res->[0] == 200) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->[0] == 200) { } 1 1 2
elsif ($res->[0] == 404) { } 1 0 1
55 1 my $less;
56 1 ▶ Plack::Util::foreach($res->[2], sub { $less .= $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
57  
58 1 my $css = $self->less->($less);
59  
60 1 my $h = Plack::Util::headers($res->[1]);
61 1 $h->set('Content-Type' => 'text/css');
62 1 $h->set('Content-Length' => length $css);
63  
64 1 $res->[2] = [$css];
65 }
66 elsif ($res->[0] == 404) {
67 1 $env->{PATH_INFO} = $orig_path_info;
68 1 $res = $self->app->($env);
69 }
70  
71 2 return $res;
72 }
73 1 return $self->app->($env);
74 }
75  
76 1 1;
77  
78 =head1 NAME
79  
80 Plack::Middleware::File::Less - compile LESS templates into CSS stylesheets
81  
82 =head1 SYNOPSIS
83  
84 use Plack::App::File;
85 use Plack::Builder;
86  
87 builder {
88 mount "/stylesheets" => builder {
89 enable "File::Less";
90 Plack::App::File->new(root => "./stylesheets");
91 };
92 };
93  
94 # Or with Middleware::Static
95 enable "File::Less";
96 enable "Static", path => qr/\.css$/, root => "./static";
97  
98 =head1 DESCRIPTION
99  
100 Plack::Middleware::File::Less is middleware that compiles
101 L<Less|http://lesscss.org> templates into CSS stylesheet..
102  
103 When a request comes in for I<.css> file, this middleware changes the
104 internal path to I<.less> in the same directory. If the LESS template
105 is found, a new CSS stylesheet is built on memory and served to the
106 browsers. Otherwise, it falls back to the original I<.css> file in
107 the directory.
108  
109 =head1 LESS BACKENDS
110  
111 If you have the C<lessc> executable available in your PATH, this
112 module automatically uses the command to convert LESS into CSS. If the
113 command is not available and you have L<CSS::LESSp> perl module
114 available, it will be used. Otherwise you'll get an exception during
115 the initialization of this middleware component.
116  
117 =head1 SEE ALSO
118  
119 L<Plack::App::File> L<CSS::LESSp> L<http://lesscss.org/>
120  
121 =head1 AUTHORS
122  
123 =over 4
124  
125 =item *
126  
127 Naoya Ito <lt>i.naoya@gmail.com<gt>
128  
129 =item *
130  
131 Franck Cuny <lt>https://github.com/franckcuny<gt>
132  
133 =item *
134  
135 Tatsuhiko Miyagawa <lt>miyagawa@bulknews.net<gt>
136  
137 =back
138  
139 =head1 LICENSE
140  
141 This library is free software; you can redistribute it and/or modify it
142 under the same terms as Perl itself.
143  
144 =cut
↑ Summary