Class: Microformats::FormatParser

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

Constant Summary

Constants inherited from ParserCore

ParserCore::FORMAT_CLASS_REG_EXP, ParserCore::PROPERTY_CLASS_REG_EXP, ParserCore::VALUE_CLASS_REG_EXP, 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

#check_for_h_propertiesObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/microformats/format_parser.rb', line 118

def check_for_h_properties
  @properties.each do |_, prop|
    prop.each do |prop_entry|
      next unless prop_entry.respond_to?(:key) && prop_entry.key?('type')
      next if prop_entry['type'].nil?

      prop_entry['type'].each do |type|
        @found_prefixes.add(prefix_from_class(type).to_sym)
      end
    end
  end
end

#imply_datesObject

imply date for dt-end if dt-start is defined with a date ###



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/microformats/format_parser.rb', line 323

def imply_dates
  return unless !@properties['end'].nil? && !@properties['start'].nil?

  start_date = nil

  @properties['start'].each do |start_val|
    if start_val =~ /^(\d{4}-[01]\d-[0-3]\d)/
      start_date = Regexp.last_match(1) if start_date.nil?
    elsif start_val =~ /^(\d{4}-[0-3]\d\d)/
      start_date = Regexp.last_match(1) if start_date.nil?
    end
  end

  unless start_date.nil?
    @properties['end'].map! do |end_val|
      if end_val.match?(/^\d{4}-[01]\d-[0-3]\d/)
        end_val
      elsif end_val.match?(/^\d{4}-[0-3]\d\d/)
        end_val
      else
        start_date + ' ' + end_val
      end
    end
  end
end

#imply_name(element) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/microformats/format_parser.rb', line 131

def imply_name(element)
  return unless @properties['name'].nil? && !@found_prefixes.include?(:e) && !@found_prefixes.include?(:p) && !@found_prefixes.include?(:h) && @children.empty?

  if element.name == 'img' && !element.attribute('alt').nil?
    @properties['name'] = [element.attribute('alt').value.strip]
  elsif element.name == 'area' && !element.attribute('alt').nil?
    @properties['name'] = [element.attribute('alt').value.strip]
  elsif element.name == 'abbr' && !element.attribute('title').nil?
    @properties['name'] = [element.attribute('title').value.strip]
  else
    child_nodes = element.children.reject { |n| n.is_a?(Nokogiri::XML::Text) }

    if child_nodes.count == 1 && child_nodes.first.is_a?(Nokogiri::XML::Element) && format_classes(child_nodes.first).empty?
      node = child_nodes.first

      # else if .h-x>img:only-child[alt]:not([alt=""]):not[.h-*] then use that img's alt for name
      if node.name == 'img' && !node.attribute('alt').nil? && !node.attribute('alt').value.empty?
        @properties['name'] = [node.attribute('alt').value.strip]
      # else if .h-x>area:only-child[alt]:not([alt=""]):not[.h-*] then use that area's alt for name
      elsif node.name == 'area' && !node.attribute('alt').nil? && !node.attribute('alt').value.empty?
        @properties['name'] = [node.attribute('alt').value.strip]
      # else if .h-x>abbr:only-child[title]:not([title=""]):not[.h-*] then use that abbr title for name
      elsif node.name == 'abbr' && !node.attribute('title').nil? && !node.attribute('title').value.empty?
        @properties['name'] = [node.attribute('title').value.strip]
      else
        child_nodes = node.children.reject { |n| n.is_a?(Nokogiri::XML::Text) }

        if child_nodes.count == 1 && child_nodes.first.is_a?(Nokogiri::XML::Element) && format_classes(child_nodes.first).empty?
          node = child_nodes.first

          # else if .h-x>:only-child:not[.h-*]>img:only-child[alt]:not([alt=""]):not[.h-*] then use that img's alt for name
          if node.name == 'img' && !node.attribute('alt').nil? && !node.attribute('alt').value.empty?
            @properties['name'] = [node.attribute('alt').value.strip]
          # else if .h-x>:only-child:not[.h-*]>area:only-child[alt]:not([alt=""]):not[.h-*] then use that area's alt for name
          elsif node.name == 'area' && !node.attribute('alt').nil? && !node.attribute('alt').value.empty?
            @properties['name'] = [node.attribute('alt').value.strip]
          # else if .h-x>:only-child:not[.h-*]>abbr:only-child[title]:not([title=""]):not[.h-*] use that abbr's title for name
          elsif node.name == 'abbr' && !node.attribute('title').nil? && !node.attribute('title').value.empty?
            @properties['name'] = [node.attribute('title').value.strip]
          else
            @properties['name'] = [render_text(element)]
          end
        else
          @properties['name'] = [render_text(element)]
        end
      end
    else
      @properties['name'] = [render_text(element)]
    end
  end
