jammit 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jammit'
3
- s.version = '0.2.7' # Keep version in sync with jammit.rb
4
- s.date = '2009-11-19'
3
+ s.version = '0.2.8' # Keep version in sync with jammit.rb
4
+ s.date = '2009-11-23'
5
5
 
6
6
  s.homepage = "http://documentcloud.github.com/jammit/"
7
7
  s.summary = "Industrial Strength Asset Packaging for Rails"
@@ -4,11 +4,13 @@ $LOAD_PATH.push File.expand_path(File.dirname(__FILE__))
4
4
  # to all of the configuration options.
5
5
  module Jammit
6
6
 
7
- VERSION = "0.2.7"
7
+ VERSION = "0.2.8"
8
8
 
9
- ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
9
+ ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
10
10
 
11
- ASSET_ROOT = defined?(RAILS_ROOT) ? RAILS_ROOT : "."
11
+ ASSET_ROOT = File.expand_path(defined?(RAILS_ROOT) ? RAILS_ROOT : ".")
12
+
13
+ PUBLIC_ROOT = "#{ASSET_ROOT}/public"
12
14
 
13
15
  DEFAULT_CONFIG_PATH = "#{ASSET_ROOT}/config/assets.yml"
14
16
 
@@ -17,8 +17,9 @@ module Jammit
17
17
  '.tiff' => 'image/tiff'
18
18
  }
19
19
 
