jekyll-responsive-image/lib/jekyll/responsive_image/utils.rb

47 lines
1.4 KiB
Ruby
Raw Permalink Normal View History

require 'pathname'
module Jekyll
module ResponsiveImage
module Utils
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
def symbolize_keys(hash)
result = {}
hash.each_key do |key|
result[key.to_sym] = hash[key]
end
result
end
2014-12-08 00:06:28 +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
end
2014-12-08 00:06:28 +11:00
# Build a hash containing image information
def image_hash(base_path, image_path, width, height)
2014-12-08 00:06:28 +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
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
end
end
end