Module: Pngnq
- Defined in:
- lib/pngnq.rb,
lib/pngnq/version.rb
Overview
A Ruby wrapper around the pngnq command line tool.
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
-
.build_line_for(options) ⇒ String
Turns a provided hash into the command-line string format
pngnqwants. -
.cocaine ⇒ Cocaine::CommandLine
Generates an instnace of a Cocaine command line runner.
-
.combine_defaults_with(options) ⇒ Hash
Mashes in a given hash with our defaults.
-
.convert(file, options = {}) ⇒ Boolean
Converts a file using
pnqnq. -
.mappings ⇒ Hash
A hash of key-to-flag mappings.
-
.value_mappings ⇒ Hash
A hash of value-to-command-line-value mappings.
Class Method Details
.build_line_for(options) ⇒ String
Turns a provided hash into the command-line string format pngnq wants.
95 96 97 98 99 100 101 102 103 |
# File 'lib/pngnq.rb', line 95 def self.build_line_for() pairs = .map do |key, value| next if value == false flag = self.mappings[key] val = self.value_mappings[value.to_s] || value.to_s flag.nil? ? nil : [flag, val].join(' ') end pairs.reject { |p| p.nil? }.join(' ').strip end |
.cocaine ⇒ Cocaine::CommandLine
Generates an instnace of a Cocaine command line runner.
40 41 42 |
# File 'lib/pngnq.rb', line 40 def self.cocaine Cocaine::CommandLine.new('pngnq', ":options :file") end |
.combine_defaults_with(options) ⇒ Hash
Mashes in a given hash with our defaults.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pngnq.rb', line 58 def self.combine_defaults_with() { :colors => 256, :force => true, :quantization => :no_dithering, :speed => 1, :gamma => 1.8, :output => '.', }.merge() end |
.convert(file, options = {}) ⇒ Boolean
Converts a file using pnqnq.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pngnq.rb', line 23 def self.convert(file, ={}) = self.combine_defaults_with() command = self.build_line_for() new_file = file.gsub(/\.png$/, '-nq8.png') destination = File.join([:output], File.basename(file)) FileUtils.rm(new_file) if File.exists?(new_file) FileUtils.rm(destination) if [:force] && File.exists?(destination) result = self.cocaine.run(:options => command, :file => file) FileUtils.mv(new_file, destination) if result result end |
.mappings ⇒ Hash
A hash of key-to-flag mappings.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pngnq.rb', line 71 def self.mappings { :colors => '-n', :force => '-f', :quantization => '-Q', :speed => '-s', :gamma => '-g', :output => '-d', } end |
.value_mappings ⇒ Hash
A hash of value-to-command-line-value mappings.
84 85 86 87 88 89 90 |
# File 'lib/pngnq.rb', line 84 def self.value_mappings { 'true' => '', 'no_dithering' => 'n', 'floyd_steinberg' => 'f', } end |