Module: Jekyll::ResponsiveMagickFilter
- Defined in:
- lib/jekyll-responsive-magick.rb
Constant Summary collapse
- @@sizes =
{}
Instance Method Summary collapse
-
#check_path(input, filter_name) ⇒ Object
Throw error if it is not an absolute path.
- #check_size(input, from) ⇒ Object
-
#convert(src, src_extname, dst, width) ⇒ Object
Convert an image from src to dst with the given width.
- #height(input, from = "height") ⇒ Object
- #identify(input) ⇒ Object
-
#is_image?(src) ⇒ Boolean
Return true if the file exists and is an image mime type.
- #size(input, width) ⇒ Object
- #srcset(input) ⇒ Object
- #width(input, from = "width") ⇒ Object
Instance Method Details
#check_path(input, filter_name) ⇒ Object
Throw error if it is not an absolute path
46 47 48 49 50 |
# File 'lib/jekyll-responsive-magick.rb', line 46 def check_path(input, filter_name) if not input.is_a? String || input.length == 0 || input.chr != '/' throw "#{filter_name}: path must be an absolute path" end end |
#check_size(input, from) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/jekyll-responsive-magick.rb', line 138 def check_size(input, from) check_path(input, from) if not @@sizes[input] identify(input) end end |
#convert(src, src_extname, dst, width) ⇒ Object
Convert an image from src to dst with the given width
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-responsive-magick.rb', line 69 def convert(src, src_extname, dst, width) site = @context.registers[:site] quality = site.config['responsive']['quality'] || 80 verbose = site.config['responsive']['verbose'] || false if File.exist?(dst) and File.mtime(src) < File.mtime(dst) return end FileUtils.mkdir_p(File.dirname(dst)) cmd = "convert #{src_extname == '.apng' ? 'apng:' : ''}#{src.shellescape} -strip -quality #{quality} -resize #{width} #{dst.shellescape}" if verbose print("#{cmd}\n") end if not system(cmd) throw "srcset: failed to execute 'convert', is ImageMagick installed?" end end |
#height(input, from = "height") ⇒ Object
133 134 135 136 |
# File 'lib/jekyll-responsive-magick.rb', line 133 def height(input, from = "height") check_size(input, from) return @@sizes[input][1] end |
#identify(input) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jekyll-responsive-magick.rb', line 27 def identify(input) site = @context.registers[:site] if site.config['responsive']['verbose'] verbose = site.config['responsive']['verbose'] else verbose = false end cmd = "identify -ping -format '%w,%h' .#{input.shellescape}" if verbose print("#{cmd}\n") end sizes = `#{cmd}` if not $?.success? throw "width/height: failed to execute 'identity', is ImageMagick installed?" end @@sizes[input] = sizes.split(',', 2).map!(&:to_i) end |
#is_image?(src) ⇒ Boolean
Return true if the file exists and is an image mime type
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jekyll-responsive-magick.rb', line 53 def is_image?(src) cmd = "identify -ping #{src.shellescape} 2>&1" output = `#{cmd}` if not $?.success? if output.include?("identify: improper image header") throw "file is not an image: '#{src}'" else throw "width/height: failed to execute 'identity', is ImageMagick installed?" end end return true end |
#size(input, width) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jekyll-responsive-magick.rb', line 145 def size(input, width) check_path(input, "size") site = @context.registers[:site] dirname = File.dirname(input) basename = File.basename(input, '.*') extname = File.extname(input) new_extname = site.config['responsive']['format'] ? ".#{site.config['responsive']['format']}" : extname src = ".#{dirname}/#{basename}#{extname}" if not File.exist?(src) or not is_image?(src) throw "srcset: file does not exist or is not an image: '#{src}'" end file = "#{basename}-#{width}w#{new_extname}" dst = "_responsive#{dirname}/#{file}" full_path = "#{dirname}/#{file}" if site.static_files.find{|file| file.path == dst} return full_path # image is already generated end site.static_files << StaticFile.new(site, "_responsive", dirname, file) convert(src, extname, dst, width) return full_path end |
#srcset(input) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/jekyll-responsive-magick.rb', line 90 def srcset(input) check_path(input, "srcset") site = @context.registers[:site] dirname = File.dirname(input) basename = File.basename(input, '.*') extname = File.extname(input) new_extname = site.config['responsive']['format'] ? ".#{site.config['responsive']['format']}" : extname src = ".#{dirname}/#{basename}#{extname}" srcwidth = width(input, "srcset") srcset = ["#{input} #{srcwidth}w"] if not File.exist?(src) or not is_image?(src) throw "srcset: file does not exist or is not an image: '#{src}'" elsif File.extname(src) != '.svg' # as default, use breakpoints of Bootstrap 5 widths = site.config['responsive']['widths'] || [576, 768, 992, 1200, 1400] widths.map do |width| if not srcwidth > width next # image is not large enough to generate a smaller version end file = "#{basename}-#{width}w#{new_extname}" dst = "_responsive#{dirname}/#{file}" srcset.push("#{dirname}/#{file} #{width}w") if site.static_files.find{|file| file.path == dst} next # image is already generated end site.static_files << StaticFile.new(site, "_responsive", dirname, file) convert(src, extname, dst, width) end end return srcset.join(', ') end |
#width(input, from = "width") ⇒ Object
128 129 130 131 |
# File 'lib/jekyll-responsive-magick.rb', line 128 def width(input, from = "width") check_size(input, from) return @@sizes[input][0] end |