jekyll-img-srcset 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/jekyll-img-srcset.rb +224 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9a56a3f914ab0ffe61cb9e9defd02ab2aa2105b7da27a3ed38462a626d31bd01
|
4
|
+
data.tar.gz: b44a9958fc4cc20013e564bc5c3a540e1d94155ea0b873963793884dffcd9f5c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 49dc2db7317d4228a898cb5f0edb7376b34a006e927ffbb1436caf083508ca3e1620f0825ff954af55adc027207753fc90c26bf516bd59fd6a3afd9ba2bdf5b8
|
7
|
+
data.tar.gz: d727ea1bc08d6100d0dc5cf37a90e3ef983399743d21730ef1139b01028fc09f5956550d813fb9dda086ee7c29b511fc536d889fbde86a42b22f32a0f87241b8
|
@@ -0,0 +1,224 @@
|
|
1
|
+
require 'mini_magick'
|
2
|
+
|
3
|
+
def resize_image(url, widths, dest, base_image_path)
|
4
|
+
image = MiniMagick::Image.open(File.join(base_image_path, url))
|
5
|
+
w = image.dimensions[0]
|
6
|
+
aspect = image.dimensions[1].to_f / w
|
7
|
+
new_widths = widths.select {|x| x <= w}
|
8
|
+
new_widths.map do |width|
|
9
|
+
[width, File.join(dest, base_image_path, "#{width}", url)]
|
10
|
+
end.select do |width, target|
|
11
|
+
not File.exists? target
|
12
|
+
end.each do |width, target|
|
13
|
+
image = MiniMagick::Image.open(File.join(base_image_path, url))
|
14
|
+
image.resize "#{width}x#{width*aspect}"
|
15
|
+
if not Dir.exists? File.dirname(target)
|
16
|
+
FileUtils.mkdir_p File.dirname(target)
|
17
|
+
end
|
18
|
+
image.write target
|
19
|
+
end
|
20
|
+
return new_widths, w
|
21
|
+
end
|
22
|
+
|
23
|
+
def parse_identifier(ident)
|
24
|
+
i = {
|
25
|
+
classes: [],
|
26
|
+
sizes: "(min-width: 1180px) 1024px, (min-width: 980px) 844px, (min-width: 740px) 640, 100vw"
|
27
|
+
}
|
28
|
+
if ident.nil?
|
29
|
+
return i
|
30
|
+
end
|
31
|
+
|
32
|
+
ident.scan(/#[^\s\}]+|\.[^\s\}]+|[^\s\}]+="[^"]+"/) do | match |
|
33
|
+
if match.start_with? "#"
|
34
|
+
match[0] = ''
|
35
|
+
i[:id] = match
|
36
|
+
elsif match.start_with? "."
|
37
|
+
match[0] = ''
|
38
|
+
i[:classes] << match
|
39
|
+
else
|
40
|
+
parts = match.split '='
|
41
|
+
i[parts[0].to_sym] = parts[1].gsub(/^"/, '').gsub(/"$/, '')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return i
|
45
|
+
end
|
46
|
+
|
47
|
+
def format_image(ident, sizes, original_width, url, caption, title, baseurl, base_image_path)
|
48
|
+
srcset = sizes.map {|s| "#{baseurl}/#{base_image_path}/#{s}/#{url} #{s}w"}
|
49
|
+
srcset << "#{baseurl}/#{base_image_path}/#{url} #{original_width}w"
|
50
|
+
srcset = srcset.join ", "
|
51
|
+
classes = "image #{ident[:dimension]} #{ident[:classes].join " "}"
|
52
|
+
content = "<div class=\"#{classes}\""
|
53
|
+
if not ident[:style].nil?
|
54
|
+
content += " style=\"#{ident[:style]}\""
|
55
|
+
end
|
56
|
+
content += ">"
|
57
|
+
content += "<div>"
|
58
|
+
srcset_sizes = if sizes.length > 0
|
59
|
+
sizes.map do | size |
|
60
|
+
"(min-width: #{(size*1.16).floor}) #{size}px"
|
61
|
+
end.join(", ") + ", 100vw"
|
62
|
+
elsif not ident[:sizes].nil? and ident[:sizes].length > 0
|
63
|
+
ident[:sizes]
|
64
|
+
else
|
65
|
+
"(min-width: 1180px) 1024px, (min-width: 980px) 844px, (min-width: 740px) 640, 100vw"
|
66
|
+
end
|
67
|
+
if not caption.nil? and caption != ""
|
68
|
+
content += "<figure>
|
69
|
+
<img
|
70
|
+
src = \"#{baseurl}/#{base_image_path}/#{sizes[0]}/#{url}\"
|
71
|
+
alt = \"#{title}\"
|
72
|
+
sizes = \"#{srcset_sizes}\"
|
73
|
+
srcset = \"#{srcset}\"
|
74
|
+
/>
|
75
|
+
<figcaption>#{caption}</figcaption>
|
76
|
+
</figure>"
|
77
|
+
else
|
78
|
+
content += "<img
|
79
|
+
src = \"#{baseurl}/#{base_image_path}/#{sizes[0]}/#{url}\"
|
80
|
+
alt = \"#{title}\"
|
81
|
+
sizes = \"#{srcset_sizes}\"
|
82
|
+
srcset = \"#{srcset}\"
|
83
|
+
/>"
|
84
|
+
end
|
85
|
+
content += "</div>"
|
86
|
+
content += "</div>"
|
87
|
+
return content
|
88
|
+
end
|
89
|
+
|
90
|
+
def format_svg_image(url, ident, title, baseurl, base_image_path)
|
91
|
+
content = "<img "
|
92
|
+
if not ident[:style].nil?
|
93
|
+
content += "style=\"#{ident[:style]}\" "
|
94
|
+
end
|
95
|
+
if not ident[:classes].empty?
|
96
|
+
content += "class=\"#{ident[:classes].join " "}\" "
|
97
|
+
end
|
98
|
+
content += "src = \"#{baseurl}/#{base_image_path}/#{url}\" "
|
99
|
+
content += "alt = \"#{title}\""
|
100
|
+
content += "/>"
|
101
|
+
end
|
102
|
+
|
103
|
+
def process_page(document, payload)
|
104
|
+
# see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841
|
105
|
+
doc_ext = document.extname.tr('.', '')
|
106
|
+
if payload['site']['markdown_ext'].include? doc_ext
|
107
|
+
content = document.content
|
108
|
+
base_image_path = document.site.config["base_image_path"] || "assets/images"
|
109
|
+
image_regex = /!\[(?<caption>.*?)?\]\((?<url>\S+?)(?<title> .+?)?\)(?<identifier>\{.+?\})?/
|
110
|
+
content = content.gsub(image_regex) do | img |
|
111
|
+
img = image_regex.match img
|
112
|
+
if (/\.(jpe?g|png)$/i =~ img["url"]).nil?
|
113
|
+
"#{base_image_path}/#{img[0]}"
|
114
|
+
elsif img["url"].start_with? "http"
|
115
|
+
img[0]
|
116
|
+
elsif img["url"].end_with? ".svg"
|
117
|
+
ident = parse_identifier img[:identifier]
|
118
|
+
format_svg_image(img["url"], ident, img["title"],
|
119
|
+
document.site.baseurl)
|
120
|
+
else
|
121
|
+
if File.exists?(File.join base_image_path, img['url'])
|
122
|
+
widths, original_width = resize_image(img['url'],
|
123
|
+
document.site.config["widths"],
|
124
|
+
document.site.config["destination"],
|
125
|
+
base_image_path)
|
126
|
+
document.site.keep_files.concat(widths.map do |w|
|
127
|
+
File.join(base_image_path, "#{w}", img['url'])
|
128
|
+
end)
|
129
|
+
ident = parse_identifier img[:identifier]
|
130
|
+
format_image(ident, widths, original_width,
|
131
|
+
img["url"], img["caption"], img["title"],
|
132
|
+
document.site.baseurl, base_image_path)
|
133
|
+
else
|
134
|
+
img[0]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
# puts content
|
139
|
+
document.content = content
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|
|
144
|
+
process_page(document, payload)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Jekyll::Hooks.register [:pages, :posts, :documents], :post_render do |document, payload|
|
148
|
+
# # puts "Post"
|
149
|
+
# # puts document
|
150
|
+
# process_page(document, payload)
|
151
|
+
# end
|
152
|
+
|
153
|
+
def check_in_context(context, path)
|
154
|
+
parts = path.split(".")
|
155
|
+
c = context.dup
|
156
|
+
parts.each do | part |
|
157
|
+
if not c.is_a?(Array) and c.key? part
|
158
|
+
c = c[part]
|
159
|
+
else
|
160
|
+
return nil
|
161
|
+
end
|
162
|
+
end
|
163
|
+
return c
|
164
|
+
end
|
165
|
+
|
166
|
+
module Jekyll
|
167
|
+
class ImgSrcsetTag < Liquid::Tag
|
168
|
+
|
169
|
+
def initialize(tag_name, text, tokens)
|
170
|
+
super
|
171
|
+
@text = text
|
172
|
+
end
|
173
|
+
|
174
|
+
def render(context)
|
175
|
+
config = context.registers[:site].config
|
176
|
+
base_image_path = config["base_image_path"] || "assets/images"
|
177
|
+
attrs = {
|
178
|
+
classes: [],
|
179
|
+
}
|
180
|
+
[/(\S+)="([^"]+?)"/, /(\S+)=([^\s\}"][^\s\}]+)/].each do | attr_regex |
|
181
|
+
@text.scan(attr_regex) do | key, value |
|
182
|
+
if key == "class"
|
183
|
+
context_content = check_in_context context, value
|
184
|
+
if context_content.nil?
|
185
|
+
attrs[:classes] << value
|
186
|
+
else
|
187
|
+
attrs[:classes] << context_content
|
188
|
+
end
|
189
|
+
else
|
190
|
+
context_content = check_in_context context, value
|
191
|
+
if context_content.nil?
|
192
|
+
attrs[key.to_sym] = value
|
193
|
+
else
|
194
|
+
attrs[key.to_sym] = context_content
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
#puts "fs context: #{context.find_variable('first_image')}"
|
201
|
+
# puts "Prepare image: #{attrs[:src]}"
|
202
|
+
if File.exists?(File.join base_image_path, attrs[:src])
|
203
|
+
widths, original_width = resize_image(
|
204
|
+
attrs[:src],
|
205
|
+
config["widths"],
|
206
|
+
config["destination"], base_image_path)
|
207
|
+
context.registers[:site].keep_files.concat(widths.map do |w|
|
208
|
+
File.join(base_image_path, "#{w}", attrs[:src])
|
209
|
+
end)
|
210
|
+
format_image(attrs, widths, original_width,
|
211
|
+
attrs[:src], attrs[:caption], attrs[:title],
|
212
|
+
context.registers[:site].baseurl, base_image_path)
|
213
|
+
else
|
214
|
+
puts "image not found: #{File.join base_image_path, attrs[:src]} in page at url #{context["page"]["url"]}"
|
215
|
+
""
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
Liquid::Template.register_tag('imgsrcset', Jekyll::ImgSrcsetTag)
|
222
|
+
|
223
|
+
|
224
|
+
# /![(?<caption>.*?)?]\((?<url>[^\s]+?)(?<title> .+?)?\)(?<identifier>{.+?})?/
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll-img-srcset
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan-Philipp Stauffert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jekyll
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mini_magick
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.9'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- jan-philipp.stauffert@uni-wuerzburg.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/jekyll-img-srcset.rb
|
77
|
+
homepage: https://gitlab2.informatik.uni-wuerzburg.de/hci-development/jekyll-img-srcset
|
78
|
+
licenses:
|
79
|
+
- Apache-2.0
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.0.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Resizes images and puts all sizes into a srcset.
|
100
|
+
test_files: []
|