Class: Microformats::Parser

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

Constant Summary

Constants inherited from ParserCore

Microformats::ParserCore::FORMAT_CLASS_REG_EXP, Microformats::ParserCore::PROPERTY_CLASS_REG_EXP, Microformats::ParserCore::VALUE_CLASS_REG_EXP, Microformats::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/microformats/parser.rb', line 5

def initialize
  @http_headers = {}
  super
end

Instance Attribute Details

#http_bodyObject (readonly)

Returns the value of attribute http_body.



3
4
5
# File 'lib/microformats/parser.rb', line 3

def http_body
  @http_body
end

#http_headersObject (readonly)

Returns the value of attribute http_headers.



3
4
5
# File 'lib/microformats/parser.rb', line 3

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
# File 'lib/microformats/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 !node.attribute('src').nil?
      absolute_url = Microformats::AbsoluteUri.new(node.attribute('src').value.to_s, base: @base).absolutize
      node.attribute('src').value = absolute_url.to_s
    elsif !node.attribute('href').nil?
      absolute_url = Microformats::AbsoluteUri.new(node.attribute('href').value.to_s, base: @base).absolutize
      node.attribute('href').value = absolute_url.to_s
    end
  end

  parse_node(document)
  parse_rels(document)

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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/microformats/parser.rb', line 43

def read_html(html, headers: {})
  stripped_html = html.strip

  (URI.respond_to?(:open) ? URI : Kernel).open(stripped_html, **headers) do |response|
    @http_headers = response.meta if response.respond_to?(:meta)
    @http_body = response.read
  end

  @base = stripped_html if @base.nil?

  @http_body
rescue Errno::ENOENT, Errno::ENAMETOOLONG
  @http_body = html
end