[Rt-commit] r5404 - in CSS-Squish: lib/CSS t t/css t/css2

trs at bestpractical.com trs at bestpractical.com
Sun Jun 18 21:29:40 EDT 2006


Author: trs
Date: Sun Jun 18 21:29:38 2006
New Revision: 5404

Added:
   CSS-Squish/t/07-basic-extra-roots.t
   CSS-Squish/t/css/07-basic-extra-roots.css
   CSS-Squish/t/css2/
   CSS-Squish/t/css2/07-basic-extra-roots2.css
Modified:
   CSS-Squish/   (props changed)
   CSS-Squish/lib/CSS/Squish.pm
   CSS-Squish/t/02-edge-cases.t

Log:
 r13177 at zot:  tom | 2006-06-18 21:25:53 -0400
 * Add optional debugging output
 * Add the ability to search multiple roots for imported files
 * Add a basic test of this multi-root feature and fix the edge cases test


Modified: CSS-Squish/lib/CSS/Squish.pm
==============================================================================
--- CSS-Squish/lib/CSS/Squish.pm	(original)
+++ CSS-Squish/lib/CSS/Squish.pm	Sun Jun 18 21:29:38 2006
@@ -3,7 +3,11 @@
 
 package CSS::Squish;
 
-$CSS::Squish::VERSION = '0.03';
+$CSS::Squish::VERSION = '0.04';
+
+# Setting this to true will enable lots of debug logging about what
+# CSS::Squish is doing
+$CSS::Squish::DEBUG   = 0;
 
 use File::Spec;
 
@@ -40,6 +44,8 @@
 # is sort of a problem.
 # 
 
+my @ROOTS = qw( );
+
 my @MEDIA_TYPES = qw(all aural braille embossed handheld print
                      projection screen tty tv);
 my $MEDIA_TYPES = '(?:' . join('|', @MEDIA_TYPES) . ')';
@@ -80,8 +86,12 @@
     my $self   = shift;
     my $string = '';
     
+    $self->_debug("Opening scalar as file");
+    
     open my $fh, '>', \$string or die "Can't open scalar as file! $!";
     $self->concatenate_to($fh, @_);
+
+    $self->_debug("Closing scalar as file");
     close $fh;
 
     return $string;
@@ -90,12 +100,16 @@
 sub concatenate_to {
     my $self = shift;
     my $dest = shift;
+
+    $self->_debug("Looping over list of files: ", join(", ", @_), "\n");
     
     FILE:
     while (my $file = shift @_) {
         my $fh;
         
+        $self->_debug("Opening '$file'");
         if (not open $fh, '<', $file) {
+            $self->_debug("Skipping '$file' due to error");
             print $dest qq[/* WARNING: Unable to open file '$file': $! */\n];
             next FILE;
         }
@@ -106,7 +120,11 @@
                 my $import = $1;
                 my $media  = $2;
 
+                $self->_debug("Processing import '$import'");
+                
                 if ( $import =~ m{^https?://} ) {
+                    $self->_debug("Skipping import because it's a remote URL");
+
                     # Skip remote URLs
                     print $dest $line;
                     next IMPORT;
@@ -114,9 +132,26 @@
 
                 # We need the path relative to where we're importing it from
                 my @spec = File::Spec->splitpath( $file );
-                my $import_path = File::Spec->catpath( @spec[0,1], $import );
+
+                # This first searches for the import relative to the file
+                # it is imported from and then in any user-specified roots
+                my $import_path = $self->_resolve_file(
+                                        $import,
+                                        File::Spec->catpath( @spec[0,1], '' ),
+                                        $self->roots
+                                  );
+
+                if ( not defined $import_path ) {
+                    $self->_debug("Skipping import of '$import'");
+                    
+                    print $dest qq[/* WARNING: Unable to find import '$import' */\n];
+                    print $dest $line;
+                    next IMPORT;
+                }
 
                 if ($import_path eq $file) {
+                    $self->_debug("Skipping import because it's a loop");
+                
                     # We're in a direct loop, don't import this
                     print $dest "/** Skipping: \n", $line, "  */\n\n";
                     next IMPORT;
@@ -140,11 +175,47 @@
                 last IMPORT if not $line =~ /^\s*$/;
             }
         }
+        $self->_debug("Printing the rest of '$file'");
         print $dest $_ while <$fh>;
+
+        $self->_debug("Closing '$file'");
         close $fh;
     }
 }
 
+=head2 B<CSS::Squish-E<gt>roots(@dirs)>
+
+A getter/setter for additional paths to search when looking for imported
+files.  The paths specified here are searched after trying to find the import
+relative to the file from which it is imported.  This is useful if your
+server has multiple document roots from which your CSS imports files.
+
+=cut
+
+sub roots {
+    my $self = shift;
+    @ROOTS = @_ if @_;
+    return @ROOTS;
+}
+
+sub _resolve_file {
+    my $self = shift;
+    my $file = shift;
+
+    for my $root ( @_ ) {
+        my @spec = File::Spec->splitpath( $root, 1 );
+        my $path = File::Spec->catpath( @spec[0,1], $file );
+        
+        return $path if -e $path;
+    }
+    return;
+}
+
+sub _debug {
+    my $self = shift;
+    warn( ( caller(1) )[3], ": ", @_, "\n") if $CSS::Squish::DEBUG;
+}
+
 =head1 BUGS AND SHORTCOMINGS
 
 At the current time, comments are not skipped.  This means comments happening

Modified: CSS-Squish/t/02-edge-cases.t
==============================================================================
--- CSS-Squish/t/02-edge-cases.t	(original)
+++ CSS-Squish/t/02-edge-cases.t	Sun Jun 18 21:29:38 2006
@@ -51,17 +51,8 @@
 
 /** End of foo2.css */
 
-
-/**
-  * From t/css/02-edge-cases.css: @import 'failure.css' print;
-  */
-
- at media print {
-/* WARNING: Unable to open file 't/css/failure.css': No such file or directory */
-}
-
-/** End of failure.css */
-
+/* WARNING: Unable to find import 'failure.css' */
+ at import 'failure.css' print;
 
 fjkls
  jk

Added: CSS-Squish/t/07-basic-extra-roots.t
==============================================================================
--- (empty file)
+++ CSS-Squish/t/07-basic-extra-roots.t	Sun Jun 18 21:29:38 2006
@@ -0,0 +1,29 @@
+#!/usr/bin/perl -w
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+use Test::LongString;
+
+use_ok("CSS::Squish");
+
+my $expected_result = <<'EOT';
+
+
+/**
+  * From t/css/07-basic-extra-roots.css: @import "07-basic-extra-roots2.css";
+  */
+
+foobar
+
+/** End of 07-basic-extra-roots2.css */
+
+blam
+
+EOT
+
+CSS::Squish->roots( 't/css2/' );
+my $result = CSS::Squish->concatenate('t/css/07-basic-extra-roots.css');
+
+is_string($result, $expected_result, "Basic extra roots");
+

Added: CSS-Squish/t/css/07-basic-extra-roots.css
==============================================================================
--- (empty file)
+++ CSS-Squish/t/css/07-basic-extra-roots.css	Sun Jun 18 21:29:38 2006
@@ -0,0 +1,4 @@
+
+ at import "07-basic-extra-roots2.css";
+blam
+

Added: CSS-Squish/t/css2/07-basic-extra-roots2.css
==============================================================================
--- (empty file)
+++ CSS-Squish/t/css2/07-basic-extra-roots2.css	Sun Jun 18 21:29:38 2006
@@ -0,0 +1 @@
+foobar


More information about the Rt-commit mailing list