2015-12-28 09:41:04 +11:00
|
|
|
require 'pathname'
|
|
|
|
|
2014-12-07 09:28:21 +11:00
|
|
|
module Jekyll
|
2016-09-28 04:59:24 +10:00
|
|
|
module ResponsiveImage
|
2014-12-08 09:03:26 +11:00
|
|
|
module Utils
|
2016-09-28 04:59:24 +10:00
|
|
|
def keep_resized_image!(site, image)
|
|
|
|
keep_dir = File.dirname(image['path'])
|
|
|
|
site.config['keep_files'] << keep_dir unless site.config['keep_files'].include?(keep_dir)
|
|
|
|
end
|
|
|
|
|
2014-12-08 09:03:26 +11:00
|
|
|
def symbolize_keys(hash)
|
2014-12-07 09:28:21 +11:00
|
|
|
result = {}
|
|
|
|
hash.each_key do |key|
|
|
|
|
result[key.to_sym] = hash[key]
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
2014-12-08 00:06:28 +11:00
|
|
|
|
2015-12-28 09:41:04 +11:00
|
|
|
def format_output_path(format, base_path, image_path, width, height)
|
|
|
|
params = symbolize_keys(image_hash(base_path, image_path, width, height))
|
|
|
|
|
|
|
|
Pathname.new(format % params).cleanpath.to_s
|
2015-11-05 09:55:32 +11:00
|
|
|
end
|
|
|
|
|
2014-12-08 00:06:28 +11:00
|
|
|
# Build a hash containing image information
|
2015-12-28 09:41:04 +11:00
|
|
|
def image_hash(base_path, image_path, width, height)
|
2014-12-08 00:06:28 +11:00
|
|
|
{
|
2015-12-28 09:41:04 +11:00
|
|
|
'path' => image_path,
|
|
|
|
'dirname' => relative_dirname(base_path, image_path),
|
|
|
|
'basename' => File.basename(image_path),
|
|
|
|
'filename' => File.basename(image_path, '.*'),
|
|
|
|
'extension' => File.extname(image_path).delete('.'),
|
2014-12-08 00:06:28 +11:00
|
|
|
'width' => width,
|
|
|
|
'height' => height,
|
|
|
|
}
|
|
|
|
end
|
2015-12-28 09:41:04 +11:00
|
|
|
|
|
|
|
def relative_dirname(base_path, image_path)
|
|
|
|
path = Pathname.new(image_path).expand_path
|
|
|
|
base = Pathname.new(base_path).expand_path
|
|
|
|
|
|
|
|
path.relative_path_from(base).dirname.to_s.delete('.')
|
|
|
|
end
|
2014-12-07 09:28:21 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|