| 1 | package File::Assets; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | File::Assets - Manage .css and .js assets for a web page or application | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.064_1 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.064_1'; | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use File::Assets | ||
| 21 | |||
| 22 | my $assets = File::Assets->new( base => [ $uri_root, $dir_root ] ) | ||
| 23 | |||
| 24 | # Put minified files in $dir_root/built/... (the trailing slash is important) | ||
| 25 | $assets->set_output_path("built/") | ||
| 26 | |||
| 27 | # File::Assets will automatically detect the type based on the extension | ||
| 28 | $assets->include("/static/style.css") | ||
| 29 | |||
| 30 | # You can also include external assets: | ||
| 31 | $assets->include("http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"); | ||
| 32 | |||
| 33 | # This asset won't get included twice, as File::Assets will ignore repeats of a path | ||
| 34 | $assets->include("/static/style.css") | ||
| 35 | |||
| 36 | # And finally ... | ||
| 37 | $assets->export | ||
| 38 | |||
| 39 | # Or you can iterate (in order) | ||
| 40 | for my $asset ($assets->exports) { | ||
| 41 | |||
| 42 | print $asset->uri, "\n"; | ||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | In your .tt (Template Toolkit) files: | ||
| 47 | |||
| 48 | [% WRAPPER page.tt %] | ||
| 49 | |||
| 50 | [% assets.include("/static/special-style.css", 100) %] # The "100" is the rank, which makes sure it is exported after other assets | ||
| 51 | |||
| 52 | [% asset = BLOCK %] | ||
| 53 | <style media="print"> | ||
| 54 | body { font: serif; } | ||
| 55 | </style> | ||
| 56 | [% END %] | ||
| 57 | [% assets.include(asset) %] # This will include the css into an inline asset with the media type of "print" | ||
| 58 | |||
| 59 | # ... finally, in your "main" template: | ||
| 60 | |||
| 61 | [% CLEAR -%] | ||
| 62 | <html> | ||
| 63 | |||
| 64 | <head> | ||
| 65 | [% assets.export("css") %] | ||
| 66 | </head> | ||
| 67 | |||
| 68 | <body> | ||
| 69 | |||
| 70 | [% content %] | ||
| 71 | |||
| 72 | <!-- Generally, you want to include your JavaScript assets at the bottom of your html --> | ||
| 73 | |||
| 74 | [% assets.export("js") %] | ||
| 75 | |||
| 76 | </body> | ||
| 77 | |||
| 78 | </html> | ||
| 79 | |||
| 80 | Use the minify option to perform minification before export | ||
| 81 | |||
| 82 | my $assets = File::Assets->new( minify => 1, ... ) | ||
| 83 | |||
| 84 | =head1 DESCRIPTION | ||
| 85 | |||
| 86 | File::Assets is a tool for managing JavaScript and CSS assets in a (web) application. It allows you to "publish" assests in one place after having specified them in different parts of the application (e.g. throughout request and template processing phases). | ||
| 87 | |||
| 88 | This package has the added bonus of assisting with minification and filtering of assets. Support is built-in for YUI Compressor (L<http://developer.yahoo.com/yui/compressor/>), L<JavaScript::Minifier>, L<CSS::Minifier>, L<JavaScript::Minifier::XS>, and L<CSS::Minifier::XS>. | ||
| 89 | |||
| 90 | File::Assets was built with L<Catalyst> in mind, although this package is framework agnostic. Look at L<Catalyst::Plugin::Assets> for an easy way to integrate File::Assets with Catalyst. | ||
| 91 | |||
| 92 | =head1 USAGE | ||
| 93 | |||
| 94 | =head2 Cascading style sheets and their media types | ||
| 95 | |||
| 96 | A cascading style sheet can be one of many different media types. For more information, look here: L<http://www.w3.org/TR/REC-CSS2/media.html> | ||
| 97 | |||
| 98 | This can cause a problem when minifying, since, for example, you can't bundle a media type of screen with a media type of print. File::Assets handles this situation by treating .css files of different media types separately. | ||
| 99 | |||
| 100 | To control the media type of a text/css asset, you can do the following: | ||
| 101 | |||
| 102 | $assets->include("/path/to/printstyle.css", ..., { media => "print" }); # The asset will be exported with the print-media indicator | ||
| 103 | |||
| 104 | $assets->include_content($content, "text/css", ..., { media => "screen" }); # Ditto, but for the screen type | ||
| 105 | |||
| 106 | =head2 Including assets in the middle of processing a Template Toolkit template | ||
| 107 | |||
| 108 | Sometimes, in the middle of a TT template, you want to include a new asset. Usually you would do something like this: | ||
| 109 | |||
| 110 | [% assets.include("/include/style.css") %] | ||
| 111 | |||
| 112 | But then this will show up in your output, because ->include returns an object: | ||
| 113 | |||
| 114 | File::Assets::Asset=HASH(0x99047e4) | ||
| 115 | |||
| 116 | The way around this is to use the TT "CALL" directive, as in the following: | ||
| 117 | |||
| 118 | [% CALL assets.include("/include/style.css") %] | ||
| 119 | |||
| 120 | =head2 Avoid minifying assets on every request (if you minify) | ||
| 121 | |||
| 122 | By default, File::Assets will avoid re-minifying assets if nothing in the files have changed. However, in a web application, this can be a problem if you serve up two web pages that have different assets. That's because File::Assets will detect different assets being served in page A versus assets being served in page B (think AJAX interface vs. plain HTML with some CSS). The way around this problem is to name your assets object with a unique name per assets bundle. By default, the name is "assets", but can be changed with $assets->name(<a new name>): | ||
| 123 | |||
| 124 | my $assets = File::Assets->new(...); | ||
| 125 | $assets->name("standard"); | ||
| 126 | |||
| 127 | You can change the name of the assets at anytime before exporting. | ||
| 128 | |||
| 129 | =head2 YUI Compressor 2.2.5 is required | ||
| 130 | |||
| 131 | If you want to use the YUI Compressor, you should have version 2.2.5 or above. | ||
| 132 | |||
| 133 | YUI Compressor 2.1.1 (and below) will *NOT WORK* | ||
| 134 | |||
| 135 | To use the compressor for minification specify the path to the .jar like so: | ||
| 136 | |||
| 137 | my $assets = File::Assets->new( minify => "/path/to/yuicompressor.jar", ... ) | ||
| 138 | |||
| 139 | =head2 Specifying an C<output_path> pattern | ||
| 140 | |||
| 141 | When aggregating or minifying assets, you need to put the result in a new file. | ||
| 142 | |||
| 143 | You can use the following directives when crafting a path/filename pattern: | ||
| 144 | |||
| 145 | %n The name of the asset, "assets" by default | ||
| 146 | %e The extension of the asset (e.g. css, js) | ||
| 147 | %f The fingerprint of the asset collection (a hexadecimal digest of the concatenated digest of each asset in the collection) | ||
| 148 | %k The kind of the asset (e.g. css-screen, css, css-print, js) | ||
| 149 | %h The kind head-part of the asset (e.g. css, js) | ||
| 150 | %l The kind tail-part of the asset (e.g. screen, print) (essentially the media type of a .css asset) | ||
| 151 | |||
| 152 | In addition, in each of the above, a ".", "/" or "-" can be placed in between the "%" and directive character. | ||
| 153 | This will result in a ".", "/", or "-" being prepended to the directive value. | ||
| 154 | |||
| 155 | The default pattern is: | ||
| 156 | |||
| 157 | %n%-l%-f.%e | ||
| 158 | |||
| 159 | A pattern of C<%n%-l.%e> can result in the following: | ||
| 160 | |||
| 161 | assets.css # name of "assets", no media type, an asset type of CSS (.css) | ||
| 162 | assets-screen.css # name of "assets", media type of "screen", an asset type of CSS (.css) | ||
| 163 | assets.js # name of "assets", an asset type of JavaScript (.js) | ||
| 164 | |||
| 165 | If the pattern ends with a "/", then the default pattern will be appended | ||
| 166 | |||
| 167 | xyzzy/ => xyzzy/%n%-l-%f.%e | ||
| 168 | |||
| 169 | If the pattern does not have an extension-like ending, then "%.e" will be appended | ||
| 170 | |||
| 171 | xyzzy => xyzzy.%e | ||
| 172 | |||
| 173 | =head2 Strange output or "sticky" content | ||
| 174 | |||
| 175 | File::Assets uses built-in caching to share content across different objects (via File::Assets::Cache). If you're having problems | ||
| 176 | try disabling the cache by passing "cache => 0" to File::Assets->new | ||
| 177 | |||
| 178 | =head1 METHODS | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | # If the pattern does NOT begin with a "/", then the base dir will be prepended | ||
| 183 | |||
| 184 | use strict; | ||
| 185 | use warnings; | ||
| 186 | |||
| 187 | use Object::Tiny qw/cache registry _registry_hash rsc filter_scheme output_path_scheme output_asset_scheme/; | ||
| 188 | use File::Assets::Carp; | ||
| 189 | |||
| 190 | use Tie::LLHash; | ||
| 191 | use Path::Resource; | ||
| 192 | use Scalar::Util qw/blessed refaddr/; | ||
| 193 | use HTML::Declare qw/LINK SCRIPT STYLE/; | ||
| 194 | use File::Copy(); | ||
| 195 | |||
| 196 | use File::Assets::Asset; | ||
| 197 | use File::Assets::Cache; | ||
| 198 | use File::Assets::Kind; | ||
| 199 | use File::Assets::Bucket; | ||
| 200 | |||
| 201 | =head2 File::Assets->new( base => <base>, output_path => <output_path>, minify => <minify> ) | ||
| 202 | |||
| 203 | Create and return a new File::Assets object. | ||
| 204 | |||
| 205 | You can configure the object with the following: | ||
| 206 | |||
| 207 | base # A hash reference with a "uri" key/value and a "dir" key/value. | ||
| 208 | For example: { uri => http://example.com/assets, dir => /var/www/htdocs/assets } | ||
| 209 | |||
| 210 | # A URI::ToDisk object | ||
| 211 | |||
| 212 | # A Path::Resource object | ||
| 213 | |||
| 214 | minify # "1" or "best" - Will either use JavaScript::Minifier::XS> & CSS::Minifier::XS or | ||
| 215 | JavaScript::Minifier> & CSS::Minifier (depending on availability) | ||
| 216 | for minification | ||
| 217 | |||
| 218 | # "0" or "" or undef - Don't do any minfication (this is the default) | ||
| 219 | |||
| 220 | # "./path/to/yuicompressor.jar" - Will use YUI Compressor via the given .jar for minification | ||
| 221 | |||
| 222 | # "minifier" - Will use JavaScript::Minifier & CSS::Minifier for minification | ||
| 223 | |||
| 224 | # "xs" or "minifier-xs" - Will use JavaScript::Minifier::XS & CSS::Minifier::XS for minification | ||
| 225 | |||
| 226 | output_path # Designates the output path for minified .css and .js assets | ||
| 227 | The default output path pattern is "%n%-l%-d.%e" (rooted at the dir of <base>) | ||
| 228 | See above in "Specifying an output_path pattern" for details | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub new { | ||
| 233 | my $self = bless {}, shift; | ||
| 234 | local %_ = @_; | ||
| 235 | |||
| 236 | $self->set_base($_{rsc} || $_{base_rsc} || $_{base}); | ||
| 237 | $self->set_base_uri($_{uri} || $_{base_uri}) if $_{uri} || $_{base_uri}; | ||
| 238 | $self->set_base_dir($_{dir} || $_{base_dir}) if $_{dir} || $_{base_dir}; | ||
| 239 | $self->set_base_path($_{base_path}) if $_{base_path}; | ||
| 240 | |||
| 241 | $self->set_output_path($_{output_path} || $_{output_path_scheme} || []); | ||
| 242 | |||
| 243 | $self->name($_{name}); | ||
| 244 | |||
| 245 | $_{cache} = 1 unless exists $_{cache}; | ||
| 246 | $self->set_cache($_{cache}) if $_{cache}; | ||
| 247 | |||
| 248 | # my $rsc = File::Assets::Util->parse_rsc($_{rsc} || $_{base_rsc} || $_{base}); | ||
| 249 | # $rsc->uri($_{uri} || $_{base_uri}) if $_{uri} || $_{base_uri}; | ||
| 250 | # $rsc->dir($_{dir} || $_{base_dir}) if $_{dir} || $_{base_dir}; | ||
| 251 | # $rsc->path($_{base_path}) if $_{base_path}; | ||
| 252 | # $self->{rsc} = $rsc; | ||
| 253 | |||
| 254 | my %registry; | ||
| 255 | $self->{registry} = tie(%registry, qw/Tie::LLHash/, { lazy => 1 }); | ||
| 256 | $self->{_registry_hash} = \%registry; | ||
| 257 | |||
| 258 | $self->{filter_scheme} = {}; | ||
| 259 | my $filter_scheme = $_{filter} || $_{filters} || $_{filter_scheme} || []; | ||
| 260 | for my $rule (@$filter_scheme) { | ||
| 261 | $self->filter(@$rule); | ||
| 262 | } | ||
| 263 | |||
| 264 | if (my $minify = $_{minify}) { | ||
| 265 | if ($minify eq 1 || $minify =~ m/^\s*(?:minifier-)?best\s*$/i) { $self->filter("minifier-best") } | ||
| 266 | elsif ($minify =~ m/^\s*yui-?compressor:/) { $self->filter($minify) } | ||
| 267 | elsif ($minify =~ m/\.jar/i) { $self->filter("yuicompressor:$minify") } | ||
| 268 | elsif ($minify =~ m/^\s*(?:minifier-)?xs\s*$/i) { $self->filter("minifier-xs") } | ||
| 269 | elsif ($minify =~ m/^\s*minifier\s*$/i) { $self->filter("minifier") } | ||
| 270 | elsif ($minify =~ m/^\s*concat\s*$/i) { $self->filter("concat") } | ||
| 271 | else { croak "Don't understand minify option ($minify)" } | ||
| 272 | } | ||
| 273 | |||
| 274 | return $self; | ||
| 275 | } | ||
| 276 | |||
| 277 | =head2 $asset = $assets->include(<path>, [ <rank>, <type>, { ... } ]) | ||
| 278 | |||
| 279 | =head2 $asset = $assets->include_path(<path>, [ <rank>, <type>, { ... } ]) | ||
| 280 | |||
| 281 | First, if <path> is a scalar reference or "looks like" some HTML (starts with a angle bracket, e.g.: <script></script>), then | ||
| 282 | it will be treated as inline content. | ||
| 283 | |||
| 284 | Otherwise, this will include an asset located at "<base.dir>/<path>" for processing. The asset will be exported as "<base.uri>/<path>" | ||
| 285 | |||
| 286 | Optionally, you can specify a rank, where a lower number (i.e. -2, -100) causes the asset to appear earlier in the exports | ||
| 287 | list, and a higher number (i.e. 6, 39) causes the asset to appear later in the exports list. By default, all assets start out | ||
| 288 | with a neutral rank of 0. | ||
| 289 | |||
| 290 | Also, optionally, you can specify a type override as the third argument. | ||
| 291 | |||
| 292 | By default, the newly created $asset is NOT inline. | ||
| 293 | |||
| 294 | Returns the newly created asset. | ||
| 295 | |||
| 296 | NOTE: See below for how the extra hash on the end is handled | ||
| 297 | |||
| 298 | =head2 $asset = $assets->include({ ... }) | ||
| 299 | |||
| 300 | Another way to invoke include is by passing in a hash reference. | ||
| 301 | |||
| 302 | The hash reference should contain the follwing information: | ||
| 303 | |||
| 304 | path # The path to the asset file, relative to base | ||
| 305 | content # The content of the asset | ||
| 306 | |||
| 307 | type # Optional if a path is given, required for content | ||
| 308 | rank # Optional, 0 by default (Less than zero is earlier, greater than zero is later) | ||
| 309 | inline # Optional, by default true if content was given, false is a path was given | ||
| 310 | base # Optional, by default the base of $assets | ||
| 311 | |||
| 312 | You can also pass extra information through the hash. Any extra information will be bundled in the ->attributes hash of $asset. | ||
| 313 | For example, you can control the media type of a text/css asset by doing something like: | ||
| 314 | |||
| 315 | $assets->include("/path/to/printstyle.css", ..., { media => "print" }) # The asset will be exported with the print-media indicator | ||
| 316 | |||
| 317 | NOTE: The order of <rank> and <type> doesn't really matter, since we can detect whether something looks like a rank (number) or | ||
| 318 | not, and correct for it (and it does). | ||
| 319 | |||
| 320 | =cut | ||
| 321 | |||
| 322 | sub include_path { | ||
| 323 | my $self = shift; | ||
| 324 | return $self->include(@_); | ||
| 325 | } | ||
| 326 | |||
| 327 | my $rankish = qr/^[\-\+]?[\.\d]+$/; # A regular expression for a string that looks like a rank | ||
| 328 | sub _correct_for_proper_rank_and_type_order ($) { | ||
| 329 | my $asset = shift; | ||
| 330 | if (defined $asset->{type} && $asset->{type} =~ $rankish || | ||
| 331 | defined $asset->{rank} && $asset->{rank} !~ $rankish) { | ||
| 332 | # Looks like someone entered a rank as the type or vice versa, so we'll switch them | ||
| 333 | my $rank = delete $asset->{type}; | ||
| 334 | my $type = delete $asset->{rank}; | ||
| 335 | $asset->{type} = $type if defined $type; | ||
| 336 | $asset->{rank} = $rank if defined $rank; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | sub include { | ||
| 341 | my $self = shift; | ||
| 342 | |||
| 343 | my (@asset, $path); | ||
| 344 | if (ref $_[0] ne "HASH") { | ||
| 345 | $path = shift; | ||
| 346 | croak "Don't have a path to include" unless defined $path && length $path; | ||
| 347 | if (ref $path eq "SCALAR" || $path =~ m/^\s*</) { | ||
| 348 | push @asset, content => $path; | ||
| 349 | } | ||
| 350 | else { | ||
| 351 | return $self->fetch($path) if $self->exists($path); | ||
| 352 | push @asset, path => $path; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | for (qw/rank type/) { | ||
| 357 | last if ! @_ || ref $_[0] eq "HASH"; | ||
| 358 | push @asset, $_ => shift; | ||
| 359 | } | ||
| 360 | push @asset, %{ $_[0] } if @_ && ref $_[0] eq "HASH"; | ||
| 361 | my %asset = @asset; | ||
| 362 | _correct_for_proper_rank_and_type_order \%asset; | ||
| 363 | |||
| 364 | my $asset = File::Assets::Asset->new(base => $self->rsc, cache => $self->cache, %asset); | ||
| 365 | |||
| 366 | return $self->fetch_or_store($asset); | ||
| 367 | } | ||
| 368 | |||
| 369 | =head2 $asset = $assets->include_content(<content>, [ <type>, <rank>, { ... } ]) | ||
| 370 | |||
| 371 | Include an asset with some content and of the supplied type. The value of <content> can be a "plain" string or a scalar reference. | ||
| 372 | |||
| 373 | You can include content that looks like HTML: | ||
| 374 | |||
| 375 | <style media="print"> | ||
| 376 | body { | ||
| 377 | font: serif; | ||
| 378 | } | ||
| 379 | </style> | ||
| 380 | |||
| 381 | In the above case, <type> is optional, as File::Assets can detect from the tag that you're supplying a style sheet. Furthermore, | ||
| 382 | the method will find all the attributes in the tag and put them into the asset. So the resulting asset from including the above | ||
| 383 | will have a type of "text/css" and media of "print". | ||
| 384 | |||
| 385 | For now, only <style> and <script> will map to types (.css and .js, respectively) | ||
| 386 | |||
| 387 | See ->include for more information on <rank>. | ||
| 388 | |||
| 389 | By default, the newly created $asset is inline. | ||
| 390 | |||
| 391 | Returns the newly created asset. | ||
| 392 | |||
| 393 | NOTE: The order of the <type> and <rank> arguments are reversed from ->include and ->include_path | ||
| 394 | Still, the order of <rank> and <type> doesn't really matter, since we can detect whether something looks like a rank (number) or | ||
| 395 | not, and correct for it (and it does). | ||
| 396 | |||
| 397 | =cut | ||
| 398 | |||
| 399 | sub include_content { | ||
| 400 | my $self = shift; | ||
| 401 | |||
| 402 | my @asset; | ||
| 403 | for (qw/content type rank/) { | ||
| 404 | last if ! @_ || ref $_[0] eq "HASH"; | ||
| 405 | push @asset, $_ => shift; | ||
| 406 | } | ||
| 407 | push @asset, %{ $_[0] } if @_ && ref $_[0] eq "HASH"; | ||
| 408 | my %asset = @asset; | ||
| 409 | _correct_for_proper_rank_and_type_order \%asset; | ||
| 410 | |||
| 411 | my $asset = File::Assets::Asset->new(%asset); | ||
| 412 | |||
| 413 | $self->store($asset); | ||
| 414 | |||
| 415 | return $asset; | ||
| 416 | } | ||
| 417 | |||
| 418 | =head2 $name = $assets->name([ <name> ]) | ||
| 419 | |||
| 420 | Retrieve and/or change the "name" of $assets; by default it is "assets" | ||
| 421 | |||
| 422 | This is useful for controlling the name of minified assets files. | ||
| 423 | |||
| 424 | Returns the name of $assets | ||
| 425 | |||
| 426 | =cut | ||
| 427 | |||
| 428 | sub name { | ||
| 429 | my $self = shift; | ||
| 430 | $self->{name} = shift if @_; | ||
| 431 | my $name = $self->{name}; | ||
| 432 | return defined $name && length $name ? $name : "assets"; | ||
| 433 | } | ||
| 434 | |||
| 435 | =head2 $html = $assets->export([ <type> ]) | ||
| 436 | |||
| 437 | Generate and return HTML for the assets of <type>. If no type is specified, then assets of every type are exported. | ||
| 438 | |||
| 439 | $html will be something like this: | ||
| 440 | |||
| 441 | <link rel="stylesheet" type="text/css" href="http://example.com/assets.css"> | ||
| 442 | <script src="http://example.com/assets.js" type="text/javascript"></script> | ||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | sub export { | ||
| 447 | my $self = shift; | ||
| 448 | my $type = shift; | ||
| 449 | my $format = shift; | ||
| 450 | $format = "html" unless defined $format; | ||
| 451 | my @assets = $self->exports($type); | ||
| 452 | |||
| 453 | if ($format eq "html") { | ||
| 454 | return $self->_export_html(\@assets); | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | croak "Don't know how to export for format ($format)"; | ||
| 458 | } | ||
| 459 | } | ||
| 460 | |||
| 461 | sub _export_html { | ||
| 462 | my $self = shift; | ||
| 463 | my $assets = shift; | ||
| 464 | |||
| 465 | my @content; | ||
| 466 | for my $asset (@$assets) { | ||
| 467 | my %attributes = %{ $asset->attributes }; | ||
| 468 | if ($asset->type->type eq "text/css") { | ||
| 469 | # if ($asset->kind->extension eq "css") { | ||
| 470 | if (! $asset->inline) { | ||
| 471 | push @content, LINK({ rel => "stylesheet", type => $asset->type->type, href => $asset->uri, %attributes }); | ||
| 472 | } | ||
| 473 | else { | ||
| 474 | push @content, STYLE({ type => $asset->type->type, %attributes, _ => [ "\n${ $asset->content }" ] }); | ||
| 475 | } | ||
| 476 | } | ||
| 477 | # elsif ($asset->kind->extension eq "js") { | ||
| 478 | elsif ($asset->type->type eq "application/javascript" || | ||
| 479 | $asset->type->type eq "application/x-javascript" || # Handle different MIME::Types versions. | ||
| 480 | $asset->type->type =~ m/\bjavascript\b/) { | ||
| 481 | if (! $asset->inline) { | ||
| 482 | push @content, SCRIPT({ type => "text/javascript", src => $asset->uri, _ => "", %attributes }); | ||
| 483 | } | ||
| 484 | else { | ||
| 485 | push @content, SCRIPT({ type => "text/javascript", %attributes, _ => [ "\n${ $asset->content }" ] }); | ||
| 486 | } | ||
| 487 | } | ||
| 488 | |||
| 489 | else { | ||
| 490 | croak "Don't know how to handle asset $asset" unless ! $asset->inline; | ||
| 491 | push @content, LINK({ type => $asset->type->type, href => $asset->uri }); | ||
| 492 | } | ||
| 493 | } | ||
| 494 | return join "\n", @content; | ||
| 495 | } | ||
| 496 | |||
| 497 | =head2 @assets = $assets->exports([ <type> ]) | ||
| 498 | |||
| 499 | Returns a list of assets, in ranking order, that are exported. If no type is specified, then assets of every type are exported. | ||
| 500 | |||
| 501 | You can use this method to generate your own HTML, if necessary. | ||
| 502 | |||
| 503 | =cut | ||
| 504 | |||
| 505 | sub exports { | ||
| 506 | my $self = shift; | ||
| 507 | my @assets = sort { $a->rank <=> $b->rank } $self->_exports(@_); | ||
| 508 | return @assets; | ||
| 509 | } | ||
| 510 | |||
| 511 | =head2 $assets->empty | ||
| 512 | |||
| 513 | Returns 1 if no assets have been included yet, 0 otherwise. | ||
| 514 | |||
| 515 | =cut | ||
| 516 | |||
| 517 | sub empty { | ||
| 518 | my $self = shift; | ||
| 519 | return keys %{ $self->_registry_hash } ? 0 : 1; | ||
| 520 | } | ||
| 521 | |||
| 522 | =head2 $assets->exists( <path> ) | ||
| 523 | |||
| 524 | Returns true if <path> has been included, 0 otherwise. | ||
| 525 | |||
| 526 | =cut | ||
| 527 | |||
| 528 | sub exists { | ||
| 529 | my $self = shift; | ||
| 530 | my $key = shift; | ||
| 531 | |||
| 532 | return exists $self->_registry_hash->{$key} ? 1 : 0; | ||
| 533 | } | ||
| 534 | |||
| 535 | =head2 $assets->store( <asset> ) | ||
| 536 | |||
| 537 | Store <asset> in $assets | ||
| 538 | |||
| 539 | =cut | ||
| 540 | |||
| 541 | sub store { | ||
| 542 | my $self = shift; | ||
| 543 | my $asset = shift; | ||
| 544 | |||
| 545 | return $self->_registry_hash->{$asset->key} = $asset; | ||
| 546 | } | ||
| 547 | |||
| 548 | =head2 $asset = $assets->fetch( <path> ) | ||
| 549 | |||
| 550 | Fetch the asset located at <path> | ||
| 551 | |||
| 552 | Returns undef if nothing at <path> exists yet | ||
| 553 | |||
| 554 | =cut | ||
| 555 | |||
| 556 | sub fetch { | ||
| 557 | my $self = shift; | ||
| 558 | my $key = shift; | ||
| 559 | |||
| 560 | return $self->_registry_hash->{$key}; | ||
| 561 | } | ||
| 562 | |||
| 563 | sub fetch_or_store { | ||
| 564 | my $self = shift; | ||
| 565 | my $asset = shift; | ||
| 566 | |||
| 567 | return $self->fetch($asset->key) if $self->exists($asset->key); | ||
| 568 | |||
| 569 | return $self->store($asset); | ||
| 570 | } | ||
| 571 | |||
| 572 | sub kind { | ||
| 573 | my $self = shift; | ||
| 574 | my $asset = shift; | ||
| 575 | my $type = $asset->type; | ||
| 576 | |||
| 577 | my $kind = File::Assets::Util->type_extension($type); | ||
| 578 | if (File::Assets::Util->same_type("css", $type)) { | ||
| 579 | # my $media = $asset->attributes->{media} || "screen"; # W3C says to assume screen by default, so we'll do the same. | ||
| 580 | my $media = $asset->attributes->{media}; | ||
| 581 | $kind = "$kind-$media" if defined $media && length $media; | ||
| 582 | } | ||
| 583 | |||
| 584 | return File::Assets::Kind->new($kind, $type); | ||
| 585 | } | ||
| 586 | |||
| 587 | sub _exports { | ||
| 588 | my $self = shift; | ||
| 589 | my $type = shift; | ||
| 590 | |||
| 591 | my $image_export; | ||
| 592 | $image_export = 1 if $type && $type =~ m/^image(?:\/\*)?$/; | ||
| 593 | |||
| 594 | my $hash = $self->_registry_hash; | ||
| 595 | my @assets; | ||
| 596 | if ($image_export) { | ||
| 597 | @assets = grep { "image" eq $_->type->mediaType } values %$hash; | ||
| 598 | } | ||
| 599 | else { | ||
| 600 | $type = File::Assets::Util->parse_type($type) unless $image_export; | ||
| 601 | if (defined $type) { | ||
| 602 | @assets = grep { $type->type eq $_->type->type } values %$hash; | ||
| 603 | } | ||
| 604 | else { | ||
| 605 | @assets = values %$hash; | ||
| 606 | } | ||
| 607 | } | ||
| 608 | |||
| 609 | my %bucket; | ||
| 610 | for my $asset (@assets) { | ||
| 611 | my $kind = $self->kind($asset); | ||
| 612 | my $bucket = $bucket{$kind->kind} ||= File::Assets::Bucket->new($kind, $self); | ||
| 613 | $bucket->add_asset($asset); | ||
| 614 | } | ||
| 615 | |||
| 616 | my @bucket; | ||
| 617 | if ($image_export) { | ||
| 618 | @bucket = values %bucket; | ||
| 619 | } | ||
| 620 | else { | ||
| 621 | my $filter_scheme = $self->{filter_scheme}; | ||
| 622 | my @global = @{ $filter_scheme->{'*'} || [] }; | ||
| 623 | for my $kind (sort keys %bucket) { | ||
| 624 | push @bucket, my $bucket = $bucket{$kind}; | ||
| 625 | $bucket->add_filter($_) for @global; | ||
| 626 | my $head = $bucket->kind->head; | ||
| 627 | for my $category (sort grep { ! m/^$head-/ } keys %$filter_scheme) { | ||
| 628 | next if length $category > length $kind; # Too specific | ||
| 629 | next unless 0 == index $kind, $category; | ||
| 630 | $bucket->add_filter($_) for (@{ $filter_scheme->{$category} }); | ||
| 631 | } | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | my @exports; | ||
| 636 | for my $bucket (@bucket) { | ||
| 637 | my $kind = $bucket->kind; | ||
| 638 | if ($kind->type->mediaType eq "image") { | ||
| 639 | for my $asset ($bucket->exports) { | ||
| 640 | $self->_transfer_asset($asset, $kind); | ||
| 641 | } | ||
| 642 | } | ||
| 643 | else { | ||
| 644 | push @exports, $bucket->exports; | ||
| 645 | } | ||
| 646 | } | ||
| 647 | |||
| 648 | if (! $image_export && $type && $type->type eq "text/css") { | ||
| 649 | $self->_exports("image"); # Do an image export if we're explicitly exporting css | ||
| 650 | } | ||
| 651 | |||
| 652 | return @exports; | ||
| 653 | } | ||
| 654 | |||
| 655 | sub _transfer_asset { | ||
| 656 | my $self = shift; | ||
| 657 | my $asset = shift; | ||
| 658 | my $kind = shift; | ||
| 659 | |||
| 660 | # This method makes the following assumption: | ||
| 661 | # $self->{output_path_scheme} = [ [ qw/*/ => $scheme ] ] | ||
| 662 | |||
| 663 | my $output_path_scheme = $self->{output_path_scheme}->[0]->[1]; | ||
| 664 | my $output_path = File::Assets::Util->build_output_path($output_path_scheme, { | ||
| 665 | name => $asset->path->last, | ||
| 666 | @_ }); | ||
| 667 | |||
| 668 | my $output_file = $self->rsc->file($output_path); | ||
| 669 | my $asset_path = $asset->path; | ||
| 670 | my $asset_file = $asset->file; | ||
| 671 | |||
| 672 | croak "Asset $asset_path does not exist!" unless -e $asset_file; | ||
| 673 | |||
| 674 | if (! -e $output_file || $asset_file->stat->mtime > $output_file->stat->mtime) { | ||
| 675 | my $dir = $output_file->parent; | ||
| 676 | $dir->mkpath unless -d $dir; | ||
| 677 | if ($dir->stat->dev eq $asset_file->stat->dev) { | ||
| 678 | link $asset_file, $output_file or croak "Couldn't link $asset_file, $output_file: $!"; | ||
| 679 | } | ||
| 680 | else { | ||
| 681 | File::Copy::copy $asset_file, $output_file or croak "Couldn't copy $asset_file, $output_file: $!"; | ||
| 682 | } | ||
| 683 | } | ||
| 684 | } | ||
| 685 | |||
| 686 | =head2 $assets->set_name( <name> ) | ||
| 687 | |||
| 688 | Set the name of $assets | ||
| 689 | |||
| 690 | This is exactly the same as | ||
| 691 | |||
| 692 | $assets->name( <name> ) | ||
| 693 | |||
| 694 | =cut | ||
| 695 | |||
| 696 | |||
| 697 | =head2 $assets->set_base( <base> ) | ||
| 698 | |||
| 699 | Set the base uri, dir, and path for assets | ||
| 700 | |||
| 701 | <base> can be a L<Path::Resource>, L<URI::ToDisk>, or a hash reference of the form: | ||
| 702 | |||
| 703 | { uri => ..., dir => ..., path => ... } | ||
| 704 | |||
| 705 | Given a dir of C</var/www/htdocs>, a uri of C<http://example.com/static>, and a | ||
| 706 | path of C<assets> then: | ||
| 707 | |||
| 708 | $assets will look for files in "/var/www/htdocs/assets" | ||
| 709 | |||
| 710 | $assets will "serve" files with "http://example.com/static/assets" | ||
| 711 | |||
| 712 | =cut | ||
| 713 | |||
| 714 | sub set_base { | ||
| 715 | my $self = shift; | ||
| 716 | croak "No base given" unless @_; | ||
| 717 | my $base = 1 == @_ ? shift : { @_ }; | ||
| 718 | croak "No base given" unless $base; | ||
| 719 | |||
| 720 | $self->{rsc} = File::Assets::Util->parse_rsc($base); | ||
| 721 | } | ||
| 722 | |||
| 723 | =head2 $assets->set_base_uri( <uri> ) | ||
| 724 | |||
| 725 | Set the base uri for assets | ||
| 726 | |||
| 727 | =cut | ||
| 728 | |||
| 729 | sub set_base_uri { | ||
| 730 | my $self = shift; | ||
| 731 | croak "No base uri given" unless defined $_[0]; | ||
| 732 | |||
| 733 | $self->{rsc}->base->uri(shift); | ||
| 734 | } | ||
| 735 | |||
| 736 | =head2 $assets->set_base_dir( <dir> ) | ||
| 737 | |||
| 738 | Set the base dir for assets | ||
| 739 | |||
| 740 | =cut | ||
| 741 | |||
| 742 | sub set_base_dir { | ||
| 743 | my $self = shift; | ||
| 744 | croak "No base dir given" unless defined $_[0]; | ||
| 745 | |||
| 746 | $self->{rsc}->base->dir(shift); | ||
| 747 | } | ||
| 748 | |||
| 749 | =head2 $assets->set_base_path( <path> ) | ||
| 750 | |||
| 751 | Set the base path for assets | ||
| 752 | |||
| 753 | Passing an undefined value for <path> will clear/get-rid-of the path | ||
| 754 | |||
| 755 | =cut | ||
| 756 | |||
| 757 | sub set_base_path { | ||
| 758 | my $self = shift; | ||
| 759 | my $path; | ||
| 760 | $path = defined $_[0] ? Path::Abstract->new(shift) : Path::Abstract->new; | ||
| 761 | # TODO-b This is very bad | ||
| 762 | $self->{rsc}->_path($path); | ||
| 763 | } | ||
| 764 | |||
| 765 | sub set_output_path_scheme { | ||
| 766 | my $self = shift; | ||
| 767 | my $scheme = shift; | ||
| 768 | |||
| 769 | if ($scheme && ref $scheme ne "ARRAY") { | ||
| 770 | $scheme = [ [ qw/*/ => $scheme ] ]; | ||
| 771 | } | ||
| 772 | |||
| 773 | $self->{output_path_scheme} = $scheme; | ||
| 774 | } | ||
| 775 | |||
| 776 | =head2 $assets->set_output_path( <path> ) | ||
| 777 | |||
| 778 | Set the output path for assets generated by $assets | ||
| 779 | |||
| 780 | See "Specifying an C<output_path> pattern" above | ||
| 781 | |||
| 782 | =cut | ||
| 783 | |||
| 784 | sub set_output_path { | ||
| 785 | my $self = shift; | ||
| 786 | $self->set_output_path_scheme(@_); | ||
| 787 | } | ||
| 788 | |||
| 789 | =head2 $assets->set_cache( <cache> ) | ||
| 790 | |||
| 791 | Specify the cache object or cache name to use | ||
| 792 | |||
| 793 | =cut | ||
| 794 | |||
| 795 | sub set_cache { | ||
| 796 | my $self = shift; | ||
| 797 | my $cache = shift; | ||
| 798 | |||
| 799 | if ($cache) { | ||
| 800 | $cache = File::Assets::Cache->new(name => $cache) unless blessed $cache && $cache->isa("File::Assets::Cache"); | ||
| 801 | $self->{cache} = $cache; | ||
| 802 | } | ||
| 803 | else { | ||
| 804 | delete $self->{cache}; | ||
| 805 | } | ||
| 806 | } | ||
| 807 | |||
| 808 | sub filter { | ||
| 809 | my $self = shift; | ||
| 810 | my ($kind, $filter); | ||
| 811 | if (@_ == 1) { | ||
| 812 | $filter = shift; | ||
| 813 | } | ||
| 814 | else { | ||
| 815 | $kind = File::Assets::Kind->new(shift); | ||
| 816 | $filter = shift; | ||
| 817 | } | ||
| 818 | |||
| 819 | my $name = $kind ? $kind->kind : '*'; | ||
| 820 | |||
| 821 | my $category = $self->{filter_scheme}->{$name} ||= []; | ||
| 822 | |||
| 823 | my $_filter = $filter; | ||
| 824 | unless (blessed $_filter) { | ||
| 825 | croak "Couldn't find filter for ($filter)" unless $_filter = File::Assets::Util->parse_filter($_filter, @_, assets => $self); | ||
| 826 | } | ||
| 827 | |||
| 828 | push @$category, $_filter; | ||
| 829 | |||
| 830 | return $_filter; | ||
| 831 | } | ||
| 832 | |||
| 833 | sub filter_clear { | ||
| 834 | my $self = shift; | ||
| 835 | if (blessed $_[0] && $_[0]->isa("File::Assets::Filter")) { | ||
| 836 | my $target = shift; | ||
| 837 | while (my ($name, $category) = each %{ $self->{filter_scheme} }) { | ||
| 838 | my @filters = grep { $_ != $target } @$category; | ||
| 839 | $self->{filter_scheme}->{$name} = \@filters; | ||
| 840 | } | ||
| 841 | return; | ||
| 842 | } | ||
| 843 | carp __PACKAGE__, "::filter_clear(\$type) is deprecated, nothing happens" and return if @_; | ||
| 844 | $self->{filter_scheme} = {}; | ||
| 845 | } | ||
| 846 | |||
| 847 | sub _calculate_best { | ||
| 848 | my $self = shift; | ||
| 849 | my $scheme = shift; | ||
| 850 | my $kind = shift; | ||
| 851 | my $signature = shift; | ||
| 852 | my $handler = shift; | ||
| 853 | my $default = shift; | ||
| 854 | |||
| 855 | my $key = join ":", $kind->kind, $signature; | ||
| 856 | |||
| 857 | my ($best_kind, %return); | ||
| 858 | %return = %$default if $default; | ||
| 859 | |||
| 860 | # TODO-f Cache the result of this | ||
| 861 | for my $rule (@$scheme) { | ||
| 862 | my ($condition, $action, $flags) = @$rule; | ||
| 863 | |||
| 864 | my $result; # 1 - A better match; -1 - A match, but worse; undef - Skip, not a match! | ||
| 865 | |||
| 866 | if (ref $condition eq "CODE") { | ||
| 867 | next unless defined ($result = $condition->($kind, $signature, $best_kind)); | ||
| 868 | } | ||
| 869 | elsif (ref $condition eq "") { | ||
| 870 | if ($condition eq $key) { | ||
| 871 | # Best possible match | ||
| 872 | $result = 1; | ||
| 873 | $best_kind = $kind; | ||
| 874 | } | ||
| 875 | elsif ($condition eq "*" || $condition eq "default") { | ||
| 876 | $result = $best_kind ? -1 : 1; | ||
| 877 | } | ||
| 878 | } | ||
| 879 | |||
| 880 | my ($condition_kind, $condition_signature) = split m/:/, $condition, 2; | ||
| 881 | |||
| 882 | unless (defined $result) { | ||
| 883 | |||
| 884 | # No exact match, try to find the best fit... | ||
| 885 | |||
| 886 | # Signature doesn't match or is not a wildcard, so move on to the next rule | ||
| 887 | next if defined $condition_signature && $condition_signature ne '*' && $condition_signature ne $signature; | ||
| 888 | |||
| 889 | if (length $condition_kind && $condition_kind ne '*') { | ||
| 890 | $condition_kind = File::Assets::Kind->new($condition_kind); | ||
| 891 | |||
| 892 | # Type isn't the same as the asset (or whatever) kind, so move on to the next rule | ||
| 893 | next unless File::Assets::Util->same_type($condition_kind->type, $kind->type); | ||
| 894 | } | ||
| 895 | } | ||
| 896 | |||
| 897 | # At this point, we have a match, but is it a better match then one we already have? | ||
| 898 | if (! $best_kind || ($condition_kind && $condition_kind->is_better_than($best_kind))) { | ||
| 899 | $result = 1; | ||
| 900 | } | ||
| 901 | |||
| 902 | next unless defined $result; | ||
| 903 | |||
| 904 | my %action; | ||
| 905 | %action = $handler->($action); | ||
| 906 | |||
| 907 | if ($result > 0) { | ||
| 908 | $return{$_} = $action{$_} for keys %action; | ||
| 909 | } | ||
| 910 | else { | ||
| 911 | for (keys %action) { | ||
| 912 | $return{$_} = $action{$_} unless defined $action{$_}; | ||
| 913 | } | ||
| 914 | } | ||
| 915 | } | ||
| 916 | |||
| 917 | return \%return; | ||
| 918 | } | ||
| 919 | |||
| 920 | sub output_path { | ||
| 921 | my $self = shift; | ||
| 922 | my $filter = shift; | ||
| 923 | |||
| 924 | my $result = $self->_calculate_best($self->{output_path_scheme}, $filter->kind, $filter->signature, sub { | ||
| 925 | my $action = shift; | ||
| 926 | return ref $action eq "CODE" ? %$action : path => $action; | ||
| 927 | }); | ||
| 928 | |||
| 929 | return $result; | ||
| 930 | } | ||
| 931 | |||
| 932 | sub output_asset { | ||
| 933 | my $self = shift; | ||
| 934 | my $filter = shift; | ||
| 935 | |||
| 936 | if (0) { | ||
| 937 | my $result = $self->_calculate_best($self->{output_asset_scheme}, $filter->kind, $filter->signature, sub { | ||
| 938 | my $action = shift; | ||
| 939 | return %$action; | ||
| 940 | }); | ||
| 941 | } | ||
| 942 | |||
| 943 | my $kind = $filter->kind; | ||
| 944 | my $output_path = $self->output_path($filter) or croak "Couldn't get output path for ", $kind->kind; | ||
| 945 | $output_path = File::Assets::Util->build_output_path($output_path, $filter); | ||
| 946 | |||
| 947 | my $asset = File::Assets::Asset->new(path => $output_path, base => $self->rsc, type => $kind->type); | ||
| 948 | return $asset; | ||
| 949 | } | ||
| 950 | |||
| 951 | 1; | ||
| 952 | |||
| 953 | =head1 AUTHOR | ||
| 954 | |||
| 955 | Robert Krimen, C<< <rkrimen at cpan.org> >> | ||
| 956 | |||
| 957 | =head1 SEE ALSO | ||
| 958 | |||
| 959 | L<Catalyst::Plugin::Assets> | ||
| 960 | |||
| 961 | L<Google::AJAX::Library> | ||
| 962 | |||
| 963 | L<JS::YUI::Loader> | ||
| 964 | |||
| 965 | L<JS::jQuery::Loader> | ||
| 966 | |||
| 967 | =head1 SOURCE | ||
| 968 | |||
| 969 | You can contribute or fork this project via GitHub: | ||
| 970 | |||
| 971 | L<http://github.com/robertkrimen/file-assets/tree/master> | ||
| 972 | |||
| 973 | git clone git://github.com/robertkrimen/file-assets.git File-Assets | ||
| 974 | |||
| 975 | =head1 BUGS | ||
| 976 | |||
| 977 | Please report any bugs or feature requests to C<bug-file-assets at rt.cpan.org>, or through | ||
| 978 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Assets>. I will be notified, and then you'll | ||
| 979 | automatically be notified of progress on your bug as I make changes. | ||
| 980 | |||
| 981 | =head1 SUPPORT | ||
| 982 | |||
| 983 | You can find documentation for this module with the perldoc command. | ||
| 984 | |||
| 985 | perldoc File::Assets | ||
| 986 | |||
| 987 | |||
| 988 | You can also look for information at: | ||
| 989 | |||
| 990 | =over 4 | ||
| 991 | |||
| 992 | =item * RT: CPAN's request tracker | ||
| 993 | |||
| 994 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Assets> | ||
| 995 | |||
| 996 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 997 | |||
| 998 | L<http://annocpan.org/dist/File-Assets> | ||
| 999 | |||
| 1000 | =item * CPAN Ratings | ||
| 1001 | |||
| 1002 | L<http://cpanratings.perl.org/d/File-Assets> | ||
| 1003 | |||
| 1004 | =item * Search CPAN | ||
| 1005 | |||
| 1006 | L<http://search.cpan.org/dist/File-Assets> | ||
| 1007 | |||
| 1008 | =back | ||
| 1009 | |||
| 1010 | |||
| 1011 | =head1 ACKNOWLEDGEMENTS | ||
| 1012 | |||
| 1013 | |||
| 1014 | =head1 COPYRIGHT & LICENSE | ||
| 1015 | |||
| 1016 | Copyright 2008 Robert Krimen | ||
| 1017 | |||
| 1018 | This program is free software; you can redistribute it and/or modify it | ||
| 1019 | under the same terms as Perl itself. | ||
| 1020 | |||
| 1021 | |||
| 1022 | =cut | ||
| 1023 | |||
| 1024 | 1; # End of File::Assets | ||
| 1025 | |||
| 1026 | __END__ |