nanoc-image-compressor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .rvmrc
2
+ .yardoc
3
+ Gemfile.lock
4
+ doc
5
+ pkg
6
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 John Nishinaga
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Nanoc Image Compressor
2
+
3
+ A [nanoc](http://nanoc.stoneship.org/) filter that compresses `jpg` and `png` images losslessly.
4
+
5
+ ## Dependencies
6
+
7
+ This gem uses the [sprockets-image_compressor](https://github.com/botandrose/sprockets-image_compressor)
8
+ gem which depends on [pngcrush](http://pmt.sourceforge.net/pngcrush/) and
9
+ [jpegoptim](http://www.kokkonen.net/tjko/projects.html) being installed.
10
+ As a fall back, the gem includes 32-bit and 64-bit binaries for Linux.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your site's `Gemfile`:
15
+
16
+ gem 'nanoc-image-compressor'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install nanoc-image-compressor
25
+
26
+ Then, add this line to your site's `lib/default.rb`:
27
+
28
+ require 'nanoc/filters/image_compressor'
29
+
30
+ ## Usage
31
+
32
+ Add a filter within a `compile` block in your site's `Rules`:
33
+
34
+ compile '/images/*/' do
35
+ filter :image_compressor if item.binary?
36
+ end
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'yard'
4
+ YARD::Rake::YardocTask.new
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
@@ -0,0 +1,40 @@
1
+ require 'fileutils'
2
+ require 'nanoc'
3
+ require 'sprockets/image_compressor'
4
+
5
+ module Nanoc
6
+ module Filters
7
+ class ImageCompressor < Nanoc::Filter
8
+
9
+ VERSION = '0.0.1'
10
+
11
+ identifier :image_compressor
12
+ type :binary
13
+
14
+ # Compresses the content with Sprockets::ImageCompressor.
15
+ #
16
+ # @param [String] filename The filename to compress
17
+ # @option params [String] :type ('auto') png, jpg or auto
18
+ # @return [void]
19
+ def run(filename, params={})
20
+ type = (params[:type] || 'auto').to_s.downcase
21
+ type = filetype_guess(filename) if type == 'auto'
22
+ content = File.read(filename)
23
+ compressor = case type
24
+ when 'png'
25
+ content = Sprockets::ImageCompressor::PngCompressor.new.compress(content)
26
+ when 'jpg', 'jpeg'
27
+ content = Sprockets::ImageCompressor::JpgCompressor.new.compress(content)
28
+ end
29
+ File.open(output_filename, 'wb') { |f| f.write content }
30
+ end
31
+
32
+ private
33
+
34
+ def filetype_guess(filename)
35
+ filename.sub(/^.+\.([a-z]+)$/i, '\1').downcase
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1 @@
1
+ require 'nanoc/filters/image_compressor'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["John Nishinaga"]
5
+ gem.email = ["jingoro@casa-z.org"]
6
+ gem.description = %q{Nanoc image compressor filter}
7
+ gem.summary = %q{A nanoc filter that compresses jpg and png images losslessly}
8
+ gem.homepage = "https://github.com/jingoro/nanoc-image-compressor"
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "nanoc-image-compressor"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = '0.0.1'
16
+
17
+ gem.add_dependency 'nanoc', '>= 3.3.0'
18
+ gem.add_dependency 'sprockets-image_compressor'
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'yard'
22
+ gem.add_development_dependency 'bluecloth'
23
+ end
@@ -0,0 +1,76 @@
1
+ require 'tempfile'
2
+ require 'nanoc/filters/image_compressor'
3
+
4
+ describe Nanoc::Filters::ImageCompressor do
5
+
6
+ before do
7
+ item = double('item')
8
+ item.stub(:identifier) { '/test/' }
9
+ item_rep = double('item_rep')
10
+ item_rep.stub(:name) { 'image' }
11
+ subject.assigns[:item] = item
12
+ subject.assigns[:item_rep] = item_rep
13
+ end
14
+
15
+ let(:extension) { path.sub(/^.+\.([a-z]+)$/, '\1') }
16
+ let(:output_size) { File.size subject.output_filename }
17
+
18
+ def self.it_should_compress_to_less_than(max_size)
19
+
20
+ it 'should compress implicitly' do
21
+ subject.run path
22
+ output_size.should be < max_size
23
+ end
24
+
25
+ it 'should compress explicitly' do
26
+ subject.run path, :type => extension
27
+ output_size.should be < max_size
28
+ end
29
+
30
+ end
31
+
32
+ def self.it_should_not_compress
33
+
34
+ it 'should pass through implicitly' do
35
+ subject.run path
36
+ output_size.should == File.size(path)
37
+ end
38
+
39
+ it 'should pass through explicitly' do
40
+ subject.run path, :type => extension
41
+ output_size.should == File.size(path)
42
+ end
43
+
44
+ end
45
+
46
+ context 'jpg image' do
47
+ let(:path) { File.expand_path '../test.jpg', __FILE__ }
48
+ it_should_compress_to_less_than 300
49
+ end
50
+
51
+ context 'jpeg image' do
52
+ let(:path) do
53
+ file = Tempfile.new(['test', '.jpeg'])
54
+ file.write File.read(File.expand_path('../test.jpg', __FILE__))
55
+ file.close
56
+ file.path
57
+ end
58
+ it_should_compress_to_less_than 300
59
+ end
60
+
61
+ context 'png image' do
62
+ let(:path) { File.expand_path '../test.png', __FILE__ }
63
+ it_should_compress_to_less_than 1900
64
+ end
65
+
66
+ context 'gif image' do
67
+ let(:path) { File.expand_path '../test.gif', __FILE__ }
68
+ it_should_not_compress
69
+ end
70
+
71
+ context 'text file' do
72
+ let(:path) { __FILE__ }
73
+ it_should_not_compress
74
+ end
75
+
76
+ end
data/spec/test.gif ADDED
Binary file
data/spec/test.jpg ADDED
Binary file
data/spec/test.png ADDED
Binary file
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoc-image-compressor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - John Nishinaga
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nanoc
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 11
29
+ segments:
30
+ - 3
31
+ - 3
32
+ - 0
33
+ version: 3.3.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: sprockets-image_compressor
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: rspec
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: yard
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id005
92
+ - !ruby/object:Gem::Dependency
93
+ name: bluecloth
94
+ prerelease: false
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ type: :development
105
+ version_requirements: *id006
106
+ description: Nanoc image compressor filter
107
+ email:
108
+ - jingoro@casa-z.org
109
+ executables: []
110
+
111
+ extensions: []
112
+
113
+ extra_rdoc_files: []
114
+
115
+ files:
116
+ - .gitignore
117
+ - .rspec
118
+ - .travis.yml
119
+ - Gemfile
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - lib/nanoc-image-compressor.rb
124
+ - lib/nanoc/filters/image_compressor.rb
125
+ - nanoc-image-compressor.gemspec
126
+ - spec/image_compressor_spec.rb
127
+ - spec/test.gif
128
+ - spec/test.jpg
129
+ - spec/test.png
130
+ homepage: https://github.com/jingoro/nanoc-image-compressor
131
+ licenses: []
132
+
133
+ post_install_message:
134
+ rdoc_options: []
135
+
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ hash: 3
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: 3
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ requirements: []
157
+
158
+ rubyforge_project:
159
+ rubygems_version: 1.8.10
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: A nanoc filter that compresses jpg and png images losslessly
163
+ test_files:
164
+ - spec/image_compressor_spec.rb
165
+ - spec/test.gif
166
+ - spec/test.jpg
167
+ - spec/test.png
168
+ has_rdoc: