Permalink
Browse files

just hacked up

  • Loading branch information...
kentaro committed Apr 18, 2012
1 parent 01e5d4f commit 732bda2252332e4054f787dd78487163b5b4576f
Showing with 571 additions and 6 deletions.
  1. +379 −0 LICENSE
  2. +7 −0 MANIFEST
  3. +24 −0 eg/app.psgi
  4. +13 −0 eg/static/css/main.css
  5. +106 −6 lib/Plack/Middleware/AutoReloadCSS.pm
  6. +41 −0 t/01_insert.t
  7. +1 −0 xt/01_podspell.t
View
379 LICENSE

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,11 +1,18 @@
Build.PL
Changes
+eg/app.psgi
+eg/static/css/main.css
lib/Plack/Middleware/AutoReloadCSS.pm
MANIFEST This list of files
t/00_compile.t
+t/01_insert.t
xt/01_podspell.t
xt/02_perlcritic.t
xt/03_pod.t
xt/04_minimum_version.t
xt/05_cpan_meta.t
xt/06_meta_author.t
+README
+LICENSE
+META.yml
+META.json
View
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+
+use FindBin;
+use Plack::Builder;
+
+my $app = sub {
+ return [
+ 200,
+ [ 'Content-Type' => 'text/html' ],
+ [
+ '<head><link rel="stylesheet" href="/css/main.css" type="text/css" /></head>',
+ '<body>Hello World</body>',
+ ]
+ ];
+};
+
+$app = builder {
+ enable 'Plack::Middleware::Static',
+ path => qr{^/css/},
+ root => "$FindBin::Bin/static";
+ enable 'AutoReloadCSS', interval => 1000;
+ $app;
+};
View
@@ -0,0 +1,13 @@
+body {
+ color : red;
+ background-color: 000;
+}
+
+
+
+
+
+
+
+
+
@@ -1,10 +1,97 @@
package Plack::Middleware::AutoReloadCSS;
+use 5.008;
use strict;
use warnings;
-use 5.008008;
-our $VERSION = '0.01';
+use Plack::Util ();
+use Plack::Util::Accessor qw(interval);
+
+use parent qw(Plack::Middleware);
+our $VERSION = '0.01';
+sub call {
+ my($self, $env) = @_;
+
+ $self->response_cb($self->app->($env), sub {
+ my $res = shift;
+ my $content_type = Plack::Util::header_get($res->[1], 'Content-Type');
+
+ if (!Plack::Util::status_with_no_entity_body($res->[0]) &&
+ (($content_type || '') =~ m{^(?:text/html|application/xhtml\+xml)})) {
+ return sub {
+ my $chunk = shift;
+ return if !defined $chunk;
+
+ $chunk =~ s{</body>}{$self->auto_reload_script . '</body>'}ei;
+ $chunk;
+ }
+ }
+ });
+}
+
+sub auto_reload_script {
+ my $self = shift;
+ my $interval = defined $self->interval ? $self->interval : 1000;
+
+ return <<"SCRIPT";
+<script type="text/javascript">
+
+// CSS auto-reload (c) Nikita Vasilyev
+// http://nv.github.com/css_auto-reload/
+
+CSSStyleSheet.prototype.reload = function reload(){
+ // Reload one stylesheet
+ // usage: document.styleSheets[0].reload()
+ // return: URI of stylesheet if it could be reloaded, overwise undefined
+ if (this.href) {
+ var href = this.href;
+ var i = href.indexOf('?'),
+ last_reload = 'last_reload=' + (new Date).getTime();
+ if (i < 0) {
+ href += '?' + last_reload;
+ } else if (href.indexOf('last_reload=', i) < 0) {
+ href += '&' + last_reload;
+ } else {
+ href = href.replace(/last_reload=\\d+/, last_reload);
+ }
+ return this.ownerNode.href = href;
+ }
+};
+
+StyleSheetList.prototype.reload = function reload(){
+ // Reload all stylesheets
+ // usage: document.styleSheets.reload()
+ for (var i=0; i<this.length; i++) {
+ this[i].reload()
+ }
+};
+
+StyleSheetList.prototype.start_autoreload = function start_autoreload(miliseconds /*Number*/){
+ // usage: document.styleSheets.start_autoreload()
+ if (!start_autoreload.running) {
+ var styles = this;
+ start_autoreload.running = setInterval(function reloading(){
+ styles.reload();
+ }, miliseconds || this.reload_interval);
+ }
+ return start_autoreload.running;
+};
+
+StyleSheetList.prototype.stop_autoreload = function stop_autoreload(){
+ // usage: document.styleSheets.stop_autoreload()
+ clearInterval(this.start_autoreload.running);
+ this.start_autoreload.running = null;
+};
+
+StyleSheetList.prototype.toggle_autoreload = function toggle_autoreload(){
+ // usage: document.styleSheets.toggle_autoreload()
+ return this.start_autoreload.running ? this.stop_autoreload() : this.start_autoreload();
+};
+
+document.styleSheets.start_autoreload($interval);
+</script>
+SCRIPT
+}
1;
__END__
@@ -13,19 +100,32 @@ __END__
=head1 NAME
-Plack::Middleware::AutoReloadCSS - blah blah blah
+Plack::Middleware::AutoReloadCSS - Enables CSS Refreshing without
+Reloading Whole Page
=head1 SYNOPSIS
- use Plack::Middleware::AutoReloadCSS;
+ # in your app.psgi
+ enable 'AutoReloadCSS', interval => 1000;
=head1 DESCRIPTION
-Plack::Middleware::AutoReloadCSS is
+Plack::Middleware::AutoReloadCSS automatically inserts a JavaScript
+snippets to enable CSS refreshing feature without reloading whole
+page.
=head1 AUTHOR
-Kentaro Kuribayashi E<lt>kentarok@gmail.comE<gt>
+=over 4
+
+=item * Kentaro Kuribayashi E<lt>kentarok@gmail.comE<gt>
+
+=item * Nikita Vasilyev
+
+CSS auto-reload is borrowed from
+L<http://nv.github.com/css_auto-reload/>.
+
+=back
=head1 SEE ALSO
View
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use Test::More;
+
+use Plack::Test;
+use Plack::Builder;
+
+use HTTP::Request::Common;
+
+my @content_types = (
+ 'text/html',
+ 'text/html; charset=utf8',
+ 'application/xhtml+xml',
+ 'application/xhtml+xml; charset=utf8',
+);
+
+for my $content_type (@content_types) {
+ note "Content-Type: $content_type";
+
+ my $app = sub {
+ return [
+ 200, [ 'Content-Type' => $content_type ],
+ ['<body>Hello World</body>']
+ ];
+ };
+ $app = builder {
+ enable 'AutoReloadCSS', interval => 999;
+ $app;
+ };
+
+ test_psgi $app, sub {
+ my $cb = shift;
+ my $res = $cb->(GET '/');
+
+ is $res->code, 200, 'response status 200';
+ like $res->content, qr{// CSS auto-reload \(c\) Nikita Vasilyev};
+ like $res->content, qr{document\.styleSheets\.start_autoreload\(999\)};
+ };
+}
+
+done_testing;
View
@@ -62,3 +62,4 @@ Kentaro
Kuribayashi
antipop
GitHub
+Vasilyev

0 comments on commit 732bda2

Please sign in to comment.