Commit 4c710900 authored by Jan-Philipp Stauffert's avatar Jan-Philipp Stauffert
Browse files

Add modification time comparison, delete native Markdown image tag

parent 09fe9252
PATH
remote: .
specs:
jekyll-img-srcset (0.1.0)
jekyll-img-srcset (0.1.3)
jekyll (~> 4.0)
mini_magick
mini_magick (~> 4.9)
parallel (~> 1.19)
GEM
remote: https://rubygems.org/
......@@ -16,10 +17,10 @@ GEM
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
eventmachine (1.2.7)
ffi (1.11.3)
ffi (1.12.1)
forwardable-extended (2.6.0)
http_parser.rb (0.6.0)
i18n (1.7.0)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
jekyll (4.0.0)
addressable (~> 2.4)
......@@ -44,25 +45,26 @@ GEM
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
liquid (4.0.3)
listen (3.2.0)
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.3.6)
mini_magick (4.9.5)
mini_magick (4.10.1)
parallel (1.19.1)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.1)
public_suffix (4.0.3)
rake (12.3.3)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
rouge (3.13.0)
rouge (3.15.0)
safe_yaml (1.0.5)
sassc (2.2.1)
ffi (~> 1.9)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
unicode-display_width (1.6.0)
unicode-display_width (1.6.1)
PLATFORMS
ruby
......
......@@ -33,11 +33,7 @@ Or install it yourself as:
## Usage
There are two ways to use this plugin. It tries to convert all Markdown image tags and offers a liquid tag for use in HTML content:
```markdown
![caption text](url "alt text"){.class style="color: black;"}
```
Use the Liquid tag as shown below to generate a figure tag with the requested resolutions.
```
{% imgsrcset src="general/map.jpg" dimension="wide" %}
......
Gem::Specification.new do |spec|
spec.name = "jekyll-img-srcset"
spec.version = "0.1.3"
spec.version = "0.2.0"
spec.authors = ["Jan-Philipp Stauffert"]
spec.email = ["jan-philipp.stauffert@uni-wuerzburg.de"]
......@@ -15,6 +15,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "jekyll", "~> 4.0"
spec.add_runtime_dependency "mini_magick", "~> 4.9"
spec.add_runtime_dependency "parallel", "~> 1.19"
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 12.0"
......
require 'mini_magick'
require 'parallel'
# Resizes an image to provide different versions for multiple
# display resolutions
......@@ -14,17 +15,28 @@ require 'mini_magick'
# 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
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}
new_widths.map do |width|
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
end.each do |width, target|
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)
......@@ -32,8 +44,9 @@ def resize_image(url, widths, dest, base_image_path, cache)
end
image.write target
end
[new_widths, w]
[new_widths, w, modified_time]
end
return new_widths, w
end
......@@ -120,50 +133,50 @@ def format_svg_image(url, ident, title, baseurl, base_image_path)
content += "/>"
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"
image_regex = /!\[(?<caption>.*?)?\]\((?<url>\S+?)(?<title> .+?)?\)(?<identifier>\{.+?\})?/
content = content.gsub(image_regex) do | img |
img = image_regex.match img
if (/\.(jpe?g|png)$/i =~ img["url"]).nil?
"#{base_image_path}/#{img[0]}"
elsif img["url"].start_with? "http"
img[0]
elsif img["url"].end_with? ".svg"
ident = parse_identifier img[:identifier]
format_svg_image(img["url"], ident, img["title"],
document.site.baseurl)
else
if File.exists?(File.join base_image_path, img['url'])
widths, original_width = resize_image(img['url'],
document.site.config["widths"],
document.site.config["destination"],
base_image_path, cache)
document.site.keep_files.concat(widths.map do |w|
File.join(base_image_path, "#{w}", img['url'])
end)
ident = parse_identifier img[:identifier]
format_image(ident, widths, original_width,
img["url"], img["caption"], img["title"],
document.site.baseurl, base_image_path)
else
img[0]
end
end
end
# puts content
document.content = content
end
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"
# image_regex = /!\[(?<caption>.*?)?\]\((?<url>\S+?)(?<title> .+?)?\)(?<identifier>\{.+?\})?/
# content = content.gsub(image_regex) do | img |
# img = image_regex.match img
# if (/\.(jpe?g|png)$/i =~ img["url"]).nil?
# "#{base_image_path}/#{img[0]}"
# elsif img["url"].start_with? "http"
# img[0]
# elsif img["url"].end_with? ".svg"
# ident = parse_identifier img[:identifier]
# format_svg_image(img["url"], ident, img["title"],
# document.site.baseurl)
# else
# if File.exists?(File.join base_image_path, img['url'])
# widths, original_width = resize_image(img['url'],
# document.site.config["widths"],
# document.site.config["destination"],
# base_image_path, cache)
# document.site.keep_files.concat(widths.map do |w|
# File.join(base_image_path, "#{w}", img['url'])
# end)
# ident = parse_identifier img[:identifier]
# format_image(ident, widths, original_width,
# img["url"], img["caption"], img["title"],
# document.site.baseurl, base_image_path)
# else
# img[0]
# end
# end
# end
# # puts content
# document.content = content
# end
# end
Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|
process_page(document, payload)
end
# Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|
# process_page(document, payload)
# end
def check_in_context(context, path)
parts = path.split(".")
......@@ -182,7 +195,7 @@ module Jekyll
class ImgSrcsetTag < Liquid::Tag
def cache
@@cache ||= Jekyll::Cache.new("jekyll-img-srcset")
@@cache ||= Jekyll::Cache.new("Jekyll::ImgSrcset")
end
def initialize(tag_name, text, tokens)
......@@ -235,7 +248,4 @@ module Jekyll
end
end
Liquid::Template.register_tag('imgsrcset', Jekyll::ImgSrcsetTag)
# /![(?<caption>.*?)?]\((?<url>[^\s]+?)(?<title> .+?)?\)(?<identifier>{.+?})?/
\ No newline at end of file
Liquid::Template.register_tag('imgsrcset', Jekyll::ImgSrcsetTag)
\ No newline at end of file
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