Class: Microformats::PropertyParser

Inherits:
ParserCore show all
Defined in:
lib/microformats/property_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 Method Summary collapse

Methods inherited from ParserCore

#initialize

Constructor Details

This class inherits a constructor from Microformats::ParserCore

Instance Method Details

#parse(element, base: nil, element_type:, format_class_array: [], backcompat: nil) ⇒ 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
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/microformats/property_parser.rb', line 3

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

  @fmt_classes = format_class_array
  @mode_backcompat = backcompat

  if element_type == 'p'
    parse_value_class_pattern(element)

    if @value.nil?
      @value =
        if element.name == 'abbr' && !element.attribute('title').nil?
          element.attribute('title').value.strip
        elsif element.name == 'link' && !element.attribute('title').nil?
          element.attribute('title').value.strip
        elsif (element.name == 'data' || element.name == 'input') && !element.attribute('value').nil?
          element.attribute('value').value.strip
        elsif (element.name == 'img' || element.name == 'area') && !element.attribute('alt').nil?
          element.attribute('alt').value.strip
        else
          render_text(element, base: @base)
        end
    end
  elsif element_type == 'e'
    @value = {
      value: render_text(element, base: @base),
      html: element.inner_html.strip
    }
  elsif element_type == 'u'
    if %w[a area link].include?(element.name) && !element.attribute('href').nil?
      @value = element.attribute('href').value.strip
    elsif %w[img audio video source].include?(element.name) && !element.attribute('src').nil?
      @value = element.attribute('src').value.strip
    elsif element.name == 'video' && !element.attribute('poster').nil?
      @value = element.attribute('poster').value.strip
    elsif element.name == 'object' && !element.attribute('data').nil?
      @value = element.attribute('data').value.strip
    end

    if !@value.nil?
      @value = Microformats::AbsoluteUri.new(@value, base: @base).absolutize
    else
      parse_value_class_pattern(element)

      if @value.nil?
        @value =
          if element.name == 'abbr' && !element.attribute('title').nil?
            element.attribute('title').value.strip
          elsif (element.name == 'data' || element.name == 'input') && !element.attribute('value').nil?
            element.attribute('value').value.strip
          else
            render_text(element, base: @base)
          end
      end
    end
  elsif element_type == 'dt'
    @value = Microformats::TimePropertyParser.new.parse(element, base: base, element_type: element_type, format_class_array: format_class_array, backcompat: backcompat)
  end

  @value
end

#parse_element(element) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/microformats/property_parser.rb', line 75

def parse_element(element)
  if value_title_classes(element).length >= 1
    @value_class_pattern_value << element.attribute('title').value.strip
  elsif value_classes(element).length >= 1
    @value_class_pattern_value <<
      if element.name == 'img' || element.name == 'area' && !element.attribute('alt').nil?
        element.attribute('alt').value.strip
      elsif element.name == 'data' && !element.attribute('value').nil?
        element.attribute('value').value.strip
      elsif element.name == 'abbr' && !element.attribute('title').nil?
        element.attribute('title').value.strip
      else
        render_and_strip(element.text.strip)
      end
  else
    p_classes = property_classes(element)
    p_classes = backcompat_property_classes(element) if @mode_backcompat

    if p_classes.empty? && format_classes(element).empty?
      parse_node(element.children)
    end
  end
end

#parse_value_class_pattern(element) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/microformats/property_parser.rb', line 67

def parse_value_class_pattern(element)
  @value_class_pattern_value = []

  parse_node(element.children)

  @value = @value_class_pattern_value.join unless @value_class_pattern_value.empty?
end