[Jifty-commit] r4295 - in jifty/trunk: lib/Jifty lib/Jifty/Plugin lib/Jifty/Plugin/CompressedCSSandJS

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Oct 22 13:10:34 EDT 2007


Author: jesse
Date: Mon Oct 22 13:10:34 2007
New Revision: 4295

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm
   jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
   jifty/trunk/lib/Jifty/Web.pm

Log:
 r68166 at pinglin:  jesse | 2007-10-17 11:45:22 -0400
 
 * working toward onclick => { submit => { action => $action,    
                                 arguments => { foo => 'value', bar => 'other value'} }}
 
 Jifty's js still needs help
 r68365 at pinglin:  jesse | 2007-10-22 13:06:27 -0400
 * Fixes to not cache compressed css+js while in devel mode.
 * Refactoring js compression code into the plugin where it belongs.


Modified: jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS.pm	Mon Oct 22 13:10:34 2007
@@ -4,8 +4,9 @@
 package Jifty::Plugin::CompressedCSSandJS;
 use base 'Jifty::Plugin';
 
-use Digest::MD5 qw(md5_hex);
+use Digest::MD5 'md5_hex';
 use IPC::Run3 'run3';
+use Compress::Zlib ();
 use IO::Handle ();
 
 =head1 NAME
@@ -22,6 +23,8 @@
         css: 1
         jsmin: /path/to/jsmin
         cdn: 'http://yourcdn.for.static.prefix/'
+        gzip: 1
+
 
 =head1 DESCRIPTION
 
@@ -36,9 +39,24 @@
 Note that you will need to use C<ConfigFileVersion> 2 to be able to
 configure jsmin feature.
 
+The gzip configuration directive, which defaults to enabled, instructs
+Jifty to transparently gzip css and js files as they're served if the client
+indicates it supports that feature.
+
 =cut
 
-__PACKAGE__->mk_accessors(qw(css js jsmin cached_javascript cached_javascript_digest cached_javascript_time cdn ));
+__PACKAGE__->mk_accessors(qw(css js jsmin cdn gzip_enabled
+
+    cached_javascript 
+    cached_javascript_gzip
+    cached_javascript_digest 
+    cached_javascript_time 
+
+    cached_css
+    cached_css_gzip
+    cached_css_time
+    cached_css_digest
+));
 
 =head2 init
 
@@ -54,6 +72,7 @@
 
     my %opt  = @_;
     $self->css( $opt{css} );
+    $self->gzip_enabled( exists $opt{gzip} ? $opt{gzip} : 1);
     $self->js( $opt{js} );
     $self->jsmin( $opt{jsmin} );
     $self->cdn( $opt{cdn} || '');
@@ -63,6 +82,12 @@
         callback  => sub { $self->_include_javascript(@_) },
         abortable => 1,
     ) if $self->js_enabled;
+
+    Jifty::Web->add_trigger(
+        name => 'include_css',
+        callback => sub { $self->_include_css(@_) },
+        abortable => 1,
+    ) if $self->css_enabled;
 }
 
 =head2 js_enabled
@@ -87,6 +112,12 @@
     defined $self->css ? $self->css : 1;
 }
 
+=head2 gzip_enabled
+
+Returns whether gzipping is enabled (which it is by default)
+
+=cut
+
 sub _include_javascript {
     my $self = shift;
 
@@ -97,6 +128,50 @@
     return 0;
 }
 
+sub _include_css {
+    my $self = shift;
+    $self->generate_css;
+    Jifty->web->out(
+    qq{<link rel="stylesheet" type="text/css" href="@{[ $self->cdn ]}/__jifty/css/}
+    . $self->cached_css_digest . '.css" />');
+    return 0;
+}
+
+=head3 generate_css 
+
+
+Checks if the compressed CSS is generated, and if it isn't, generates
+and caches it. (In devel mode, it always regenerates it)
+
+=cut
+
+            
+sub generate_css {
+    my $self = shift;
+            
+    if (not defined $self->cached_css_digest or Jifty->config->framework('DevelMode')) {
+        Jifty->log->debug("Generating CSS...");
+        
+        my @roots = map { Jifty::Util->absolute_path( File::Spec->catdir( $_, 'css' ) ) }
+                        Jifty->handler->view('Jifty::View::Static::Handler')->roots;
+    
+        CSS::Squish->roots( @roots );
+        
+        my $css = CSS::Squish->concatenate(
+            map { CSS::Squish->_resolve_file( $_, @roots ) }
+                @{ Jifty->web->css_files }
+        );
+
+        $self->cached_css( $css );
+        $self->cached_css_digest( md5_hex( $css ) );
+        $self->cached_css_time( time );
+        $self->cached_css_gzip(Compress::Zlib::memGzip( $css));
+
+    }
+}
+
+
+
 =head3 _generate_javascript
 
 Checks if the compressed JS is generated, and if it isn't, generates
@@ -139,6 +214,7 @@
         $self->cached_javascript($js);
         $self->cached_javascript_digest( md5_hex($js) );
         $self->cached_javascript_time(time);
+        $self->cached_javascript_gzip(Compress::Zlib::memGzip( $js));
     }
 }
 

Modified: jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm	Mon Oct 22 13:10:34 2007
@@ -13,13 +13,10 @@
 which serve out compiled and compressed CSS and Javascript rules.
 
 =cut
-use Compress::Zlib qw();
 use HTTP::Date ();
 
 use Jifty::Dispatcher -base;
 
