Commit 09fe9252 authored by Jan-Philipp Stauffert's avatar Jan-Philipp Stauffert
Browse files

Update

parent ee707654
Gem::Specification.new do |spec|
spec.name = "jekyll-img-srcset"
spec.version = "0.1.1"
spec.version = "0.1.3"
spec.authors = ["Jan-Philipp Stauffert"]
spec.email = ["jan-philipp.stauffert@uni-wuerzburg.de"]
......
require 'mini_magick'
def resize_image(url, widths, dest, base_image_path)
image = MiniMagick::Image.open(File.join(base_image_path, url))
w = image.dimensions[0]
aspect = image.dimensions[1].to_f / w
new_widths = widths.select {|x| x <= w}
new_widths.map do |width|
[width, File.join(dest, base_image_path, "#{width}", url)]
end.select do |width, target|
not File.exists? target
end.each do |width, target|
image = MiniMagick::Image.open(File.join(base_image_path, url))
image.resize "#{width}x#{width*aspect}"
if not Dir.exists? File.dirname(target)
FileUtils.mkdir_p File.dirname(target)
# 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
def resize_image(url, widths, dest, base_image_path, cache)
src_path = File.join(base_image_path, url)
new_widths, w = cache.getset(src_path) do
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}
new_widths.map do |width|
[width, File.join(dest, base_image_path, "#{width}", url)]
end.select do |width, target|
not File.exists? target
end.each 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
image.write target
[new_widths, w]
end
return new_widths, w
end
# Parses the argument list
#
# @param ident [String]
# the list to parse, e.g., '.someclass key="value"'
# @return [Hash]
# a hash containing the sorted values
def parse_identifier(ident)
i = {
classes: [],
......@@ -55,11 +78,7 @@ def format_image(ident, sizes, original_width, url, caption, title, baseurl, bas
end
content += ">"
content += "<div>"
srcset_sizes = if sizes.length > 0
sizes.map do | size |
"(min-width: #{(size*1.16).floor}) #{size}px"
end.join(", ") + ", 100vw"
elsif not ident[:sizes].nil? and ident[:sizes].length > 0
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"
......@@ -87,6 +106,7 @@ def format_image(ident, sizes, original_width, url, caption, title, baseurl, bas
return content
end
# Formats svg image data by providing the associated image tag
def format_svg_image(url, ident, title, baseurl, base_image_path)
content = "<img "
if not ident[:style].nil?
......@@ -103,6 +123,7 @@ end
def process_page(document, payload)
# see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841
doc_ext = document.extname.tr('.', '')
cache = Jekyll::Cache.new("jekyll-img-srcset")
if payload['site']['markdown_ext'].include? doc_ext
content = document.content
base_image_path = document.site.config["base_image_path"] || "assets/images"
......@@ -122,7 +143,7 @@ def process_page(document, payload)
widths, original_width = resize_image(img['url'],
document.site.config["widths"],
document.site.config["destination"],
base_image_path)
base_image_path, cache)
document.site.keep_files.concat(widths.map do |w|
File.join(base_image_path, "#{w}", img['url'])
end)
......@@ -144,12 +165,6 @@ Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, p
process_page(document, payload)
end
# Jekyll::Hooks.register [:pages, :posts, :documents], :post_render do |document, payload|
# # puts "Post"
# # puts document
# process_page(document, payload)
# end
def check_in_context(context, path)
parts = path.split(".")
c = context.dup
......@@ -165,6 +180,10 @@ end
module Jekyll
class ImgSrcsetTag < Liquid::Tag
def cache
@@cache ||= Jekyll::Cache.new("jekyll-img-srcset")
end
def initialize(tag_name, text, tokens)
super
......@@ -197,13 +216,11 @@ module Jekyll
end
end
#puts "fs context: #{context.find_variable('first_image')}"
# puts "Prepare image: #{attrs[:src]}"
if File.exists?(File.join base_image_path, attrs[:src])
widths, original_width = resize_image(
attrs[:src],
config["widths"],
config["destination"], base_image_path)
config["destination"], base_image_path, cache)
context.registers[:site].keep_files.concat(widths.map do |w|
File.join(base_image_path, "#{w}", attrs[:src])
end)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment