Method: Microformats::FormatParser#parse

Defined in:
lib/microformats/format_parser.rb

#parse(element, base: nil, element_type: nil, format_class_array: [], backcompat: false) ⇒ Object



3
4
5
6
7
8
9
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
43
44
45
46
47
48
49
50
51
52
# File 'lib/microformats/format_parser.rb', line 3

def parse(element, base: nil, element_type: nil, format_class_array: [], backcompat: false)
  @base = base

  @mode_backcompat = backcompat

  @properties = {}
  @children = []
  @found_prefixes = Set.new

  @format_property_type = element_type
  @value = nil

  @mode_backcompat = backcompat

  @fmt_classes = format_class_array

  parse_node(element.children)

  # check properties for any missing h-* so we know not to imply anything
  check_for_h_properties

  imply_properties(element)

  if @value.nil? || @value.empty?
    if element_type == 'p' && !@properties['name'].nil? && !@properties['name'].empty?
      @value = @properties['name'].first
    elsif element_type == 'u' && !@properties['url'].nil? && !@properties['url'].empty?
      @value = @properties['url'].first
    elsif !element_type.nil?
      @value = PropertyParser.new.parse(element, base: @base, element_type: element_type, backcompat: @mode_backcompat)
    end
  end

  h_object = {}

  h_object['value'] = @value unless @value.nil?
  h_object['type'] = format_class_array
  h_object['properties'] = @properties

  h_object['children'] = @children unless @children.empty?

  if @format_property_type == 'e'
    h_object['value'] = render_text(element)
    h_object['html'] = element.inner_html.strip
  end

  # TODO: fall back to p- dt- u- parsing if value still not set?
  # not sure that is correct by the spec actually
  h_object
end