Class: Jekyll::ResponsiveImage::Renderer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/jekyll-responsive-image/renderer.rb

Instance Method Summary collapse

Methods included from Utils

#image_hash, #keep_resized_image!, #relative_dirname, #symbolize_keys

Constructor Details

#initialize(site, attributes) ⇒ Renderer

Returns a new instance of Renderer.



6
7
8
9
# File 'lib/jekyll-responsive-image/renderer.rb', line 6

def initialize(site, attributes)
  @site = site
  @attributes = attributes
end

Instance Method Details

#render_responsive_imageObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jekyll-responsive-image/renderer.rb', line 11

def render_responsive_image
  config = Config.new(@site).to_h
  use_cache = config['cache'] || @attributes['cache']
  cache_key = @attributes.to_s
  result = use_cache ? RenderCache.get(cache_key) : nil

  if result.nil?
    image = ImageProcessor.process(@attributes['path'], config)
    @attributes['original'] = image[:original]
    @attributes['resized'] = image[:resized]

    @attributes['resized'].each { |resized| keep_resized_image!(@site, resized) }

    image_template = @site.in_source_dir(@attributes['template'] || config['template'])
    partial = File.read(image_template)
    template = Liquid::Template.parse(partial)

    result = template.render!(@attributes.merge(@site.site_payload))

    RenderCache.set(cache_key, result)
  end

  result
end