Class: Jekyll::ImgSrcsetTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-img-srcset.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ ImgSrcsetTag

Returns a new instance of ImgSrcsetTag.



188
189
190
191
# File 'lib/jekyll-img-srcset.rb', line 188

def initialize(tag_name, text, tokens)
  super
  @text = text
end

Instance Method Details

#cacheObject



184
185
186
# File 'lib/jekyll-img-srcset.rb', line 184

def cache
  @@cache ||= Jekyll::Cache.new("jekyll-img-srcset")
end

#render(context) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/jekyll-img-srcset.rb', line 193

def render(context)
  config = context.registers[:site].config
  base_image_path = config["base_image_path"] || "assets/images"
  attrs = {
      classes: [],
  }
  [/(\S+)="([^"]+?)"/, /(\S+)=([^\s\}"][^\s\}]+)/].each do | attr_regex |
      @text.scan(attr_regex) do | key, value |
          if key == "class"
              context_content = check_in_context context, value
              if context_content.nil?
                  attrs[:classes] << value
              else
                  attrs[:classes] << context_content
              end
          else
              context_content = check_in_context context, value
              if context_content.nil?
                  attrs[key.to_sym] = value
              else
                  attrs[key.to_sym] = context_content
              end
          end
      end
  end

  if File.exists?(File.join base_image_path, attrs[:src])
      widths, original_width = resize_image(
          attrs[:src], 
          config["widths"],
          config["destination"], base_image_path, cache)
      context.registers[:site].keep_files.concat(widths.map do |w|
          File.join(base_image_path, "#{w}", attrs[:src])
      end)
      format_image(attrs, widths, original_width,
          attrs[:src], attrs[:caption], attrs[:title], 
          context.registers[:site].baseurl, base_image_path)
  else
      puts "image not found: #{File.join base_image_path, attrs[:src]} in page at url #{context["page"]["url"]}"
      ""
  end
end