Top Level Namespace
Defined Under Namespace
Modules: Jekyll
Instance Method Summary collapse
-
#check_in_context(context, path) ⇒ Object
Jekyll::Hooks.register [:pages, :posts, :documents], :post_render do |document, payload| # puts “Post” # puts document process_page(document, payload) end.
- #format_image(ident, sizes, original_width, url, caption, title, baseurl, base_image_path) ⇒ Object
- #format_svg_image(url, ident, title, baseurl, base_image_path) ⇒ Object
- #parse_identifier(ident) ⇒ Object
- #process_page(document, payload) ⇒ Object
- #resize_image(url, widths, dest, base_image_path) ⇒ Object
Instance Method Details
#check_in_context(context, path) ⇒ Object
Jekyll::Hooks.register [:pages, :posts, :documents], :post_render do |document, payload|
# puts "Post"
# puts document
process_page(document, payload)
end
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/jekyll-img-srcset.rb', line 153 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) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-img-srcset.rb', line 47 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 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 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}\" /> <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) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jekyll-img-srcset.rb', line 90 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) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll-img-srcset.rb', line 23 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 |
#process_page(document, payload) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/jekyll-img-srcset.rb', line 103 def process_page(document, payload) # see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841 doc_ext = document.extname.tr('.', '') 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) 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 |
#resize_image(url, widths, dest, base_image_path) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jekyll-img-srcset.rb', line 3 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) end image.write target end return new_widths, w end |