| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Plack::Middleware::File::Sass; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 128387 | use strict; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 181 |  | 
| 4 | 2 |  |  | 2 |  | 66 | use 5.008_001; | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 2 |  |  |  |  | 148 |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 14 | use parent qw(Plack::Middleware); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 177 |  | 
| 8 | 2 |  |  | 2 |  | 3842 | use Plack::Util::Accessor qw(sass syntax); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 9 | 2 |  |  | 2 |  | 97 | use Plack::Util; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 46 |  | 
| 10 | 2 |  |  | 2 |  | 2929 | use IPC::Open3 qw(open3); | 
|  | 2 |  |  |  |  | 9123 |  | 
|  | 2 |  |  |  |  | 272 |  | 
| 11 | 2 |  |  | 2 |  | 20 | use Carp (); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 2185 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | my $text_sass; | 
| 14 |  |  |  |  |  |  | my %valid = (sass => 1, scss => 1); | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub prepare_app { | 
| 17 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 0 | 0 |  |  |  |  | $self->syntax("sass") unless defined $self->syntax; | 
| 20 | 0 | 0 |  |  |  |  | $valid{$self->syntax} or Carp::croak("Unsupported syntax: ", $self->syntax); | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  |  | my $sass = `sass -v`; | 
| 23 | 0 | 0 | 0 |  |  |  | if ($sass && $sass =~ /Sass 3/) { | 
|  |  | 0 |  |  |  |  |  | 
| 24 | 0 |  |  |  |  |  | $self->sass(\&sass_command); | 
| 25 | 0 |  |  |  |  |  | } elsif (eval { require Text::Sass }) { | 
| 26 | 0 |  |  |  |  |  | $self->sass(\&sass_perl); | 
| 27 |  |  |  |  |  |  | } else { | 
| 28 | 0 |  |  |  |  |  | Carp::croak("Can't find sass gem nor Text::Sass module"); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub sass_command { | 
| 33 | 0 |  |  | 0 | 0 |  | my($syntax, $body) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 0 | 0 |  |  |  |  | my $pid = open3(my $in, my $out, my $err, | 
| 36 |  |  |  |  |  |  | "sass", "--stdin", ($syntax eq 'scss' ? '--scss' : ())); | 
| 37 | 0 |  |  |  |  |  | print $in $body; | 
| 38 | 0 |  |  |  |  |  | close $in; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 |  |  |  |  |  | my $buf = join '', <$out>; | 
| 41 | 0 |  |  |  |  |  | waitpid $pid, 0; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  |  | return $buf; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub sass_perl { | 
| 47 | 0 |  |  | 0 | 0 |  | my($syntax, $body) = @_; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  |  |  |  | my $method = "${syntax}2css"; | 
| 50 | 0 |  | 0 |  |  |  | $text_sass ||= Text::Sass->new; | 
| 51 | 0 |  |  |  |  |  | $text_sass->$method($body); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub call { | 
| 55 | 0 |  |  | 0 | 1 |  | my($self, $env) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | my $syntax = $self->syntax; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # Sort of depends on how App::File works | 
| 60 | 0 |  |  |  |  |  | my $orig_path_info = $env->{PATH_INFO}; | 
| 61 | 0 | 0 |  |  |  |  | if ($env->{PATH_INFO} =~ s/\.css$/.$syntax/i) { | 
| 62 | 0 |  |  |  |  |  | my $res = $self->app->($env); | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 | 0 |  |  |  |  | return $res unless ref $res eq 'ARRAY'; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 | 0 |  |  |  |  | if ($res->[0] == 200) { | 
|  |  | 0 |  |  |  |  |  | 
| 67 | 0 |  |  | 0 |  |  | my $sass; Plack::Util::foreach($res->[2], sub { $sass .= $_[0] }); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 68 | 0 |  |  |  |  |  | my $css = $self->sass->($syntax, $sass); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | my $h = Plack::Util::headers($res->[1]); | 
| 71 | 0 |  |  |  |  |  | $h->set('Content-Type' => 'text/css'); | 
| 72 | 0 |  |  |  |  |  | $h->set('Content-Length' => length $css); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 |  |  |  |  |  | $res->[2] = [ $css ]; | 
| 75 |  |  |  |  |  |  | } elsif ($res->[0] == 404) { | 
| 76 | 0 |  |  |  |  |  | $env->{PATH_INFO} = $orig_path_info; | 
| 77 | 0 |  |  |  |  |  | $res = $self->app->($env); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  |  |  |  | return $res; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 |  |  |  |  |  | return $self->app->($env); | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | 1; | 
| 87 |  |  |  |  |  |  | __END__ |