Skip to content

Commit

Permalink
Resolves issue accessing Middleman Information.
Browse files Browse the repository at this point in the history
  • Loading branch information
fdeschenes committed Jan 14, 2018
1 parent a539cae commit 4ce88b8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
29 changes: 26 additions & 3 deletions lib/middleman-imageoptim/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ module Middleman
# Middleman extension entry point
module Imageoptim
class Extension < Middleman::Extension
if Gem::Version.new(Middleman::VERSION) >= Gem::Version.new('4.0.0')
option :advpng, { level: 4 }, ''
option :allow_lossy, false, ''
option :gifsicle, { interlace: false }, ''
option :image_extensions, %w(.png .jpg .jpeg .gif .svg), ''
option :jpegoptim, { strip: ['all'], max_quality: 100 }, ''
option :jpegtran, { copy_chunks: false, progressive: true, jpegrescan: true }, ''
option :nice, true, ''
option :manifest, true, ''
option :optipng, { level: 6, interlace: false }, ''
option :pack, true, ''
option :pngcrush, { chunks: ['alla'], fix: false, brute: false }, ''
option :pngout, { copy_chunks: false, strategy: 0 }, ''
option :skip_missing_workers, true, ''
option :svgo, {}, ''
option :threads, true, ''
option :verbose, false, ''
end

def after_build(builder)
Middleman::Imageoptim::Optimizer.optimize!(app, builder, options)
end
Expand All @@ -16,9 +35,13 @@ def manipulate_resource_list(resources)
private

def setup_options(options_hash = {}, &_block)
@options = Middleman::Imageoptim::Options.new(options_hash)
yield @options if block_given?
@options.freeze
if Gem::Version.new(Middleman::VERSION) >= Gem::Version.new('4.0.0')
super
else
@options = Middleman::Imageoptim::Options.new(options_hash)
yield @options if block_given?
@options.freeze
end
end
end
end
Expand Down
10 changes: 9 additions & 1 deletion lib/middleman-imageoptim/optimizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ def builder_thor
end

def optimizer
@optimizer ||= ImageOptim.new(options.imageoptim_options)
@optimizer ||= begin
imageoptim_options = if Gem::Version.new(Middleman::VERSION) >= Gem::Version.new('4.0.0')
options.to_h.except(:image_extensions, :manifest)
else
options.imageoptim_options
end

ImageOptim.new(imageoptim_options)
end
end

def manifest
Expand Down
90 changes: 46 additions & 44 deletions lib/middleman-imageoptim/options.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
module Middleman
module Imageoptim
# An options store that handles default options will accept user defined
# overrides
class Options
# Mapping of valid option names to default values
EXTENSION_OPTIONS = [
:image_extensions,
:manifest
].freeze
OPTIONS = {
advpng: { level: 4 },
allow_lossy: false,
gifsicle: { interlace: false },
image_extensions: %w(.png .jpg .jpeg .gif .svg),
jpegoptim: { strip: ['all'], max_quality: 100 },
jpegtran: { copy_chunks: false, progressive: true, jpegrescan: true },
nice: true,
manifest: true,
optipng: { level: 6, interlace: false },
pack: true,
pngcrush: { chunks: ['alla'], fix: false, brute: false },
pngout: { copy_chunks: false, strategy: 0 },
skip_missing_workers: true,
svgo: {},
threads: true,
verbose: false
}.freeze
if Gem::Version.new(Middleman::VERSION) < Gem::Version.new('4.0.0')
module Middleman
module Imageoptim
# An options store that handles default options will accept user defined
# overrides
class Options
# Mapping of valid option names to default values
EXTENSION_OPTIONS = [
:image_extensions,
:manifest
].freeze
OPTIONS = {
advpng: { level: 4 },
allow_lossy: false,
gifsicle: { interlace: false },
image_extensions: %w(.png .jpg .jpeg .gif .svg),
jpegoptim: { strip: ['all'], max_quality: 100 },
jpegtran: { copy_chunks: false, progressive: true, jpegrescan: true },
nice: true,
manifest: true,
optipng: { level: 6, interlace: false },
pack: true,
pngcrush: { chunks: ['alla'], fix: false, brute: false },
pngout: { copy_chunks: false, strategy: 0 },
skip_missing_workers: true,
svgo: {},
threads: true,
verbose: false
}.freeze

attr_accessor *OPTIONS.keys.map(&:to_sym)
attr_accessor *OPTIONS.keys.map(&:to_sym)

def initialize(user_options = {})
set_options(user_options)
end
def initialize(user_options = {})
set_options(user_options)
end

def imageoptim_options
Hash[instance_variables.map do |name|
[symbolize_key(name), instance_variable_get(name)]
end].reject { |key| EXTENSION_OPTIONS.include?(key) }
end
def imageoptim_options
Hash[instance_variables.map do |name|
[symbolize_key(name), instance_variable_get(name)]
end].reject { |key| EXTENSION_OPTIONS.include?(key) }
end

private
private

def symbolize_key(key)
key.to_s[1..-1].to_sym
end
def symbolize_key(key)
key.to_s[1..-1].to_sym
end

def set_options(user_options)
OPTIONS.keys.each do |name|
instance_variable_set(:"@#{name}", user_options.fetch(name, OPTIONS[name]))
def set_options(user_options)
OPTIONS.keys.each do |name|
instance_variable_set(:"@#{name}", user_options.fetch(name, OPTIONS[name]))
end
end
end
end
Expand Down

0 comments on commit 4ce88b8

Please sign in to comment.