Class: Microformats2::FormatParser

Inherits:
Object
  • Object
show all
Defined in:
lib/microformats2/format_parser.rb

Class Method Summary collapse

Class Method Details

.constant_name(html_class) ⇒ Object



52
53
54
55
# File 'lib/microformats2/format_parser.rb', line 52

def constant_name(html_class)
  # html-Class -> html-class -> html_class -> Html_class -> HtmlClass
  html_class.downcase.gsub("-","_").gsub(/^([a-z])/){$1.upcase}.gsub(/_(.)/){$1.upcase}
end

.find_or_create_ruby_class(const_name) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/microformats2/format_parser.rb', line 57

def find_or_create_ruby_class(const_name)
  if Object.const_defined?(const_name)
    klass = Object.const_get(const_name)
  else
    klass = Class.new(Microformats2::Format)
    Object.const_set const_name, klass
  end
  klass
end

.format_classes(element) ⇒ Object



46
47
48
49
50
# File 'lib/microformats2/format_parser.rb', line 46

def format_classes(element)
  element.attribute("class").to_s.split.select do |html_class|
    html_class =~ Format::CLASS_REG_EXP
  end
end

.parse(element, base = nil, parsing_children = false) ⇒ Object



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

def parse(element, base=nil, parsing_children = false)
  @@base = base
  parse_node(element, parsing_children).flatten.compact
end

.parse_for_microformats(element, parsing_children = false) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/microformats2/format_parser.rb', line 21

def parse_for_microformats(element, parsing_children=false)
  if property_classes(element).length >= 1 and parsing_children
    #do nothing because we don't want properties obj in children
  elsif format_classes(element).length >= 1
    parse_microformat(element)
  else
    parse_nodeset(element.children, parsing_children)
  end
end

.parse_microformat(element) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/microformats2/format_parser.rb', line 31

def parse_microformat(element)
  # only create ruby object for first format class
  html_class = format_classes(element).first
  const_name = constant_name(html_class)
  klass = find_or_create_ruby_class(const_name)

  klass.new(element, @@base).parse
end

.parse_node(node, parsing_children = false) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/microformats2/format_parser.rb', line 9

def parse_node(node, parsing_children=false)
  case
  when node.is_a?(Nokogiri::HTML::Document) then parse_node(node.children, parsing_children)
  when node.is_a?(Nokogiri::XML::NodeSet)   then parse_nodeset(node, parsing_children)
  when node.is_a?(Nokogiri::XML::Element)   then [parse_for_microformats(node, parsing_children)]
  end
end

.parse_nodeset(nodeset, parsing_children = false) ⇒ Object



17
18
19
# File 'lib/microformats2/format_parser.rb', line 17

def parse_nodeset(nodeset, parsing_children=false)
  nodeset.map { |node| parse_node(node, parsing_children) }
end

.property_classes(element) ⇒ Object



40
41
42
43
44
# File 'lib/microformats2/format_parser.rb', line 40

def property_classes(element)
  element.attribute("class").to_s.split.select do |html_class|
    html_class =~ Property::CLASS_REG_EXP
  end
end