end

#imply_photo(element) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/microformats/format_parser.rb', line 183

def imply_photo(element)
  return unless @properties['photo'].nil?

  if element.name == 'img' && !element.attribute('src').nil?
    @properties['photo'] = [element.attribute('src').value]
  elsif element.name == 'object' && !element.attribute('data').nil?
    @properties['photo'] = [element.attribute('data').value]
  else
    # else if .h-x>img[src]:only-of-type:not[.h-*] then use that img src for photo
    child_img_tags_with_src = element.children.select do |child|
      child.is_a?(Nokogiri::XML::Element) && child.name == 'img' && !child.attribute('src').nil?
    end

    if child_img_tags_with_src.count == 1
      node = child_img_tags_with_src.first

      @properties['photo'] = [node.attribute('src').value.strip] if format_classes(node).empty?
    end

    if @properties['photo'].nil?
      # else if .h-x>object[data]:only-of-type:not[.h-*] then use that object's data for photo
      child_object_tags_with_data = element.children.select do |child|
        child.is_a?(Nokogiri::XML::Element) && child.name == 'object' && !child.attribute('data').nil?
      end

      if child_object_tags_with_data.count == 1
        node = child_object_tags_with_data.first

        if format_classes(node).empty?
          @properties['photo'] = [node.attribute('data').value.strip]
        end
      end
    end

    child_elements = element.children.reject { |child| child.is_a?(Nokogiri::XML::Text) }

    if @properties['photo'].nil? && child_elements.count == 1 && format_classes(child_elements.first).empty?
      # else if .h-x>:only-child:not[.h-*]>img[src]:only-of-type:not[.h-*], then use that img's src for photo
      child_img_tags_with_src = child_elements.first.children.select do |child|
        child.is_a?(Nokogiri::XML::Element) && child.name == 'img' && !child.attribute('src').nil?
      end

      if child_img_tags_with_src.count == 1
        node = child_img_tags_with_src.first

        if format_classes(node).empty?
          @properties['photo'] = [node.attribute('src').value.strip]
        end
      end

      if @properties['photo'].nil?
        # else if .h-x>:only-child:not[.h-*]>object[data]:only-of-type:not[.h-*], then use that object's data for photo
        child_object_tags_with_data = child_elements.first.children.select do |child|
          child.is_a?(Nokogiri::XML::Element) && child.name == 'object' && !child.attribute('data').nil?
        end

        if child_object_tags_with_data.count == 1
          node = child_object_tags_with_data.first

          if format_classes(node).empty?
            @properties['photo'] = [node.attribute('data').value.strip]
          end
        end
      end
    end
  end

  @properties['photo'] = [Microformats::AbsoluteUri.new(@properties['photo'].first, base: @base).absolutize] unless @properties['photo'].nil?
end

#imply_properties(element) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/microformats/format_parser.rb', line 349

def imply_properties(element)
  ##### Implied Properties ######
  # NOTE: much of this code may be simplified by using element.css, not sure yet, but coding to have passing tests first
  # can optimize this later
  unless @mode_backcompat
    imply_name(element)
    imply_photo(element)
    imply_url(element)
  end
  ##### END Implied Properties when not in backcompat mode######

  imply_dates
end

#imply_url(element) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/microformats/format_parser.rb', line 253

