Skip to content

Commit

Permalink
Add SVG support from image_optim.
Browse files Browse the repository at this point in the history
[Closes plasticine#12]
  • Loading branch information
nybblr committed Mar 3, 2014
1 parent c5a9aa7 commit 53f21f7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```

Expand Down
3 changes: 2 additions & 1 deletion lib/middleman-imageoptim/optimizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions lib/middleman-imageoptim/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
8 changes: 7 additions & 1 deletion spec/unit/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
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} }
its(:advpng_options) { should == {:level => 4} }
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
Expand Down Expand Up @@ -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

0 comments on commit 53f21f7

Please sign in to comment.