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



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



50
51
52
53
54
55
56
57
# File 'lib/jekyll/srcset_tag/image.rb', line 50

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
26
27
28
# File 'lib/jekyll/srcset_tag/image.rb', line 22

def generate_images!
  unless images_exist?
    original = MiniMagick::Image.open File.join(source_path, image_path)
    instances.each { |instance| instance.for_image!(original) }
    resize_images! uniq_instances, original
  end
end

#images_exist?Boolean



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

def images_exist?
  Dir.exist? output_dir
end

#instancesObject



34
35
36
# File 'lib/jekyll/srcset_tag/image.rb', line 34

def instances
  @instances ||= create_instances
end

#output_dirObject



59
60
61
# File 'lib/jekyll/srcset_tag/image.rb', line 59

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

#to_htmlObject



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

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



38
39
40
# File 'lib/jekyll/srcset_tag/image.rb', line 38

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

#web_output_dirObject



63
64
65
# File 'lib/jekyll/srcset_tag/image.rb', line 63

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