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.



169
170
171
172
# File 'lib/jekyll-img-srcset.rb', line 169

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

Instance Method Details

#render(context) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
# File 'lib/jekyll-img-srcset.rb', line 174

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

  #puts "fs context: #{context.find_variable('first_image')}"
  # puts "Prepare image: #{attrs[:src]}"
  if File.exists?(File.join base_image_path, attrs[:src])
      widths, original_width = resize_image(
          attrs[:src], 
          config["widths"],
          config["destination"], base_image_path)
      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