Class: Middleman::ImgLoadingAttribute

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-img_loading_attribute/extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ ImgLoadingAttribute

Returns a new instance of ImgLoadingAttribute.



8
9
10
11
12
13
14
15
16
17
# File 'lib/middleman-img_loading_attribute/extension.rb', line 8

def initialize(app, options_hash={}, &block)
  # Call super to build options from the options_hash
  super

  # Require libraries only when activated
  # require 'necessary/library'

  # set up your extension
  # puts options.my_option
end

Instance Method Details

#after_build(builder) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/middleman-img_loading_attribute/extension.rb', line 23

def after_build(builder)
  files = Dir.glob(File.join(app.config[:build_dir], "**", "*.html"))
  files.each do |file|
    doc = Nokogiri::HTML(File.read(file))
    doc.css('img').each do |elem|
      next if elem.path.include?('pre') || elem.path.include?('code') || elem.path.include?('blockquote')

      elem['loading'] = options[:loading]
    end
    File.open(file, 'w') do |f|
      f.write doc.to_html
    end
  end
end

#after_configurationObject



19
20
21
# File 'lib/middleman-img_loading_attribute/extension.rb', line 19

def after_configuration
  # Do something
end