jekyll-img-srcset 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jekyll-img-srcset.rb +46 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92a18f3afa30c975e5595fb97644e09667ccb469c19fdb8ffb2641e6a6864b13
|
4
|
+
data.tar.gz: 2c87894a3a7f6f37a2f9757ebe6370818c6f2a1c7aa0514258557d5c8a4f21db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df94acc2e731136259369e950b9067ba4f2582b17a02e18709486346900c956206c24cb7b1c8cd7570367393358ae30aa78f77763e0bfedfbede2ff84345680c
|
7
|
+
data.tar.gz: 23acba98d8129f4f00c475b142fe5a769f565ced4362846cf21868db848218e831a24dc2b47caebffb837f6a3b2d08c8d6a74e142ad3303e8693f95a7c01ca91
|
data/lib/jekyll-img-srcset.rb
CHANGED
@@ -1,25 +1,48 @@
|
|
1
1
|
require 'mini_magick'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
3
|
+
# Resizes an image to provide different versions for multiple
|
4
|
+
# display resolutions
|
5
|
+
#
|
6
|
+
# @param url [String] the image's url
|
7
|
+
# @param widths [Array<Numeric>]
|
8
|
+
# target sizes to resize to;
|
9
|
+
# only widths smaller than the original width are generated
|
10
|
+
# @param base_image_path [String] the base image path. Usually assets/images
|
11
|
+
# @param cache [Jekyll::Cache]
|
12
|
+
# cache object to not resize if the computation was already done
|
13
|
+
# @return [Array<Numeric>, Numeric]
|
14
|
+
# the generated widths and the original iamge width
|
15
|
+
def resize_image(url, widths, dest, base_image_path, cache)
|
16
|
+
src_path = File.join(base_image_path, url)
|
17
|
+
new_widths, w = cache.getset(src_path) do
|
18
|
+
image = MiniMagick::Image.open(src_path)
|
19
|
+
w = image.dimensions[0]
|
20
|
+
aspect = image.dimensions[1].to_f / w
|
21
|
+
[w, aspect]
|
22
|
+
new_widths = widths.select {|x| x <= w}
|
23
|
+
new_widths.map do |width|
|
24
|
+
[width, File.join(dest, base_image_path, "#{width}", url)]
|
25
|
+
end.select do |width, target|
|
26
|
+
not File.exists? target
|
27
|
+
end.each do |width, target|
|
28
|
+
image = MiniMagick::Image.open(src_path)
|
29
|
+
image.resize "#{width}x#{width*aspect}"
|
30
|
+
if not Dir.exists? File.dirname(target)
|
31
|
+
FileUtils.mkdir_p File.dirname(target)
|
32
|
+
end
|
33
|
+
image.write target
|
17
34
|
end
|
18
|
-
|
35
|
+
[new_widths, w]
|
19
36
|
end
|
20
37
|
return new_widths, w
|
21
38
|
end
|
22
39
|
|
40
|
+
# Parses the argument list
|
41
|
+
#
|
42
|
+
# @param ident [String]
|
43
|
+
# the list to parse, e.g., '.someclass key="value"'
|
44
|
+
# @return [Hash]
|
45
|
+
# a hash containing the sorted values
|
23
46
|
def parse_identifier(ident)
|
24
47
|
i = {
|
25
48
|
classes: [],
|
@@ -83,6 +106,7 @@ def format_image(ident, sizes, original_width, url, caption, title, baseurl, bas
|
|
83
106
|
return content
|
84
107
|
end
|
85
108
|
|
109
|
+
# Formats svg image data by providing the associated image tag
|
86
110
|
def format_svg_image(url, ident, title, baseurl, base_image_path)
|
87
111
|
content = "<img "
|
88
112
|
if not ident[:style].nil?
|
@@ -99,6 +123,7 @@ end
|
|
99
123
|
def process_page(document, payload)
|
100
124
|
# see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841
|
101
125
|
doc_ext = document.extname.tr('.', '')
|
126
|
+
cache = Jekyll::Cache.new("jekyll-img-srcset")
|
102
127
|
if payload['site']['markdown_ext'].include? doc_ext
|
103
128
|
content = document.content
|
104
129
|
base_image_path = document.site.config["base_image_path"] || "assets/images"
|
@@ -118,7 +143,7 @@ def process_page(document, payload)
|
|
118
143
|
widths, original_width = resize_image(img['url'],
|
119
144
|
document.site.config["widths"],
|
120
145
|
document.site.config["destination"],
|
121
|
-
base_image_path)
|
146
|
+
base_image_path, cache)
|
122
147
|
document.site.keep_files.concat(widths.map do |w|
|
123
148
|
File.join(base_image_path, "#{w}", img['url'])
|
124
149
|
end)
|
@@ -140,12 +165,6 @@ Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, p
|
|
140
165
|
process_page(document, payload)
|
141
166
|
end
|
142
167
|
|
143
|
-
# Jekyll::Hooks.register [:pages, :posts, :documents], :post_render do |document, payload|
|
144
|
-
# # puts "Post"
|
145
|
-
# # puts document
|
146
|
-
# process_page(document, payload)
|
147
|
-
# end
|
148
|
-
|
149
168
|
def check_in_context(context, path)
|
150
169
|
parts = path.split(".")
|
151
170
|
c = context.dup
|
@@ -161,6 +180,10 @@ end
|
|
161
180
|
|
162
181
|
module Jekyll
|
163
182
|
class ImgSrcsetTag < Liquid::Tag
|
183
|
+
|
184
|
+
def cache
|
185
|
+
@@cache ||= Jekyll::Cache.new("jekyll-img-srcset")
|
186
|
+
end
|
164
187
|
|
165
188
|
def initialize(tag_name, text, tokens)
|
166
189
|
super
|
@@ -193,13 +216,11 @@ module Jekyll
|
|
193
216
|
end
|
194
217
|
end
|
195
218
|
|
196
|
-
#puts "fs context: #{context.find_variable('first_image')}"
|
197
|
-
# puts "Prepare image: #{attrs[:src]}"
|
198
219
|
if File.exists?(File.join base_image_path, attrs[:src])
|
199
220
|
widths, original_width = resize_image(
|
200
221
|
attrs[:src],
|
201
222
|
config["widths"],
|
202
|
-
config["destination"], base_image_path)
|
223
|
+
config["destination"], base_image_path, cache)
|
203
224
|
context.registers[:site].keep_files.concat(widths.map do |w|
|
204
225
|
File.join(base_image_path, "#{w}", attrs[:src])
|
205
226
|
end)
|