microformats2/lib/microformats2/property_parser.rb
Michael Mitchell + Chris Stringer 65fb44239c DRY up the logic to create new property classes.
Take some code that was duplicated in both the PropertyParser and
Format classes and move into a method in the Property module.
2013-04-18 22:13:38 -07:00

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