Class: JekyllImageData::Crawler

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

Instance Method Summary collapse

Constructor Details

#initializeCrawler

Returns a new instance of Crawler.



3
4
5
6
7
8
9
# File 'lib/jekyll-image-data/crawler.rb', line 3

def initialize
  @alt = %r{!\[(.*)\]}
  @url = %r{(/[\w/.-]*\.(jpg|png|gif))}
  @tag = %r{\[(.*)\]}
  @image = %r{#{@alt}(\(#{@url}\).*|#{@tag})}
  @reference = %r{#{@tag}: *#{@url}}
end

Instance Method Details

#crawl(content) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-image-data/crawler.rb', line 11

def crawl(content)
  images = []

  content.scan(@image) do |match|
    if $3
      images << { "alt" => "#{$1}", "url" => "#{$3}" }
    elsif $5
      images << { "alt" => "#{$1}", "url" => "#{$5}" }
    end
  end

  content.scan(@reference) do |match|
    images.map do |image|
      image["url"] = $2 if image["url"] == $1
    end
  end

  images
end