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

34 lines
943 B
Ruby
Raw Permalink Normal View History

module Jekyll
module ResponsiveImage
class Renderer
2016-09-28 07:04:09 +10:00
attr_reader :site, :attributes
def initialize(site, attributes)
@site = site
@attributes = attributes
end
2016-09-28 07:04:09 +10:00
def render
cache_key = attributes.to_s
result = attributes['cache'] ? RenderCache.get(cache_key) : nil
if result.nil?
2016-09-28 07:04:09 +10:00
config = Config.new(site).to_h
2016-09-28 07:04:09 +10:00
image = ImageProcessor.process(attributes['path'], config)
template_vars = attributes.merge(site.site_payload)
.merge('original' => image[:original], 'resized' => image[:resized])
2016-09-28 07:04:09 +10:00
image_template = site.in_source_dir(attributes['template'] || config['template'])
template = Liquid::Template.parse(File.read(image_template))
result = template.render(template_vars)
RenderCache.set(cache_key, result)
end
result
end
end
end
end