lib/middleman-webp/converter.rb in middleman-webp-0.1.0 vs lib/middleman-webp/converter.rb in middleman-webp-0.2.0
- old
+ new
@@ -1,12 +1,15 @@
+require "middleman-webp/options"
+
module Middleman
module WebP
class Converter
- def initialize(app, builder)
+ def initialize(app, options={}, builder)
@app = app
+ @options = Middleman::WebP::Options.new(options)
@builder = builder
end
def convert
@total = 0
@@ -20,25 +23,24 @@
end
def convert_images(paths, &after_conversion)
paths.each do |p|
dst = destination_path(p)
- if p.to_s =~ /gif$/i
- run_gif2webp(p, dst)
- else
- run_cwebp(p, dst)
- end
+ exec_convert_tool(p, dst)
yield File.new(p), File.new(dst)
end
end
- def run_cwebp(src, dst)
- system("cwebp -quiet #{src} -o #{dst}")
+ def exec_convert_tool(src, dst)
+ system("#{tool_for(src)} #{@options.for(src)} -quiet #{src} -o #{dst}")
end
- def run_gif2webp(src, dst)
- system("gif2webp -quiet #{src} -o #{dst}")
+ # Internal: Return proper tool command based on file type
+ #
+ # file - Filename
+ def tool_for(file)
+ file.to_s =~ /gif$/i ? "gif2webp" : "cwebp"
end
def reject_file(file)
@builder.say_status :webp, "#{file.path} skipped", :yellow
File.unlink(file)
@@ -47,10 +49,10 @@
# Calculate change percentage of converted file size
#
# src - File instance of the source
# dst - File instance of the destination
def change_percentage(src, dst)
- "%.2f%%" % [100 - 100.0 * dst.size / src.size]
+ "%g %%" % ("%.2f" % [100 - 100.0 * dst.size / src.size])
end
def destination_path(src_path)
dst_name = src_path.basename.to_s.gsub(/(jpe?g|png|tiff?|gif)$/, "webp")
src_path.parent.join(dst_name)