Class: Microformats2::Parser

Inherits:
ParserCore show all
Defined in:
lib/microformats2/parser.rb

Constant Summary

Constants inherited from ParserCore

Microformats2::ParserCore::FORMAT_CLASS_REG_EXP, Microformats2::ParserCore::PROPERTY_CLASS_REG_EXP, Microformats2::ParserCore::VALUE_CLASS_REG_EXP, Microformats2::ParserCore::VALUE_TITLE_CLASS_REG_EXP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



5
6
7
8
# File 'lib/microformats2/parser.rb', line 5

def initialize
  @http_headers = {}
  super
end

Instance Attribute Details

#http_bodyObject (readonly)

Returns the value of attribute http_body.



4
5
6
# File 'lib/microformats2/parser.rb', line 4

def http_body
  @http_body
end

#http_headersObject (readonly)

Returns the value of attribute http_headers.



4
5
6
# File 'lib/microformats2/parser.rb', line 4

def http_headers
  @http_headers
end

Instance Method Details

#parse(html, base: nil, headers: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/microformats2/parser.rb', line 10

def parse(html, base: nil, headers:{})

  @http_headers = {}

  @items = []
  @rels = {}
  @rel_urls = {}

  @alternates = []

  @base = base

  html = read_html(html, headers: headers)
  document = Nokogiri::HTML(html)

  found_base = parse_base(document)
  @base = found_base unless found_base.nil?

  document.traverse do |node|
    if not node.attribute('src').nil?
      absolute_url = Microformats2::AbsoluteUri.new(node.attribute('src').value.to_s, base: @base).absolutize
      node.attribute('src').value = absolute_url

    elsif not node.attribute('href').nil?
      absolute_url = Microformats2::AbsoluteUri.new(node.attribute('href').value.to_s, base: @base).absolutize
      node.attribute('href').value = absolute_url
    end
  end
  parse_node(document)
  parse_rels(document)

  Collection.new({'items' => @items, 'rels' => @rels, 'rel-urls' =>  @rel_urls})
end

#read_html(html, headers: {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/microformats2/parser.rb', line 44

def read_html(html, headers:{})
  open(html, headers) do |response|
    @http_headers = response.meta if response.respond_to?(:meta)
    @http_body = response.read
  end
  @http_body
rescue Errno::ENOENT, Errno::ENAMETOOLONG => e
  @http_body = html
end