From 53f21f73a33c1bba288cf59e7c780a4417cf187c Mon Sep 17 00:00:00 2001 From: Jonathan Martin Date: Thu, 30 Jan 2014 21:33:47 -0500 Subject: [PATCH] Add SVG support from image_optim. [Closes #12] --- README.md | 3 ++- lib/middleman-imageoptim/optimizer.rb | 3 ++- lib/middleman-imageoptim/options.rb | 10 +++++++--- spec/unit/options_spec.rb | 8 +++++++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e1fd48a..8e0e142 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ activate :imageoptim do |options| options.threads = true # Image extensions to attempt to compress - options.image_extensions = %w(.png .jpg .gif) + options.image_extensions = %w(.png .jpg .gif .svg) # compressor worker options, individual optimisers can be disabled by passing # false instead of a hash @@ -52,6 +52,7 @@ activate :imageoptim do |options| options.jpegoptim_options = {:strip => ['all'], :max_quality => 100} options.jpegtran_options = {:copy_chunks => false, :progressive => true, :jpegrescan => true} options.gifsicle_options = {:interlace => false} + options.svgo_options = {} end ``` diff --git a/lib/middleman-imageoptim/optimizer.rb b/lib/middleman-imageoptim/optimizer.rb index 443dc20..2535f39 100644 --- a/lib/middleman-imageoptim/optimizer.rb +++ b/lib/middleman-imageoptim/optimizer.rb @@ -85,7 +85,8 @@ def optimizer :advpng => @options.advpng_options, :jpegoptim => @options.jpegoptim_options, :jpegtran => @options.jpegtran_options, - :gifsicle => @options.gifsicle_options + :gifsicle => @options.gifsicle_options, + :svgo => @options.svgo_options ) end diff --git a/lib/middleman-imageoptim/options.rb b/lib/middleman-imageoptim/options.rb index 7900da0..5c444f3 100644 --- a/lib/middleman-imageoptim/options.rb +++ b/lib/middleman-imageoptim/options.rb @@ -7,11 +7,11 @@ class Options attr_accessor :user_options attr_reader :verbose, :nice, :threads, :image_extensions, :pngcrush_options, :pngout_options, :optipng_options, :advpng_options, - :jpegoptim_options, :jpegtran_options, :gifsicle_options + :jpegoptim_options, :jpegtran_options, :gifsicle_options, :svgo_options UserOptions = Struct.new(:verbose, :nice, :threads, :image_extensions, :pngcrush_options, :pngout_options, :optipng_options, :advpng_options, - :jpegoptim_options, :jpegtran_options, :gifsicle_options) + :jpegoptim_options, :jpegtran_options, :gifsicle_options, :svgo_options) def initialize(options_hash = {}) @user_options = UserOptions.new(*options_hash) @@ -30,7 +30,7 @@ def threads end def image_extensions - !@user_options.image_extensions.nil? ? @user_options.image_extensions : %w(.png .jpg .jpeg .gif) + !@user_options.image_extensions.nil? ? @user_options.image_extensions : %w(.png .jpg .jpeg .gif .svg) end def pngcrush_options @@ -60,6 +60,10 @@ def jpegtran_options def gifsicle_options !@user_options.gifsicle_options.nil? ? @user_options.gifsicle_options : {:interlace => false} end + + def svgo_options + !@user_options.svgo_options.nil? ? @user_options.svgo_options : {} + end end end end diff --git a/spec/unit/options_spec.rb b/spec/unit/options_spec.rb index 6a76ebb..b517aa8 100644 --- a/spec/unit/options_spec.rb +++ b/spec/unit/options_spec.rb @@ -8,7 +8,7 @@ its(:verbose) { should be_false } its(:nice) { should be_true } its(:threads) { should be_true } - its(:image_extensions) { should == ['.png', '.jpg', '.jpeg', '.gif'] } + its(:image_extensions) { should == ['.png', '.jpg', '.jpeg', '.gif', '.svg'] } its(:pngcrush_options) { should == {:chunks => ['alla'], :fix => false, :brute => false} } its(:pngout_options) { should == {:copy_chunks => false, :strategy => 0} } its(:optipng_options) { should == {:level => 6, :interlace => false} } @@ -16,6 +16,7 @@ its(:jpegoptim_options) { should == {:strip => ['all'], :max_quality => 100} } its(:jpegtran_options) { should == {:copy_chunks => false, :progressive => true, :jpegrescan => true} } its(:gifsicle_options) { should == {:interlace => false} } + its(:svgo_options) { should == {} } end describe "with user options" do @@ -73,5 +74,10 @@ let(:options_hash) { {gifsicle_options: {foo: 'bar'}} } subject { options.gifsicle_options().should == {foo: 'bar'} } end + + describe "#svgo_options" do + let(:options_hash) { {svgo_options: {foo: 'bar'}} } + subject { options.svgo_options().should == {foo: 'bar'} } + end end end