20
- # Detect all image URLs that are inside of an "embed" folder.
21
- IMAGE_DETECTOR = /url\(['"]?([^\s)]*embed\/[^\s)]+\.(png|jpg|jpeg|gif|tif|tiff))['"]?\)/
20
+
21
+ IMAGE_DETECTOR = /url\(['"]?([^\s)]+\.(png|jpg|jpeg|gif|tif|tiff))['"]?\)/
22
+ IMAGE_EMBED = /[\A\/]embed\//
22
23
  IMAGE_REPLACER = /url\(__EMBED__([^\s)]+)\)/
23
24
 
24
25
  # MHTML file constants.
@@ -57,11 +58,13 @@ module Jammit
57
58
  # :datauri or :mhtml variant, post-processes the result to embed
58
59
  # referenced images.
59
60
  def compress_css(paths, variant=nil, asset_url=nil)
60
- return @css_compressor.compress(concatenate(paths)) if variant.nil?
61
- compressed_css = @css_compressor.compress(concatenate_and_tag_images(paths))
62
- return with_data_uris(compressed_css) if variant == :datauri
63
- return with_mhtml(compressed_css, asset_url) if variant == :mhtml
64
- raise PackageNotFound, "\"#{variant}\" is not a valid stylesheet variant"
61
+ compressed_css = @css_compressor.compress(concatenate_and_tag_images(paths, variant))
62
+ case variant
63
+ when nil then return compressed_css
64
+ when :datauri then return with_data_uris(compressed_css)
65
+ when :mhtml then return with_mhtml(compressed_css, asset_url)
66
+ else raise PackageNotFound, "\"#{variant}\" is not a valid stylesheet variant"
67
+ end
65
68
  end
66
69
 
67
70
  # Compiles a single JST file by writing out a javascript that adds
@@ -86,11 +89,11 @@ module Jammit
86
89
  # expand the paths before contatenating the CSS together and losing the
87
90
  # location of the original stylesheet path. Validate the images while we're
88
91
  # at it.
89
- def concatenate_and_tag_images(paths)
92
+ def concatenate_and_tag_images(paths, variant=nil)
90
93
  stylesheets = [paths].flatten.map do |css_path|
91
94
  File.read(css_path).gsub(IMAGE_DETECTOR) do |url|
92
- image_path = public_path($1, css_path)
93
- valid_image(image_path) ? "url(__EMBED__#{image_path})" : url
95
+ new_path = rewrite_image_path(Pathname.new($1), Pathname.new(File.expand_path(css_path)), !!variant)
96
+ "url(#{new_path})"
94
97
  end
95
98
  end
96
99
  stylesheets.join("\n")
@@ -120,18 +123,35 @@ module Jammit
120
123
  [MHTML_START, mhtml, MHTML_END, css].flatten.join('')
121
124
  end
122
125
 
126
+ # Return a rewritten image URL for a new stylesheet -- the image should
127
+ # be tagged for embedding if embeddable, and referenced at the correct level
128
+ # if relative.
129
+ def rewrite_image_path(image_path, css_path, embed=false)
130
+ public_path = absolute_path(image_path, css_path)
131
+ return "__EMBED__#{public_path}" if embed && embeddable?(public_path)
132
+ image_path.relative? ? relative_path(public_path) : image_path.to_s
133
+ end
134
+
123
135
  # Get the site-absolute public path for an image file path that may or may
124
136
  # not be relative, given the path of the stylesheet that contains it.
125
- def public_path(image_path, css_path)
126
- image_path, css_path = Pathname.new(image_path), Pathname.new(css_path)
127
- (image_path.absolute? ? Pathname.new("#{ASSET_ROOT}/public#{image_path}") : css_path.dirname + image_path).cleanpath
137
+ def absolute_path(image_pathname, css_pathname)
138
+ (image_pathname.absolute? ?
139
+ Pathname.new(File.join(PUBLIC_ROOT, image_pathname)) :
140
+ css_pathname.dirname + image_pathname).cleanpath
141
+ end
142
+
143
+ # CSS images that are referenced by relative paths, and are *not* being
144
+ # embedded, must be rewritten relative to the newly-merged stylesheet path.
145
+ def relative_path(absolute_path)
146
+ File.join('../', absolute_path.sub(PUBLIC_ROOT, ''))
128
147
  end
129
148
 
130
- # An image is valid if it exists, and is less than 32K.
149
+ # An image is valid for embedding if it exists, is less than 32K, and is
150
+ # stored somewhere inside of a folder named "embed".
131
151
  # IE does not support Data-URIs larger than 32K, and you probably shouldn't
132
152
  # be embedding images that large in any case.
133
- def valid_image(image_path)
134
- image_path.exist? && image_path.size < 32.kilobytes
153
+ def embeddable?(image_path)
154
+ image_path.to_s.match(IMAGE_EMBED) && image_path.exist? && image_path.size < 32.kilobytes
135
155
  end
136
156
 
137
157
  # Return the Base64-encoded contents of an image on a single line.
@@ -9,7 +9,7 @@ module Jammit
9
9
 
10
10
  SUFFIX_STRIPPER = /-(datauri|mhtml)\Z/
11
11
 
12
- NOT_FOUND_PATH = "#{ASSET_ROOT}/public/404.html"
12
+ NOT_FOUND_PATH = "#{PUBLIC_ROOT}/404.html"
13
13
 
14
14
  after_filter :cache_package if perform_caching
15
15
 
@@ -82,6 +82,7 @@ module Jammit
82
82
 
83
83
  end
84
84
 
85
+ # Make the Jammit::Controller available to Rails as a top-level controller.
85
86
  ::JammitController = Jammit::Controller
86
87
 
87
88
  if RAILS_ENV == 'development'
@@ -18,7 +18,7 @@ module Jammit
18
18
  # create a new Packager.
19
19
  def initialize
20
20
  @compressor = Compressor.new
21
- @force = true
21
+ @force = false
22
22
  @config = {
23
23
  :css => (Jammit.configuration[:stylesheets] || {}).symbolize_keys,
24
24
  :js => (Jammit.configuration[:javascripts] || {}).symbolize_keys,
@@ -37,7 +37,7 @@ module Jammit
37
37
  # Unless forced, will only rebuild assets whose source files have been
38
38
  # changed since their last package build.
39
39
  def precache_all(output_dir=nil, base_url=nil)
40
- output_dir ||= "#{ASSET_ROOT}/public/#{Jammit.package_path}"
40
+ output_dir ||= File.join(PUBLIC_ROOT, Jammit.package_path)
41
41
  cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) }
42
42
  cacheable(:jst, output_dir).each {|p| cache(p, 'jst', pack_templates(p), output_dir) }
43
43
  cacheable(:css, output_dir).each do |p|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jammit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Ashkenas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-19 00:00:00 -05:00
12
+ date: 2009-11-23 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency