lib/middleman-simple-thumbnailer/extension.rb in middleman-simple-thumbnailer-1.3.1 vs lib/middleman-simple-thumbnailer/extension.rb in middleman-simple-thumbnailer-1.4.0
- old
+ new
@@ -1,9 +1,10 @@
require 'tmpdir'
require 'yaml'
require 'json'
require 'tempfile'
+require 'uri'
# add resize_to param to image_tag to create thumbnails
#
#
# Usage:
# = image_tag item.image, resize_to: '50x30', class: 'thumbnail'
@@ -15,10 +16,11 @@
option :use_specs, false, 'Wether or not use the specification data file'
option :specs_data, 'simple_thumbnailer', 'name of the specification data file. Must follow middleman data file name convention'
option :specs_data_default_format, 'yaml', 'defaut specification file format (and extension). Can be yaml, yml or json'
option :specs_data_save_old, true, 'save previous specification data file '
option :update_specs, true, 'Warn about missing image file in specification and add them to teh spec file'
+ option :use_cache_dev, false, 'In development, add a Rack middleware to serve the resized image from cache'
def initialize(app, options_hash={}, &block)
super
@images_store = MiddlemanSimpleThumbnailer::ImageStore.new
@resize_specs = app.data[options.specs_data] || []
@@ -28,33 +30,29 @@
if !%w(yaml yml json).include?(options.specs_data_default_format)
raise "value assigned to option specs_data_default_format is not correct. should be one of json, yaml, or yml"
end
end
+ def after_configuration
+ if app.development? && options.use_cache_dev
+ app.use MiddlemanSimpleThumbnailer::Rack, options, app, options
+ end
+ end
+
def store_resized_image(img_path, resize_to)
@images_store.store(img_path, resize_to)
end
- # def after_configuration
- # MiddlemanSimpleThumbnailer::Image.options = options
- # end
-
def check_image_in_specs(img_path, resize_to)
@resize_specs.each do |resize_spec|
if resize_to == resize_spec.resize_to && File.fnmatch(resize_spec.path, img_path)
return true
end
end
return false
end
-
- # def source_dir
- # File.absolute_path(app.config[:source], app.root)
- # end
-
-
def manipulate_resource_list(resources)
return resources unless options.use_specs
resources + @resize_specs.reduce([]) do |res, resize_spec|
Dir.chdir(File.absolute_path(File.join(app.root, app.config[:source], app.config[:images_dir]))) do
Dir.glob(resize_spec.path) do |image_file|
@@ -133,26 +131,37 @@
ext = app.extensions[:middleman_simple_thumbnailer]
image = MiddlemanSimpleThumbnailer::Image.new(path, resize_to, app, ext.options)
if app.development?
- "data:#{image.mime_type};base64,#{image.base64_data}"
+ if ext.options.use_cache_dev
+ [path, {"simple-thumbnailer" => "#{path}|#{resize_to}"}]
+ else
+ ["data:#{image.mime_type};base64,#{image.base64_data}", {}]
+ end
else
ext.store_resized_image(path, resize_to)
- image.resized_img_path
+ [image.resized_img_path, {}]
end
end
def image_tag(path, options={})
resize_to = options.delete(:resize_to)
- new_path = resize_to ? resized_image_path(path, resize_to) : path
+ new_path, query_params = resize_to ? resized_image_path(path, resize_to) : [path, {}]
+ if(!query_params.empty?)
+ new_path = "#{new_path}?#{URI.encode_www_form(query_params)}"
+ end
super(new_path, options)
end
def image_path(path, options={})
resize_to = options.delete(:resize_to)
- new_path = resize_to ? resized_image_path(path, resize_to) : path
- super(new_path)
+ new_path, query_params = resize_to ? resized_image_path(path, resize_to) : [path, {}]
+ res = super(new_path)
+ if(!query_params.empty?)
+ res = "#{res}?#{URI.encode_www_form(query_params)}"
+ end
+ res
end
end
end