-our($GZIP_CSS,$GZIP_JS);
-
 
 on '/__jifty/js/*' => run {
     my $arg = $1;
@@ -50,14 +47,12 @@
     # XXX TODO: If we start caching the squished JS in a file somewhere, we
     # can have the static handler serve it, which would take care of gzipping
     # for us.
-    if ( Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
+    if ( $ccjs->gzip_enabled && Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
         Jifty->log->debug("Sending gzipped squished JS");
         Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
         Jifty->handler->apache->send_http_header();
         binmode STDOUT;
-
-
-        print $GZIP_JS ||= Compress::Zlib::memGzip( $ccjs->cached_javascript );
+        print $ccjs->cached_javascript_gzip;
 
     } else {
         Jifty->log->debug("Sending squished JS");
@@ -69,17 +64,18 @@
 
 on '/__jifty/css/*' => run {
     my $arg = $1;
-    if ( $arg !~ /^[0-9a-f]{32}\.css$/ ) {
+    my ($ccjs) = Jifty->find_plugin('Jifty::Plugin::CompressedCSSandJS');
+    if ( $arg !~ /^[0-9a-f]{32}\.css$/ || !$ccjs) {
 
         # This doesn't look like a real request for squished CSS,
         # so redirect to a more failsafe place
         Jifty->web->redirect( "/static/css/" . $arg );
     }
 
-    Jifty->web->generate_css;
+    $ccjs->generate_css;
 
     if ( Jifty->handler->cgi->http('If-Modified-Since')
-        and $arg eq Jifty->web->cached_css_digest . '.css' )
+        and $arg eq $ccjs->cached_css_digest . '.css' )
     {
         Jifty->log->debug("Returning 304 for cached css");
         Jifty->handler->apache->header_out( Status => 304 );
@@ -92,16 +88,16 @@
     # XXX TODO: If we start caching the squished CSS in a file somewhere, we
     # can have the static handler serve it, which would take care of gzipping
     # for us.
-    if ( Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
+    if ($ccjs->gzip_enabled && Jifty::View::Static::Handler->client_accepts_gzipped_content ) {
         Jifty->log->debug("Sending gzipped squished CSS");
         Jifty->handler->apache->header_out( "Content-Encoding" => "gzip" );
         Jifty->handler->apache->send_http_header();
         binmode STDOUT;
-        print $GZIP_CSS ||= Compress::Zlib::memGzip( Jifty->web->cached_css);
+        print $ccjs->cached_css_gzip;
     } else {
         Jifty->log->debug("Sending squished CSS");
         Jifty->handler->apache->send_http_header();
-        print Jifty->web->cached_css;
+        print $ccjs->cached_css;
     }
     abort;
 };

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Mon Oct 22 13:10:34 2007
@@ -1074,23 +1074,15 @@
 =cut
 
 sub include_css {
-    # XXX: move to CompressCSSandJS plugin
     my $self = shift;
-    my ($ccjs) = Jifty->find_plugin('Jifty::Plugin::CompressedCSSandJS');
-    if ( $ccjs && $ccjs->css_enabled ) {
-        $self->generate_css;
-        $self->out(
-            qq{<link rel="stylesheet" type="text/css" href="@{[ $ccjs->cdn ]}/__jifty/css/}
-            . __PACKAGE__->cached_css_digest . '.css" />'
-        );
-    }
-    else {
-        $self->out(
-            '<link rel="stylesheet" type="text/css" '
-            . 'href="/static/css/main.css" />'
-        );
-    }
-    
+
+    # if there's no trigger, 0 is returned.  if aborted/handled, undef
+    # is returned.
+    defined $self->call_trigger( 'include_css', @_ ) or return '';
+
+    $self->out( '<link rel="stylesheet" type="text/css" '
+            . 'href="/static/css/main.css" />' );
+
     return '';
 }
 
@@ -1108,37 +1100,6 @@
     ]);
 }
 
-=head3 generate_css
-
-Checks if the compressed CSS is generated, and if it isn't, generates
-and caches it.
-
-=cut
-
-sub generate_css {
-    my $self = shift;
-
-    if (not defined __PACKAGE__->cached_css_digest
-            or Jifty->config->framework('DevelMode'))
-    {
-        Jifty->log->debug("Generating CSS...");
-        
-        my @roots = map { Jifty::Util->absolute_path( File::Spec->catdir( $_, 'css' ) ) }
-                        Jifty->handler->view('Jifty::View::Static::Handler')->roots;
-
-        CSS::Squish->roots( @roots );
-        
-        my $css = CSS::Squish->concatenate(
-            map { CSS::Squish->_resolve_file( $_, @roots ) }
-                @{ $self->css_files }
-        );
-
-        __PACKAGE__->cached_css( $css );
-        __PACKAGE__->cached_css_digest( md5_hex( $css ) );
-		__PACKAGE__->cached_css_time( time );
-    }
-}
-
 =head3 include_javascript
 
 Returns a C<< <script> >> tag for the compressed Javascript.
@@ -1202,10 +1163,7 @@
 
 sub add_javascript {
     my $self = shift;
-    Jifty->web->javascript_libs([
-        @{ Jifty->web->javascript_libs },
-        @_
-    ]);
+    Jifty->web->javascript_libs([ @{ Jifty->web->javascript_libs }, @_ ]);
 }
 
 =head3 add_external_javascript URL1, URL2, ...
@@ -1217,9 +1175,7 @@
 sub add_external_javascript {
     my $self = shift;
     Jifty->web->external_javascript_libs([
-        @{ Jifty->web->external_javascript_libs },
-        @_
-    ]);
+        @{ Jifty->web->external_javascript_libs }, @_ ]);
 }
 
 =head2 STATE VARIABLES


More information about the Jifty-commit mailing list