Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Feb 6, 2010
0 parents commit b473453
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
META.yml
Makefile
inc/
pm_to_blib
*~
2 changes: 2 additions & 0 deletions .shipit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN
git.push_to = origin
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Revision history for Perl extension Plack::Middleware::File::Sass

0.01 Sat Feb 6 01:33:23 2010
- original version
26 changes: 26 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.gitignore
Changes
inc/Module/Install.pm
inc/Module/Install/AuthorTests.pm
inc/Module/Install/Base.pm
inc/Module/Install/Can.pm
inc/Module/Install/Fetch.pm
inc/Module/Install/Include.pm
inc/Module/Install/Makefile.pm
inc/Module/Install/Metadata.pm
inc/Module/Install/ReadmeFromPod.pm
inc/Module/Install/Repository.pm
inc/Module/Install/Win32.pm
inc/Module/Install/WriteAll.pm
inc/Test/Builder/Module.pm
inc/Test/Requires.pm
lib/Plack/Middleware/File/Sass.pm
Makefile.PL
MANIFEST This list of files
META.yml
README
t/00_compile.t
xt/perlcritic.t
xt/pod.t
xt/podspell.t
xt/synopsis.t
14 changes: 14 additions & 0 deletions MANIFEST.SKIP
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
\bRCS\b
\bCVS\b
\.svn/
\.git/
^MANIFEST\.
^Makefile$
~$
\.old$
^blib/
^pm_to_blib
^MakeMaker-\d
\.gz$
\.cvsignore
\.shipit
12 changes: 12 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use inc::Module::Install;
name 'Plack-Middleware-File-Sass';
all_from 'lib/Plack/Middleware/File/Sass.pm';
readme_from 'lib/Plack/Middleware/File/Sass.pm';
build_requires 'Test::More';
test_requires 'Test::Requires';
requires 'Plack::App::File';
requires 'Text::Sass';
auto_include_deps;
author_tests('xt');
auto_set_repository;
WriteAll;
17 changes: 17 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
NAME
Plack::Middleware::File::Sass -

SYNOPSIS
use Plack::Middleware::File::Sass;

DESCRIPTION
Plack::Middleware::File::Sass is

AUTHOR
Tatsuhiko Miyagawa <miyagawa@bulknews.net>

LICENSE
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

SEE ALSO
11 changes: 11 additions & 0 deletions foo.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
!blue = #3bbfce
!margin = 16px

.content_navigation
border-color = !blue
color = !blue - #111

.border
padding = !margin / 2
margin = !margin / 2
border-color = !blue
99 changes: 99 additions & 0 deletions lib/Plack/Middleware/File/Sass.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package Plack::Middleware::File::Sass;

use strict;
use 5.008_001;
our $VERSION = '0.01';

use parent qw(Plack::Middleware);
use Plack::Util::Accessor qw(sass);
use Plack::Util;
use Text::Sass;

sub prepare_app {
my $self = shift;
$self->sass(Text::Sass->new);
}

sub call {
my($self, $env) = @_;

# Depends on how App::File works -- not really comfortable
my $orig_path_info = $env->{PATH_INFO};
if ($env->{PATH_INFO} =~ s/\.css$/.sass/i) {
my $res = $self->app->($env);

return $res unless ref $res eq 'ARRAY';

if ($res->[0] == 200) {
my $sass; Plack::Util::foreach($res->[2], sub { $sass .= $_[0] });
my $css = $self->sass->sass2css($sass);

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;
__END__
=encoding utf-8
=for stopwords
=head1 NAME
Plack::Middleware::File::Sass - Sass support for all Plack frameworks
=head1 SYNOPSIS
use Plack::App::File;
use Plack::Builder;
builder {
mount "/stylesheets" => builder {
enable "File::Sass";
Plack::App::File->new(root => "./stylesheets");
};
};
# Or with Middleware::Static
enable "File::Sass";
enable "Static", path => qr/\.css$/, root => "./static";
=head1 DESCRIPTION
Plack::Middleware::File::Sass is a Plack middleware component that
works with L<Plack::App::File> or L<Plack::Middleware::Static> to
compile Sass templates into CSS stylesheet in every request.
When a request comes in for I<.css> file, this middleware changes the
internal path to I<.sass> in the same directory. If the Sass template
is found, a new CSS stylesheet is built on memory and served to the
browsers. Otherwise, it falls back to the original I<.css> file in
the directory.
=head1 AUTHOR
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<Plack::App::File> L<Text::Sass>
=cut
4 changes: 4 additions & 0 deletions t/00_compile.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use strict;
use Test::More tests => 1;

BEGIN { use_ok 'Plack::Middleware::File::Sass' }
4 changes: 4 additions & 0 deletions t/bar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#data {
float: left;
margin-left: 10px;
}
13 changes: 13 additions & 0 deletions t/foo.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Sass
!blue = #3bbfce
!margin = 16px

.content_navigation
border-color = !blue
color = !blue - #111

.border
padding = !margin / 2
margin = !margin / 2
border-color = !blue
28 changes: 28 additions & 0 deletions t/sass.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use strict;
use Plack::App::File;
use Plack::Middleware::File::Sass;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;

my $app = Plack::App::File->new(root => "t");
$app = Plack::Middleware::File::Sass->wrap($app);

test_psgi $app, sub {
my $cb = shift;

my $res = $cb->(GET "/");
is $res->code, 404;

$res = $cb->(GET "/foo.css");
is $res->code, 200;
is $res->content_type, 'text/css';
like $res->content, qr/border-color: \#3bbfce/;

$res = $cb->(GET "/bar.css");
is $res->code, 200;
is $res->content_type, 'text/css';
like $res->content, qr/float: left/;
};

done_testing;
5 changes: 5 additions & 0 deletions xt/perlcritic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use strict;
use Test::More;
eval q{ use Test::Perl::Critic };
plan skip_all => "Test::Perl::Critic is not installed." if $@;
all_critic_ok("lib");
4 changes: 4 additions & 0 deletions xt/pod.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Test::More;
eval "use Test::Pod 1.00";
plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
all_pod_files_ok();
9 changes: 9 additions & 0 deletions xt/podspell.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use Test::More;
eval q{ use Test::Spelling };
plan skip_all => "Test::Spelling is not installed." if $@;
add_stopwords(<DATA>);
set_spell_cmd("aspell -l en list");
all_pod_files_spelling_ok('lib');
__DATA__
Tatsuhiko
Miyagawa
4 changes: 4 additions & 0 deletions xt/synopsis.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Test::More;
eval "use Test::Synopsis";
plan skip_all => "Test::Synopsis required" if $@;
all_synopsis_ok();

0 comments on commit b473453

Please sign in to comment.