CSS-Squish
view release on metacpan or search on metacpan
lib/CSS/Squish.pm view on Meta::CPAN
print $dest "/** Skipping: \n", $line, " */\n\n";
next;
}
# Look up the new file in root(s), so we can leave import
# if something is wrong
my $new_fh = $self->file_handle( $path );
unless ( defined $new_fh ) {
$self->_debug("Skipping import of '$import'");
print $dest qq[/* WARNING: Unable to find import '$import' */\n];
print $dest $line;
next;
}
print $dest "\n/**\n * From $file: $line */\n\n";
if ( defined $media ) {
print $dest "\@media $media {\n";
$self->_concatenate_to($dest, $new_fh, $path, $seen);
print $dest "}\n";
}
else {
$self->_concatenate_to($dest, $new_fh, $path, $seen);
}
print $dest "\n/** End of $import */\n\n";
}
else {
print $dest $line;
last if not $line =~ /^\s*$/;
}
}
$self->_debug("Printing the rest");
local $_;
print $dest $_ while <$fh>;
close $fh;
}
=head1 RESOLVING METHODS
The following methods help map URIs to files and find them on the disk.
In common situation you control CSS and can adopt it to use imports with
relative URIs and most probably only have to set root(s).
However, you can subclass these methods to parse css files before submitting,
implement advanced mapping of URIs to file system and other things.
Mapping works in the following way. When you call concatenate method we get
content of file using file_handle method which as well lookup files in roots.
If roots are not defined then files are treated as absolute paths or relative
to the current directory. Using of absolute paths is not recommended as
unhide server dirrectory layout to clients in css comments and as well don't
allow to handle @import commands with absolute URIs. When files is found we
parse its content for @import commands. On each URI we call resolve_uri method
that convert absolute and relative URIs into file paths.
Here is example of processing:
roots: /www/overlay/, /www/shared/
$squisher->concatenate('/css/main.css');
->file_handle('/css/main.css');
->resolve_file('/css/main.css');
<- '/www/shared/css/main.css';
<- handle;
content parsing
find '@import url(nav.css)'
-> resolve_uri('nav.css', '/css/main.css');
<- '/css/nav.css';
... recursivly process file
find '@import url(/css/another.css)'
-> resolve_uri('/css/another.css', '/css/main.css');
<- '/css/another.css'
...
=head2 roots( @dirs )
A getter/setter for paths to search when looking for files.
The paths specified here are searched for files. This is useful if
your server has multiple document roots or document root doesn't match
the current dir.
See also 'resolve_file' below.
=cut
sub roots {
my $self = shift;
my @res;
unless ( blessed $self ) {
@ROOTS = @_ if @_;
@res = @ROOTS;
} else {
$self->{'roots'} = [ grep defined, @_ ] if @_;
@res = @{ $self->{'roots'} };
}
$self->_debug("Roots are: ". join ", ", map "'$_'", @res);
return @res;
}
=head2 file_handle( $file )
Takes a path to a file, resolves (see resolve_file) it and returns a handle.
Returns undef if file couldn't be resolved or it's impossible to open file.
You can subclass it to filter content, process it with templating system or
generate it on the fly:
package My::CSS::Squish;
use base qw(CSS::Squish);
sub file_handle {
my $self = shift;
my $file = shift;
( run in 1.154 second using v1.01-cache-2.11-cpan-df04353d9ac )