sprockets-image_compressor 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,7 +1,12 @@
1
+ 0.1.2 (01/26/2012)
2
+ -------------------------
3
+
4
+ * Bugfix: Works on Rails 3.1 and 3.2 on Ruby 1.9
5
+
1
6
  0.1.1 (12/26/2011)
2
7
  -------------------------
3
8
 
4
- * Bugfix: make sure we're using binary encoding when dealing with the image files
9
+ * Bugfix: Make sure we're using binary encoding when dealing with the image files
5
10
  * Bugfix: Add basic sprockets integration tests
6
11
 
7
12
  0.1.0 (09/21/2011)
data/README.md CHANGED
@@ -14,9 +14,9 @@ The gem ships with a Railtie which will automatically register the compressor pr
14
14
 
15
15
  ## TODO
16
16
 
17
- * Detect missing pngcrush or jpegoptim installations
17
+ * Detect missing pngcrush or jpegoptim installations (vendor?)
18
18
  * Provide configuration hooks
19
- * Tests
19
+ * Test Railtie
20
20
 
21
21
  ## License
22
22
 
@@ -2,6 +2,8 @@ module Sprockets
2
2
  module ImageCompressor
3
3
  class Integration
4
4
  def self.setup env
5
+ monkey_patch_older_sprockets!
6
+
5
7
  env.register_mime_type 'image/png', '.png'
6
8
  env.register_postprocessor 'image/png', :png_compressor do |context, data|
7
9
  PngCompressor.new.compress data
@@ -12,6 +14,115 @@ module Sprockets
12
14
  JpgCompressor.new.compress data
13
15
  end
14
16
  end
17
+
18
+ # Rails locks down Sprockets to an older version, which will cause the image compressor to barf on ruby 1.9.
19
+ # Monkey patch in binary fixes from Sprockets 2.2.0.
20
+ def self.monkey_patch_older_sprockets!
21
+ return unless Sprockets::VERSION =~ /2\.[01]\.\d/ # Sprockets version >= 2.0.0 and < 2.2.0
22
+ return unless "".respond_to?(:valid_encoding?) # is ruby 1.9?
23
+
24
+ Sprockets::BundledAsset.class_eval do
25
+ def build_dependency_context_and_body
26
+ start_time = Time.now.to_f
27
+
28
+ context = blank_context
29
+
30
+ # Read original data once and pass it along to `Context`
31
+ mime_type = environment.mime_types(pathname.extname)
32
+ encoding = environment.encoding_for_mime_type(mime_type)
33
+ if encoding && "".respond_to?(:valid_encoding?)
34
+ data = Sprockets::Utils.read_unicode(pathname, encoding)
35
+ else
36
+ data = Sprockets::Utils.read_unicode(pathname)
37
+ end
38
+
39
+ # Prime digest cache with data, since we happen to have it
40
+ environment.file_digest(pathname, data)
41
+
42
+ # Runs all processors on `Context`
43
+ body = context.evaluate(pathname, :data => data)
44
+
45
+ @dependency_context, @body = context, body
46
+
47
+ elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i
48
+ logger.info "Compiled #{logical_path} (#{elapsed_time}ms) (pid #{Process.pid})"
49
+
50
+ return context, body
51
+ end
52
+ end
53
+
54
+ Sprockets::Context.class_eval do
55
+ def evaluate(path, options = {})
56
+ pathname = resolve(path)
57
+ attributes = environment.attributes_for(pathname)
58
+ processors = options[:processors] || attributes.processors
59
+
60
+ if options[:data]
61
+ result = options[:data]
62
+ else
63
+ mime_type = environment.mime_types(pathname.extname)
64
+ encoding = environment.encoding_for_mime_type(mime_type)
65
+ if encoding && "".respond_to?(:valid_encoding?)
66
+ result = Sprockets::Utils.read_unicode(pathname, encoding)
67
+ else
68
+ result = Sprockets::Utils.read_unicode(pathname)
69
+ end
70
+ end
71
+
72
+ processors.each do |processor|
73
+ begin
74
+ template = processor.new(pathname.to_s) { result }
75
+ result = template.render(self, {})
76
+ rescue Exception => e
77
+ annotate_exception! e
78
+ raise
79
+ end
80
+ end
81
+
82
+ result
83
+ end
84
+ end
85
+
86
+ Sprockets::Mime.module_eval do
87
+ # Mime types that should be opened with BINARY encoding.
88
+ def binary_mime_types
89
+ @binary_mime_types ||= [ %r{^(image|audio|video)/} ]
90
+ end
91
+
92
+ # Returns the correct encoding for a given mime type, while falling
93
+ # back on the default external encoding, if it exists.
94
+ def encoding_for_mime_type(type)
95
+ encoding = "BINARY" if binary_mime_types.any? { |matcher| matcher === type }
96
+ encoding ||= default_external_encoding if respond_to?(:default_external_encoding)
97
+ encoding
98
+ end
99
+ end
100
+
101
+ Sprockets::Utils.module_eval do
102
+ def self.utf8_bom_pattern
103
+ @ut8_bom_pattern ||= Regexp.new("\\A\uFEFF".encode('utf-8'))
104
+ end
105
+
106
+ def self.read_unicode(pathname, external_encoding = Encoding.default_external)
107
+ pathname.open("r:#{external_encoding}") do |f|
108
+ f.read.tap do |data|
109
+ # Eager validate the file's encoding. In most cases we
110
+ # expect it to be UTF-8 unless `default_external` is set to
111
+ # something else. An error is usually raised if the file is
112
+ # saved as UTF-16 when we expected UTF-8.
113
+ if !data.valid_encoding?
114
+ raise EncodingError, "#{pathname} has a invalid " +
115
+ "#{data.encoding} byte sequence"
116
+
117
+ # If the file is UTF-8 and theres a BOM, strip it for safe concatenation.
118
+ elsif data.encoding.name == "UTF-8" && data =~ utf8_bom_pattern
119
+ data.sub!(utf8_bom_pattern, "")
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
15
126
  end
