
Take some code that was duplicated in both the PropertyParser and Format classes and move into a method in the Property module.
49 lines
1.3 KiB
Ruby
49 lines
1.3 KiB
Ruby
module Microformats2
|
|
class PropertyParser
|
|
class << self
|
|
def parse(element)
|
|
parse_node(element).flatten.compact
|
|
end
|
|
|
|
def parse_node(node)
|
|
case
|
|
when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node)
|
|
when node.is_a?(Nokogiri::XML::Element) then [parse_for_properties(node)]
|
|
end
|
|
end
|
|
|
|
def parse_nodeset(nodeset)
|
|
nodeset.map { |node| parse_node(node) }
|
|
end
|
|
|
|
def parse_for_properties(element)
|
|
if property_classes(element).length >= 1
|
|
parse_property(element)
|
|
else
|
|
parse_nodeset(element.children)
|
|
end
|
|
end
|
|
|
|
def parse_property(element)
|
|
property_classes(element).map do |property_class|
|
|
property = Property.new(element, property_class).parse
|
|
properties = format_classes(element).empty? ? PropertyParser.parse(element.children) : []
|
|
|
|
[property].concat properties
|
|
end
|
|
end
|
|
|
|
def property_classes(element)
|
|
element.attribute("class").to_s.split.select do |html_class|
|
|
html_class =~ Property::CLASS_REG_EXP
|
|
end
|
|
end
|
|
|
|
def format_classes(element)
|
|
element.attribute("class").to_s.split.select do |html_class|
|
|
html_class =~ Format::CLASS_REG_EXP
|
|
end
|
|
end
|
|
end # class << self
|
|
end
|
|
end
|