Class: Jekyll::SrcsetTag::Image
- Inherits:
-
Object
- Object
- Jekyll::SrcsetTag::Image
- Defined in:
- lib/jekyll/srcset_tag/image.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#html_attributes ⇒ Object
readonly
Returns the value of attribute html_attributes.
-
#image_path ⇒ Object
readonly
Returns the value of attribute image_path.
-
#output_path ⇒ Object
readonly
Returns the value of attribute output_path.
-
#ppi ⇒ Object
readonly
Returns the value of attribute ppi.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
-
#web_output_path ⇒ Object
readonly
Returns the value of attribute web_output_path.
Instance Method Summary collapse
- #digest ⇒ Object
- #generate_images! ⇒ Object
-
#initialize(source_path:, output_path:, web_output_path:, image_path:, ppi:, sources:, html_attributes:) ⇒ Image
constructor
A new instance of Image.
- #instances ⇒ Object
- #output_dir ⇒ Object
- #to_html ⇒ Object
- #uniq_instances ⇒ Object
- #web_output_dir ⇒ Object
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_attributes ⇒ Object (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_path ⇒ Object (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_path ⇒ Object (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 |
#ppi ⇒ Object (readonly)
Returns the value of attribute ppi.
10 11 12 |
# File 'lib/jekyll/srcset_tag/image.rb', line 10 def ppi @ppi end |
#source_path ⇒ Object (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 |
#sources ⇒ Object (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_path ⇒ Object (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
#digest ⇒ Object
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 |
#instances ⇒ Object
27 28 29 |
# File 'lib/jekyll/srcset_tag/image.rb', line 27 def instances @instances ||= create_instances end |
#output_dir ⇒ Object
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_html ⇒ Object
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_instances ⇒ Object
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_dir ⇒ Object
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 |