16
127
  end
17
128
  end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Imagecompressor
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -0,0 +1,3 @@
1
+ html {
2
+ body: red; // UTF8 ☃
3
+ }
@@ -13,14 +13,21 @@ describe "sprockets integration" do
13
13
  end
14
14
 
15
15
  it "should compress pngs" do
16
- get "/largepng.png"
17
- last_response.should be_ok
18
- last_response.headers["Content-Length"].should == "116773"
16
+ big_response = get "/largepng.png"
17
+ small_response = get "/smallpng.png"
18
+ big_response.headers["Content-Length"].should == small_response.headers["Content-Length"]
19
+ big_response.body.should == small_response.body
19
20
  end
20
21
 
21
22
  it "should compress jpgs" do
22
- get "/largejpg.jpg"
23
- last_response.should be_ok
24
- last_response.headers["Content-Length"].should == "4000"
23
+ big_response = get "/largejpg.jpg"
24
+ small_response = get "/smalljpg.jpg"
25
+ big_response.headers["Content-Length"].should == small_response.headers["Content-Length"]
26
+ big_response.body.should == small_response.body
27
+ end
28
+
29
+ it "should still serve text assets" do
30
+ response = get "/test.css"
31
+ response.status.should == 200
25
32
  end
26
33
  end
metadata CHANGED
@@ -1,89 +1,67 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sprockets-image_compressor
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Micah Geisel
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-12-26 00:00:00 -08:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-01-27 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: sprockets
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &80292680 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: ruby-debug
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *80292680
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby-debug
27
+ requirement: &80292420 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
47
33
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
34
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *80292420
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &80292210 !ruby/object:Gem::Requirement
53
39
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
61
44
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: rack-test
65
45
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *80292210
47
+ - !ruby/object:Gem::Dependency
48
+ name: rack-test
49
+ requirement: &80291950 !ruby/object:Gem::Requirement
67
50
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
75
55
  type: :development
76
- version_requirements: *id004
56
+ prerelease: false
57
+ version_requirements: *80291950
77
58
  description: Losslessly compress images in the Rails asset pipeline
78
- email:
59
+ email:
79
60
  - micah@botandrose.com
80
61
  executables: []
81
-
82
62
  extensions: []
83
-
84
63
  extra_rdoc_files: []
85
-
86
- files:
64
+ files:
87
65
  - .gitignore
88
66
  - CHANGELOG.md
89
67
  - Gemfile
@@ -100,41 +78,31 @@ files:
100
78
  - spec/fixtures/largepng.png
101
79
  - spec/fixtures/smalljpg.jpg
102
80
  - spec/fixtures/smallpng.png
81
+ - spec/fixtures/test.css
103
82
  - spec/integration/sprockets_integration_spec.rb
104
83
  - sprockets-image_compressor.gemspec
105
- has_rdoc: true
106
- homepage: ""
84
+ homepage: ''
107
85
  licenses: []
108
-
109
86
  post_install_message:
110
87
  rdoc_options: []
111
-
112
- require_paths:
88
+ require_paths:
113
89
  - lib
114
- required_ruby_version: !ruby/object:Gem::Requirement
90
+ required_ruby_version: !ruby/object:Gem::Requirement
115
91
  none: false
116
- requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- hash: 3
120
- segments:
121
- - 0
122
- version: "0"
123
- required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
97
  none: false
125
- requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- hash: 3
129
- segments:
130
- - 0
131
- version: "0"
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
132
102
  requirements: []
133
-
134
103
  rubyforge_project: sprockets-image_compressor
135
- rubygems_version: 1.6.2
104
+ rubygems_version: 1.8.10
136
105
  signing_key:
137
106
  specification_version: 3
138
107
  summary: Losslessly compress images in the Rails asset pipeline
139
108
  test_files: []
140
-