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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
182
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
252
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
|
# File 'lib/microformats/format_parser.rb', line 4
def parse(element, base:nil, element_type:nil, format_class_array:[], backcompat:false)
@base = base
@mode_backcompat = backcompat
@properties = {}
@children = []
@format_property_type = element_type
@value = nil
@mode_backcompat = backcompat
@fmt_classes = format_class_array
parse_node(element.children)
unless @mode_backcompat
if @properties['name'].nil?
if element.name == 'img' and not element.attribute('alt').nil?
@properties['name'] = [element.attribute('alt').value.strip]
elsif element.name == 'area' and not element.attribute('alt').nil?
@properties['name'] = [element.attribute('alt').value.strip]
elsif element.name == 'abbr' and not element.attribute('title').nil?
@properties['name'] = [element.attribute('title').value.strip]
else
child_nodes = element.children.select{|n| not n.is_a?(Nokogiri::XML::Text)}
if child_nodes.count == 1 and child_nodes.first.is_a?(Nokogiri::XML::Element) and format_classes(child_nodes.first).empty?
node = child_nodes.first
if node.name == 'img' and not node.attribute('alt').nil? and not node.attribute('alt').value.empty?
@properties['name'] = [node.attribute('alt').value.strip]
elsif node.name == 'area' and not node.attribute('alt').nil? and not node.attribute('alt').value.empty?
@properties['name'] = [node.attribute('alt').value.strip]
elsif node.name == 'abbr' and not node.attribute('title').nil? and not node.attribute('title').value.empty?
@properties['name'] = [node.attribute('title').value.strip]
else
child_nodes = node.children.select{|n| not n.is_a?(Nokogiri::XML::Text)}
if child_nodes.count == 1 and child_nodes.first.is_a?(Nokogiri::XML::Element) and format_classes(child_nodes.first).empty?
node = child_nodes.first
if node.name == 'img' and not node.attribute('alt').nil? and not node.attribute('alt').value.empty?
@properties['name'] = [node.attribute('alt').value.strip]
elsif node.name == 'area' and not node.attribute('alt').nil? and not node.attribute('alt').value.empty?
@properties['name'] = [node.attribute('alt').value.strip]
elsif node.name == 'abbr' and not node.attribute('title').nil? and not node.attribute('title').value.empty?
@properties['name'] = [node.attribute('title').value.strip]
else
@properties['name'] = [element.text.strip]
end
else
@properties['name'] = [element.text.strip]
end
end
else
@properties['name'] = [element.text.strip]
end
end
end
if @properties['photo'].nil?
if element.name == 'img' and not element.attribute('src').nil?
@properties['photo'] = [element.attribute('src').value]
elsif element.name == 'object' and not element.attribute('data').nil?
@properties['photo'] = [element.attribute('data').value]
else
child_img_tags_with_src = element.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'img' and not 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?
child_object_tags_with_data = element.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'object' and not 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.select do |child| not child.is_a?(Nokogiri::XML::Text) end
if @properties['photo'].nil? and child_elements.count == 1 and format_classes(child_elements.first).empty?
child_img_tags_with_src = child_elements.first.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'img' and not 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?
child_object_tags_with_data = child_elements.first.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'object' and not 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
unless @properties['photo'].nil?
@properties['photo'] = [ Microformats::AbsoluteUri.new(@properties['photo'].first, base: @base).absolutize ]
end
end
if @properties['url'].nil?
if element.name == 'a' and not element.attribute('href').nil?
@properties['url'] = [element.attribute('href').value]
elsif element.name == 'area' and not element.attribute('href').nil?
@properties['url'] = [element.attribute('href').value]
else
child_a_tags_with_href = element.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'a' and not child.attribute('href').nil?
end
if child_a_tags_with_href.count == 1
node = child_a_tags_with_href.first
if format_classes(node).empty?
@properties['url'] = [node.attribute('href').value.strip]
end
end
if @properties['url'].nil?
child_area_tags_with_href = element.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'area' and not child.attribute('href').nil?
end
if child_area_tags_with_href.count == 1
node = child_area_tags_with_href.first
if format_classes(node).empty?
@properties['url'] = [node.attribute('href').value.strip]
end
end
end
child_elements = element.children.select do |child| not child.is_a?(Nokogiri::XML::Text) end
if @properties['url'].nil? and child_elements.count == 1 and format_classes(child_elements.first).empty?
child_element = child_elements.first
child_a_tags_with_href = child_element.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'a' and not child.attribute('href').nil?
end
if child_a_tags_with_href.count == 1
node = child_a_tags_with_href.first
if format_classes(node).empty?
@properties['url'] = [node.attribute('href').value.strip]
end
end
if @properties['url'].nil?
child_area_tags_with_href = child_element.children.select do |child|
child.is_a?(Nokogiri::XML::Element) and child.name == 'area' and not child.attribute('href').nil?
end
if child_area_tags_with_href.count == 1
node = child_area_tags_with_href.first
if format_classes(node).empty?
@properties['url'] = [node.attribute('href').value.strip]
end
end
end
end
end
unless @properties['url'].nil?
@properties['url'] = [ Microformats::AbsoluteUri.new(@properties['url'].first, base: @base).absolutize ]
end
end
end
if not @properties['end'].nil? and not @properties['start'].nil?
start_date = nil
@properties['start'].each do |start_val|
if start_val =~ /^(\d{4}-[01]\d-[0-3]\d)/
start_date = $1 if start_date.nil?
elsif start_val =~ /^(\d{4}-[0-3]\d\d)/
start_date = $1 if start_date.nil?
end
end
unless start_date.nil?
@properties['end'].map! do |end_val|
if end_val=~ /^\d{4}-[01]\d-[0-3]\d/
end_val
elsif end_val=~ /^\d{4}-[0-3]\d\d/
end_val
else
start_date + ' ' + end_val
end
end
end
end
if @value.nil? or @value.empty?
if element_type == 'p' and not @properties['name'].nil? and not @properties['name'].empty?
@value = @properties['name'].first
elsif element_type == 'u' and not @properties['url'].nil? and not @properties['url'].empty?
@value = @properties['url'].first
elsif not 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'] = element.text.strip
h_object['html'] = element.inner_html
end
h_object
end
|