Class: JekyllImageData::Crawler

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-image-data/crawler.rb

Instance Method Summary collapse

Instance Method Details

#crawl(content, config) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll-image-data/crawler.rb', line 5

def crawl(content, config)
  images = []
  exclude = config.dig("image_data", "exclude") || []
  html = Nokogiri::HTML(content)
  html.xpath("//img").each do |item|
    src = item.xpath("@src").first
    alt = item.xpath("@alt").first
    excluded = exclude.select do |exclude_item|
      src.value.include?(exclude_item)
    end
    next unless excluded.empty?
    images << {
      "url" => src ? src.content : "",
      "alt" => alt ? alt.content : ""
    }
  end
  images
end