optipng 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (10) hide show
  1. data/.document +5 -0
  2. data/Gemfile +15 -0
  3. data/Gemfile.lock +27 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.md +47 -0
  6. data/Rakefile +37 -0
  7. data/VERSION +1 -0
  8. data/lib/optipng.rb +124 -0
  9. data/test +8 -0
  10. metadata +122 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "depq", ">= 0.4"
5
+ # gem "hash-utils", ">= 0.9.0"
6
+ gem "pipe-run", ">= 0.1.0"
7
+ gem "command-builder", ">= 0.1.0"
8
+ gem "unix-whereis", ">= 0.1.0"
9
+
10
+ # Add dependencies to develop your gem here.
11
+ # Include everything needed to run rake, tests, features, etc.
12
+ group :development do
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.2"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ command-builder (0.1.0)
5
+ hash-utils (>= 0.7.0)
6
+ git (1.2.5)
7
+ hash-utils (0.9.0)
8
+ jeweler (1.5.2)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ pipe-run (0.1.0)
13
+ rake (0.8.7)
14
+ unix-whereis (0.1.0)
15
+ command-builder (>= 0.1.0)
16
+ hash-utils (>= 0.9.0)
17
+ pipe-run (>= 0.1.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ bundler (~> 1.0.0)
24
+ command-builder (>= 0.1.0)
25
+ jeweler (~> 1.5.2)
26
+ pipe-run (>= 0.1.0)
27
+ unix-whereis (>= 0.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ Optipng
2
+ =======
3
+
4
+ **Optipng** provides Ruby interface to the [`optipng`][1] tool.
5
+ Some examples follow: (for details, see module documentation)
6
+
7
+ require "optipng"
8
+
9
+ Optipng.available? # will return true (or false)
10
+
11
+ Optipng.optimize(["foo.png", "empty.png", "nonexist.png"], { :level => 5 })
12
+
13
+ # will run 'optipng -o 5 foo.png bar.png empty.png'
14
+ # and then will return for example:
15
+ # '#<struct Optipng::Result succeed={"foo.png => -22.1}}, errors=[["empty.png", "Unrecognized image file format"], ["nonexist.png", "Can't open the input file"]]>
16
+
17
+ ### Call Result
18
+
19
+ Result contains members `:success` and `:errors`. Sucess member contains
20
+ hash of successfully optimized files with ratio as value. Zero or
21
+ positive percent ratio means the same as file has been skipped so
22
+ already optimized. It's negative number against the number reported by
23
+ `optipng` so it means new size against the old size.
24
+
25
+ Errors contains array with pairs where first member of the pair is
26
+ filename and second the message. First one can be null if message isn't
27
+ strictly associated with concrete file. (But fortunately usually it is.)
28
+
29
+ Contributing
30
+ ------------
31
+
32
+ 1. Fork it.
33
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
34
+ 3. Commit your changes (`git commit -am "Added something"`).
35
+ 4. Push to the branch (`git push origin 20101220-my-change`).
36
+ 5. Create an [Issue][2] with a link to your branch.
37
+ 6. Enjoy a refreshing Diet Coke and wait.
38
+
39
+ Copyright
40
+ ---------
41
+
42
+ Copyright &copy; 2011 [Martin Kozák][3]. See `LICENSE.txt` for
43
+ further details.
44
+
45
+ [1]: http://optipng.sourceforge.net/
46
+ [2]: http://github.com/martinkozak/qrpc/issues
47
+ [3]: http://www.martinkozak.net/
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "optipng"
17
+ gem.homepage = "https://github.com/martinkozak/optipng"
18
+ gem.license = "MIT"
19
+ gem.summary = "Ruby interface to 'optipng' tool."
20
+ gem.email = "martinkozak@martinkozak.net"
21
+ gem.authors = ["Martin Kozák"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/rdoctask'
30
+ Rake::RDocTask.new do |rdoc|
31
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
32
+
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = "qrpc #{version}"
35
+ rdoc.rdoc_files.include('README*')
36
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/optipng.rb ADDED
@@ -0,0 +1,124 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "command-builder"
5
+ require "pipe-run"
6
+ require "unix/whereis"
7
+
8
+ ##
9
+ # The +optipng+ tool command frontend.
10
+ # @see http://optipng.sourceforge.net/
11
+ #
12
+
13
+ module Optipng
14
+
15
+ ##
16
+ # Holds +optipng+ command.
17
+ #
18
+
19
+ COMMAND = :optipng
20
+
21
+ ##
22
+ # Result structure.
23
+ #
24
+
25
+ Result = Struct::new(:succeed, :errors)
26
+
27
+ ##
28
+ # Holds output matchers.
29
+ #
30
+
31
+ MATCHERS = [
32
+ /^Processing\:\s*(.*)/,
33
+ /^Error:\s*(.*)/,
34
+ /(\d+\.\d+)%/,
35
+ /already optimized\.$/,
36
+ ]
37
+
38
+ ##
39
+ # Checks if +jpegoptim+ is available.
40
+ # @return [Boolean] +true+ if it is, +false+ in otherwise
41
+ #
42
+
43
+ def self.available?
44
+ return Whereis.available? self::COMMAND
45
+ end
46
+
47
+ ##
48
+ # Performs optimizations above file or set of files.
49
+ #
50
+ # @param [String, Array] paths file path or array of paths for optimizing
51
+ # @param [Hash] options options
52
+ # @option options [Integer] :level optimization level (0-7)
53
+ # @option options [Boolean] :debug turn on debugging mode, so command will be put out to the +STDERR+
54
+ # @return [Struct] see {Result}
55
+ #
56
+
57
+ def self.optimize(paths, options = { })
58
+
59
+ # Command
60
+ cmd = CommandBuilder::new(self::COMMAND)
61
+
62
+ # Max
63
+ if options[:level].kind_of? Integer
64
+ cmd.arg(:o, options[:level].to_i)
65
+ end
66
+
67
+ # Files
68
+ if paths.kind_of? String
69
+ paths = [paths]
70
+ end
71
+
72
+ # Runs the command
73
+ cmd << paths
74
+
75
+ if options[:debug] == true
76
+ STDERR.write cmd.to_s + "\n"
77
+ end
78
+
79
+ output = Pipe.run(cmd.to_s)
80
+
81
+ # Parses output
82
+ succeed, errors = __parse_output(output)
83
+ return self::Result::new(succeed, errors)
84
+
85
+ end
86
+
87
+
88
+ private
89
+
90
+ ##
91
+ # Parses output.
92
+ #
93
+
94
+ def self.__parse_output(output)
95
+ errors = [ ]
96
+ succeed = { }
97
+
98
+ output.split("**").each do |section|
99
+ section.strip!
100
+ if section.start_with? "Processing:"
101
+
102
+ # Scans each line
103
+ filename = nil
104
+
105
+ section.each_line do |line|
106
+ if m = line.match(self::MATCHERS[0])
107
+ filename = m[1]
108
+ elsif m = line.match(self::MATCHERS[1])
109
+ errors << [filename, m[1]]
110
+ next
111
+ elsif m = line.match(self::MATCHERS[2])
112
+ succeed[filename] = -1 * m[1].to_f
113
+ next
114
+ elsif m = line.match(self::MATCHERS[3])
115
+ succeed[filename] = 0.0
116
+ end
117
+ end
118
+
119
+ end
120
+ end
121
+
122
+ return [succeed, errors]
123
+ end
124
+ end
data/test ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+
4
+ $:.push("./lib")
5
+ require "optipng"
6
+
7
+ puts Optipng.available?.inspect
8
+ puts Optipng.optimize(["1.png", "4.png", "mp.png"], :level => 7, :debug => true)
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optipng
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Martin Koz\xC3\xA1k"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-21 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: pipe-run
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: command-builder
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.1.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: unix-whereis
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 0.1.0
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: bundler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.0.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: jeweler
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 1.5.2
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ description:
72
+ email: martinkozak@martinkozak.net
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - LICENSE.txt
79
+ - README.md
80
+ files:
81
+ - .document
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - VERSION
88
+ - lib/optipng.rb
89
+ - test
90
+ has_rdoc: true
91
+ homepage: https://github.com/martinkozak/optipng
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: -2347584964124837502
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.5.2
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Ruby interface to 'optipng' tool.
121
+ test_files: []
122
+