Class: Jekyll::SrcsetTag::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/srcset_tag/image.rb

Defined Under Namespace

Classes: Instance, Source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path:, output_path:, web_output_path:, image_path:, ppi:, sources:, html_attributes:) ⇒ Image

Returns a new instance of Image.



12
13
14
15
16
17
18
19
20
# File 'lib/jekyll/srcset_tag/image.rb', line 12

def initialize(source_path:, output_path:, web_output_path:, image_path:, ppi:, sources:, html_attributes:)
  @source_path = source_path
  @output_path = output_path
  @web_output_path = web_output_path
  @image_path = image_path
  @ppi = ppi.respond_to?(:sort_by) ? ppi : ppi.split(',').map(&:to_i)
  @sources = sources
  @html_attributes = html_attributes
end

Instance Attribute Details

#html_attributesObject (readonly)

Returns the value of attribute html_attributes.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def html_attributes
  @html_attributes
end

#image_pathObject (readonly)

Returns the value of attribute image_path.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def image_path
  @image_path
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def output_path
  @output_path
end

#ppiObject (readonly)

Returns the value of attribute ppi.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def ppi
  @ppi
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def source_path
  @source_path
end

#sourcesObject (readonly)

Returns the value of attribute sources.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def sources
  @sources
end

#web_output_pathObject (readonly)

Returns the value of attribute web_output_path.



10
11
12
# File 'lib/jekyll/srcset_tag/image.rb', line 10

def web_output_path
  @web_output_path
end

Instance Method Details

#digestObject



43
44
45
46
47
48
49
50
# File 'lib/jekyll/srcset_tag/image.rb', line 43

def digest
  unless @digest
    file = Digest::SHA1.file(File.join(source_path, image_path)).to_s
    sizes = Digest::SHA1.hexdigest instances.map { |i| i.width.to_s + '-' + i.height.to_s }.join(' ')
    @digest = Digest::SHA1.hexdigest file + sizes
  end
  @digest
end

#generate_images!Object



22
23
24
25
# File 'lib/jekyll/srcset_tag/image.rb', line 22

def generate_images!
  needed_instances = uniq_instances.select { |instance| !File.exist?(File.join(output_dir, instance.filename)) }
  resize_images! needed_instances
end

#instancesObject



27
28
29
# File 'lib/jekyll/srcset_tag/image.rb', line 27

def instances
  @instances ||= create_instances
end

#output_dirObject



52
53
54
# File 'lib/jekyll/srcset_tag/image.rb', line 52

def output_dir
  File.join(output_path, image_path + '-' + digest.slice(0, 7))
end

#to_htmlObject



35
36
37
38
39
40
41
# File 'lib/jekyll/srcset_tag/image.rb', line 35

def to_html
  srcs = image_srcs
  src = CGI.escape_html(srcs.last[0])
  srcset = CGI.escape_html(srcs.map {|path, size| "#{path} #{size}w" }.join(', '))
  sizes = CGI::escape_html(source_sizes.join(', '))
  "<img src=\"#{src}\" srcset=\"#{srcset}\" sizes=\"#{sizes}\" #{html_attributes}/>\n"
end

#uniq_instancesObject



31
32
33
# File 'lib/jekyll/srcset_tag/image.rb', line 31

def uniq_instances
  instances.uniq { |instance| [ instance.output_width, instance.output_height ] }
end

#web_output_dirObject



56
57
58
# File 'lib/jekyll/srcset_tag/image.rb', line 56

def web_output_dir
  File.join(web_output_path, image_path + '-' + digest.slice(0, 7))
end