class Object
Public Instance Methods
check_in_context(context, path)
click to toggle source
Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|
process_page(document, payload)
end
# File lib/jekyll-img-srcset.rb, line 182 def check_in_context(context, path) parts = path.split(".") c = context.dup parts.each do | part | if not c.is_a?(Array) and c.key? part c = c[part] else return nil end end return c end
format_image(ident, sizes, original_width, url, caption, title, baseurl, base_image_path)
click to toggle source
# File lib/jekyll-img-srcset.rb, line 83 def format_image(ident, sizes, original_width, url, caption, title, baseurl, base_image_path) srcset = sizes.map {|s| "#{baseurl}/#{base_image_path}/#{s}/#{url} #{s}w"} srcset << "#{baseurl}/#{base_image_path}/#{url} #{original_width}w" srcset = srcset.join ", " classes = "image #{ident[:dimension]} #{ident[:classes].join " "}" content = "<div class=\"#{classes}\"" if not ident[:style].nil? content += " style=\"#{ident[:style]}\"" end content += ">" content += "<div>" srcset_sizes = if not ident[:sizes].nil? and ident[:sizes].length > 0 ident[:sizes] else "(min-width: 1180px) 1024px, (min-width: 980px) 844px, (min-width: 740px) 640, 100vw" end if not caption.nil? and caption != "" content += "<figure> <img src = \"#{baseurl}/#{base_image_path}/#{sizes[0]}/#{url}\" alt = \"#{title}\" sizes = \"#{srcset_sizes}\" srcset= \"#{srcset}\" width = \"100%\" /> <figcaption>#{caption}</figcaption> </figure>" else content += "<img src = \"#{baseurl}/#{base_image_path}/#{sizes[0]}/#{url}\" alt = \"#{title}\" sizes = \"#{srcset_sizes}\" srcset = \"#{srcset}\" />" end content += "</div>" content += "</div>" return content end
format_svg_image(url, ident, title, baseurl, base_image_path)
click to toggle source
Formats svg image data by providing the associated image tag
# File lib/jekyll-img-srcset.rb, line 124 def format_svg_image(url, ident, title, baseurl, base_image_path) content = "<img " if not ident[:style].nil? content += "style=\"#{ident[:style]}\" " end if not ident[:classes].empty? content += "class=\"#{ident[:classes].join " "}\" " end content += "src = \"#{baseurl}/#{base_image_path}/#{url}\" " content += "alt = \"#{title}\"" content += "/>" end
parse_identifier(ident)
click to toggle source
Parses the argument list
@param ident [String]
the list to parse, e.g., '.someclass key="value"'
@return [Hash]
a hash containing the sorted values
# File lib/jekyll-img-srcset.rb, line 59 def parse_identifier(ident) i = { classes: [], sizes: "(min-width: 1180px) 1024px, (min-width: 980px) 844px, (min-width: 740px) 640, 100vw" } if ident.nil? return i end ident.scan(/#[^\s\}]+|\.[^\s\}]+|[^\s\}]+="[^"]+"/) do | match | if match.start_with? "#" match[0] = '' i[:id] = match elsif match.start_with? "." match[0] = '' i[:classes] << match else parts = match.split '=' i[parts[0].to_sym] = parts[1].gsub(/^"/, '').gsub(/"$/, '') end end return i end
resize_image(url, widths, dest, base_image_path, cache)
click to toggle source
Resizes an image to provide different versions for multiple display resolutions
@param url [String] the image's url @param widths [Array<Numeric>]
target sizes to resize to; only widths smaller than the original width are generated
@param base_image_path [String] the base image path. Usually assets/images @param cache [Jekyll::Cache]
cache object to not resize if the computation was already done
@return [Array<Numeric>, Numeric]
the generated widths and the original iamge width
# File lib/jekyll-img-srcset.rb, line 16 def resize_image(url, widths, dest, base_image_path, cache) src_path = File.join(base_image_path, url) src_mtime = File.new(src_path).mtime if cache.key? src_path _, _, modified_time = cache[src_path] if src_mtime > modified_time cache.delete src_path end end # resize images if they aren't already in the cache new_widths, w, modified_time = cache.getset(src_path) do modified_time = File.new(src_path).mtime image = MiniMagick::Image.open(src_path) w = image.dimensions[0] aspect = image.dimensions[1].to_f / w [w, aspect] new_widths = widths.select {|x| x <= w} width_to_create = new_widths.map do |width| [width, File.join(dest, base_image_path, "#{width}", url)] end .select do |width, target| (not File.exists? target) or (File.new(target).mtime < src_mtime) end Parallel.each(width_to_create) do |width, target| image = MiniMagick::Image.open(src_path) image.resize "#{width}x#{width*aspect}" if not Dir.exists? File.dirname(target) FileUtils.mkdir_p File.dirname(target) end image.write target end [new_widths, w, modified_time] end return new_widths, w end