1CSS::Squish(3) User Contributed Perl Documentation CSS::Squish(3)
2
3
4
6 CSS::Squish - Compact many CSS files into one big file
7
9 use CSS::Squish;
10 my $concatenated = CSS::Squish->concatenate(@files);
11
12 my $squisher = CSS::Squish->new( roots => ['/root1', '/root2'] );
13 my $concatenated = $squisher->concatenate(@files);
14
16 This module takes a list of CSS files and concatenates them, making
17 sure to honor any valid @import statements included in the files.
18
19 Following the CSS 2.1 spec, @import statements must be the first rules
20 in a CSS file. Media-specific @import statements will be honored by
21 enclosing the included file in an @media rule. This has the side
22 effect of actually improving compatibility in Internet Explorer, which
23 ignores media-specific @import rules but understands @media rules.
24
25 It is possible that future versions will include methods to compact
26 whitespace and other parts of the CSS itself, but this functionality is
27 not supported at the current time.
28
30 new( [roots=>[...]] )
31
32 A constructor. For backward compatibility with versions prior to 0.06
33 you can still call everything as a class method, but should remember
34 that roots are shared between all callers in this case.
35
36 if you're using persistent environment (like mod_perl) then it's very
37 recomended to use objects.
38
39 concatenate( @files )
40
41 Takes a list of files to concatenate and returns the results as one big
42 scalar.
43
44 concatenate_to( $dest, @files )
45
46 Takes a filehandle to print to and a list of files to concatenate.
47 "concatenate" uses this method with an "open"ed scalar.
48
50 The following methods help map URIs to files and find them on the disk.
51
52 In common situation you control CSS and can adopt it to use imports
53 with relative URIs and most probably only have to set root(s).
54
55 However, you can subclass these methods to parse css files before sub‐
56 mitting, implement advanced mapping of URIs to file system and other
57 things.
58
59 Mapping works in the following way. When you call concatenate method we
60 get content of file using file_handle method which as well lookup files
61 in roots. If roots are not defined then files are treated as absolute
62 paths or relative to the current directory. Using of absolute paths is
63 not recommended as unhide server dirrectory layout to clients in css
64 comments and as well don't allow to handle @import commands with abso‐
65 lute URIs. When files is found we parse its content for @import com‐
66 mands. On each URI we call resolve_uri method that convert absolute and
67 relative URIs into file paths.
68
69 Here is example of processing:
70
71 roots: /www/overlay/, /www/shared/
72
73 $squisher->concatenate('/css/main.css');
74
75 ->file_handle('/css/main.css');
76 ->resolve_file('/css/main.css');
77 <- '/www/shared/css/main.css';
78 <- handle;
79
80 content parsing
81 find '@import url(nav.css)'
82 -> resolve_uri('nav.css', '/css/main.css');
83 <- '/css/nav.css';
84 ... recursivly process file
85 find '@import url(/css/another.css)'
86 -> resolve_uri('/css/another.css', '/css/main.css');
87 <- '/css/another.css'
88 ...
89
90 roots( @dirs )
91
92 A getter/setter for paths to search when looking for files.
93
94 The paths specified here are searched for files. This is useful if your
95 server has multiple document roots or document root doesn't match the
96 current dir.
97
98 See also 'resolve_file' below.
99
100 file_handle( $file )
101
102 Takes a path to a file, resolves (see resolve_file) it and returns a
103 handle.
104
105 Returns undef if file couldn't be resolved or it's impossible to open
106 file.
107
108 You can subclass it to filter content, process it with templating sys‐
109 tem or generate it on the fly:
110
111 package My::CSS::Squish;
112 use base qw(CSS::Squish);
113
114 sub file_handle {
115 my $self = shift;
116 my $file = shift;
117
118 my $content = $self->my_prepare_content($file);
119 return undef unless defined $content;
120
121 open my $fh, "<", \$content or warn "Couldn't open handle: $!";
122 return $fh;
123 }
124
125 Note that the file is not resolved yet and is relative to the root(s),
126 so you have to resolve it yourself or call resolve_file method.
127
128 resolve_file( $file )
129
130 Lookup file in the root(s) and returns first path it found or undef.
131
132 When roots are not set just checks if file exists.
133
134 _resolve_file( $file, @roots )
135
136 DEPRECATED. This private method is deprecated and do nothing useful
137 except maintaining backwards compatibility. If you were using it then
138 most probably to find files in roots before submitting them into con‐
139 catenate method. Now, it's not required and this method returns back
140 file path without changes.
141
142 resolve_uri( $uri_string, $base_file )
143
144 Takes an URI and base file path and transforms it into new file path.
145
147 At the current time, comments are not skipped. This means comments
148 happening before @import statements at the top of a file will cause the
149 @import rules to not be parsed. Make sure the @import rules are the
150 very first thing in the file (and only one per line). Processing of
151 @import rules stops as soon as the first line that doesn't match an
152 @import rule is encountered.
153
154 All other bugs should be reported via <http://rt.cpan.org/Pub‐
155 lic/Dist/Display.html?Name=CSS-Squish> or bug-CSS-Squish@rt.cpan.org.
156
158 Thomas Sibley <trs@bestpractical.com>, Ruslan Zakirov <ruz@bestpracti‐
159 cal.com>
160
162 Copyright (c) 2006.
163
164 This library is free software; you can redistribute it and/or modify it
165 under the same terms as Perl itself, either Perl version 5.8.3 or, at
166 your option, any later version of Perl 5 you may have available.
167
168
169
170perl v5.8.8 2007-11-09 CSS::Squish(3)