def imply_url(element)
  return unless @properties['url'].nil? && !@found_prefixes.include?(:u) && !@found_prefixes.include?(:h) && @children.empty?

  if element.name == 'a' && !element.attribute('href').nil?
    @properties['url'] = [element.attribute('href').value]
  elsif element.name == 'area' && !element.attribute('href').nil?
    @properties['url'] = [element.attribute('href').value]
  else
    # else if .h-x>a[href]:only-of-type:not[.h-*], then use that [href] for url
    child_a_tags_with_href = element.children.select do |child|
      child.is_a?(Nokogiri::XML::Element) && child.name == 'a' && !child.attribute('href').nil?
    end

    if child_a_tags_with_href.count == 1
      node = child_a_tags_with_href.first
      @properties['url'] = [node.attribute('href').value.strip] if format_classes(node).empty?
    end

    if @properties['url'].nil?
      # else if .h-x>area[href]:only-of-type:not[.h-*], then use that [href] for url
      child_area_tags_with_href = element.children.select do |child|
        child.is_a?(Nokogiri::XML::Element) && child.name == 'area' && !child.attribute('href').nil?
      end

      if child_area_tags_with_href.count == 1
        node = child_area_tags_with_href.first
        @properties['url'] = [node.attribute('href').value.strip] if format_classes(node).empty?
      end
    end

    child_elements = element.children.reject { |child| child.is_a?(Nokogiri::XML::Text) }

    if @properties['url'].nil? && child_elements.count == 1 && format_classes(child_elements.first).empty?
      child_element = child_elements.first
      # else if .h-x>:only-child:not[.h-*]>a[href]:only-of-type:not[.h-*], then use that [href] for url
      child_a_tags_with_href = child_element.children.select do |child|
        child.is_a?(Nokogiri::XML::Element) && child.name == 'a' && !child.attribute('href').nil?
      end

      if child_a_tags_with_href.count == 1
        node = child_a_tags_with_href.first
        @properties['url'] = [node.attribute('href').value.strip] if format_classes(node).empty?
      end

      if @properties['url'].nil?
        # else if .h-x>:only-child:not[.h-*]>area[href]:only-of-type:not[.h-*], then use that [href] for url
        child_area_tags_with_href = child_element.children.select do |child|
          child.is_a?(Nokogiri::XML::Element) && child.name == 'area' && !child.attribute('href').nil?
        end

        if child_area_tags_with_href.count == 1
          node = child_area_tags_with_href.first
          @properties['url'] = [node.attribute('href').value.strip] if format_classes(node).empty?
        end
      end
    end
  end

  @properties['url'] = [Microformats::AbsoluteUri.new(@properties['url'].first, base: @base).absolutize] unless @properties['url'].nil?
end

#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

#parse_element(element) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/microformats/format_parser.rb', line 54

def parse_element(element)
  prop_classes = property_classes(element)
  prop_classes = backcompat_property_classes(element) if @mode_backcompat

  bc_classes_found = false
  fmt_classes = format_classes(element)

  prop_classes.each do |p_class|
    element_type = prefix_from_class(p_class)
    @found_prefixes.add(element_type.to_sym)
  end
  fmt_classes.each do |p_class|
    element_type = prefix_from_class(p_class)
    @found_prefixes.add(element_type.to_sym)
  end

  if fmt_classes.empty?
    fmt_classes = backcompat_format_classes(element)
    bc_classes_found = true unless fmt_classes.empty?
  end

  if prop_classes.length >= 1
    if fmt_classes.length >= 1
      prop_classes.each do |element_class|
        element_type = prefix_from_class(element_class)
        property_name = property_from_class(element_class)

        parsed_format = FormatParser.new.parse(element, base: @base, element_type: element_type, format_class_array: fmt_classes, backcompat: bc_classes_found)

        if @value.nil?
          if @format_property_type == 'p' && property_name == 'name'
            @value = parsed_format['value']
          # elsif @format_property_type == 'dt' and property_name == '???'
          #   @value = parsed_format['value']
          elsif @format_property_type == 'u' && property_name == 'url'
            @value = parsed_format['value']
          end
        end

        @properties[property_name] = [] if @properties[property_name].nil?
        @properties[property_name] << parsed_format
      end
    else
      prop_classes.each do |element_class|
        element_type = prefix_from_class(element_class)
        property_name = property_from_class(element_class)

        parsed_property = PropertyParser.new.parse(element, base: @base, element_type: element_type, backcompat: @mode_backcompat)

        unless parsed_property.nil?
          @properties[property_name] = [] if @properties[property_name].nil?
          @properties[property_name] << parsed_property
        end
      end

      parse_nodeset(element.children)
    end
  elsif fmt_classes.length >= 1
    @children << FormatParser.new.parse(element, base: @base, format_class_array: fmt_classes, backcompat: bc_classes_found)
  else
    parse_nodeset(element.children)
  end
end

#prefix_from_class(class_name) ⇒ Object



314
315
316
# File 'lib/microformats/format_parser.rb', line 314

def prefix_from_class(class_name)
  class_name.downcase.split('-')[0]
end

#property_from_class(class_name) ⇒ Object



318
319
320
# File 'lib/microformats/format_parser.rb', line 318

def property_from_class(class_name)
  class_name.downcase.split('-')[1..-1].join('-')
end