Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package Plack::Middleware::MCLess; | ||
|
||
# ABSTRACT: LESS CSS support | ||
|
||
# Code based off of Plack::Middleware::File::Less;; | ||
|
||
use strict; | ||
use warnings; | ||
use 5.008_001; | ||
our $VERSION = '0.02'; | ||
|
||
use parent qw(Plack::Middleware); | ||
use Plack::Util; | ||
use Plack::Util::Accessor qw(root); | ||
use Capture::Tiny ':all'; | ||
use Try::Tiny; | ||
|
||
use IPC::Open3 qw(open3); | ||
use Carp; | ||
|
||
sub prepare_app { | ||
my $self = shift; | ||
|
||
my $less = `lessc -v`; | ||
unless($less) { | ||
Carp::croak("Can't find lessc command"); | ||
} | ||
} | ||
|
||
sub _run_less { | ||
my ($self, $file) = @_; | ||
|
||
my ($stdout, $stderr, $exit) = capture { | ||
system( 'lessc', $file ); | ||
}; | ||
|
||
croak $stderr if $stderr; | ||
return $stdout; | ||
} | ||
|
||
sub less_response { | ||
my ($self, $path_info) = @_; | ||
|
||
my $docroot = $self->root || "."; | ||
my $path = $docroot . $path_info; | ||
|
||
return [404, [], []] unless -f $path; | ||
|
||
try { | ||
my $css = $self->_run_less($path); | ||
return [ | ||
200, | ||
[ | ||
'Content-Type' => 'text/css', | ||
'Content-Length' => length $css, | ||
], | ||
["$css"] | ||
]; | ||
} catch { | ||
return [ | ||
500, | ||
[ | ||
'Content-Type' => 'text/css', | ||
'Content-Length' => length $_, | ||
], | ||
["$_"] | ||
]; | ||
|
||
}; | ||
|
||
} | ||
|
||
|
||
sub call { | ||
my ($self, $env) = @_; | ||
|
||
my $path_info = $env->{PATH_INFO}; | ||
|
||
if( $path_info =~ /\.less$/ ) { | ||
|
||
return $self->less_response($path_info); | ||
|
||
} else { | ||
# carry on | ||
return $self->app->($env); | ||
} | ||
|
||
|
||
# if ($env->{PATH_INFO} =~ s/\.css$/.less/i) { | ||
# my $res = $self->app->($env); | ||
|
||
# return $res unless ref $res eq 'ARRAY'; | ||
|
||
# if ($res->[0] == 200) { | ||
# my $less; | ||
# Plack::Util::foreach($res->[2], sub { $less .= $_[0] }); | ||
|
||
|
||
# my $h = Plack::Util::headers($res->[1]); | ||
# $h->set('Content-Type' => 'text/css'); | ||
# $h->set('Content-Length' => length $css); | ||
|
||
# $res->[2] = [$css]; | ||
# } | ||
# elsif ($res->[0] == 404) { | ||
# $env->{PATH_INFO} = $orig_path_info; | ||
# $res = $self->app->($env); | ||
# } | ||
|
||
# return $res; | ||
# } | ||
# return $self->app->($env); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import a/dodgy/file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@variable_c: #4D926F; | ||
|
||
#header { | ||
color: @variable_c; | ||
} | ||
|
||
h2 { | ||
color: @variable_c; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "foo.less"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use strict; | ||
use Plack::App::File; | ||
use Plack::Middleware::MCLess; | ||
use Test::More; | ||
use Plack::Test; | ||
use HTTP::Request::Common; | ||
|
||
use Data::Dumper; | ||
|
||
my $app = Plack::App::File->new(root => "t/plack/css"); | ||
$app = Plack::Middleware::MCLess->wrap($app, root => "t/plack/css"); | ||
|
||
test_psgi $app, sub { | ||
my $cb = shift; | ||
|
||
my $res = $cb->(GET "/foo.less"); | ||
is $res->code, 200; | ||
is $res->content_type, 'text/css'; | ||
like $res->content, qr/color: #4D926F;/i, "Content match for foo.less"; | ||
|
||
# Something that uses includes | ||
$res = $cb->(GET "/style.less"); | ||
is $res->code, 200; | ||
is $res->content_type, 'text/css'; | ||
like $res->content, qr/\scolor: #4D926F;/i; | ||
|
||
$res = $cb->(GET "/missing.less"); | ||
is $res->code, 404, '404 for a missing less file'; | ||
|
||
$res = $cb->(GET "/broken.less"); | ||
is $res->code, 500, '500 for a broken less file'; | ||
|
||
}; | ||
|
||
done_testing; |