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.



201
202
203
204
# File 'lib/jekyll-img-srcset.rb', line 201

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

Instance Method Details

#cacheObject



197
198
199
# File 'lib/jekyll-img-srcset.rb', line 197

def cache
  @@cache ||= Jekyll::Cache.new("Jekyll::ImgSrcset")
end

#render(context) ⇒ Object



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
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/jekyll-img-srcset.rb', line 206

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