diff --git a/lib/microformats2/format.rb b/lib/microformats2/format.rb index 21a8734..dce5646 100644 --- a/lib/microformats2/format.rb +++ b/lib/microformats2/format.rb @@ -30,7 +30,6 @@ module Microformats2 end def parse_microformat(element, html_classes) - format_classes = html_classes.select { |html_class| html_class =~ /^(h-)/ } property_classes = html_classes.select { |html_class| html_class =~ Microformats2::Property::PrefixesRegEx } property_classes.each do |property_class| @@ -42,7 +41,7 @@ module Microformats2 method_name = "klass" if method_name == "class" # parse property - value = Microformats2::Property::Parsers[prefix].new.parse(element, format_classes) + value = Microformats2::Property::Parsers[prefix].new(element).parse # save property under custom method define_method_and_set_value(method_name, value) diff --git a/lib/microformats2/parser.rb b/lib/microformats2/parser.rb index ffb5b48..0ed9e9f 100644 --- a/lib/microformats2/parser.rb +++ b/lib/microformats2/parser.rb @@ -42,7 +42,7 @@ module Microformats2 value = klass.new.parse(microformat) # save microformat in array in order - @formats << value + formats << value # save microformat under custom method define_method_and_set_value(method_name, value) @@ -77,8 +77,8 @@ module Microformats2 end def save_method_name(method_name) - unless @added_methods.include?(method_name) - @added_methods << method_name + unless added_methods.include?(method_name) + added_methods << method_name end end diff --git a/lib/microformats2/property/date_time.rb b/lib/microformats2/property/date_time.rb index fae9556..6e4d43e 100644 --- a/lib/microformats2/property/date_time.rb +++ b/lib/microformats2/property/date_time.rb @@ -1,46 +1,18 @@ module Microformats2 module Property class DateTime < Property::Parser - def hash_safe_value - @value.to_s - end - - def parse_flat_element(element) - value = Element.new(element).value - begin - ::DateTime.parse(value) - rescue ArgumentError => e - value - end + def value + ::DateTime.parse(super) + rescue ArgumentError => e + super end - class Element < Struct.new(:element) - ATTR_MAP = { + def attr_map + @attr_map ||= { "time" => "datetime", "ins" => "datetime", "abbr" => "title", - "data" => "value" - } - - def value - value_class_pattern || element_value || text_value - end - - def value_class_pattern - # TODO - end - - def element_value - element.attribute(attribute).to_s if attribute - end - - def text_value - element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip - end - - def attribute - ATTR_MAP[element.name] - end + "data" => "value" } end end end diff --git a/lib/microformats2/property/embedded.rb b/lib/microformats2/property/embedded.rb index 2afd448..6efdd6f 100644 --- a/lib/microformats2/property/embedded.rb +++ b/lib/microformats2/property/embedded.rb @@ -1,8 +1,8 @@ module Microformats2 module Property class Embedded < Property::Parser - def parse_flat_element(element) - element.inner_html.strip + def value + @value ||= @element.inner_html.strip end end end diff --git a/lib/microformats2/property/parser.rb b/lib/microformats2/property/parser.rb index 770ce1f..97816ee 100644 --- a/lib/microformats2/property/parser.rb +++ b/lib/microformats2/property/parser.rb @@ -1,26 +1,52 @@ module Microformats2 module Property class Parser < Microformats2::Parser - attr_accessor :value + attr_accessor :value, :element - def parse(element, format_classes=[]) + def initialize(element) + @element = element + super() + end + + def parse + html_classes = element.attribute("class").to_s.split + format_classes = html_classes.select { |html_class| html_class =~ /^(h-)/ } if format_classes.length >= 1 parse_microformat(element, format_classes) end - @value = parse_flat_element(element) self end - def to_hash - if @formats.empty? - hash_safe_value - else - { value: hash_safe_value }.merge @formats.first.to_hash - end - end + def value + @value ||= value_class_pattern || element_value || text_value + end - def hash_safe_value - @value + def value_class_pattern + # TODO + end + + def element_value + element.attribute(attribute).to_s if attribute + end + + def text_value + element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip + end + + def attribute + attr_map[element.name] + end + + def attr_map + {} + end + + def to_hash + if formats.empty? + value.to_s + else + { value: value.to_s }.merge formats.first.to_hash + end end end end diff --git a/lib/microformats2/property/text.rb b/lib/microformats2/property/text.rb index 0dd93e8..1a264e8 100644 --- a/lib/microformats2/property/text.rb +++ b/lib/microformats2/property/text.rb @@ -1,37 +1,12 @@ module Microformats2 module Property class Text < Property::Parser - def parse_flat_element(element) - Element.new(element).value - end - - class Element < Struct.new(:element) - ATTR_MAP = { + def attr_map + @attr_map = { "abbr" => "title", "data" => "value", "img" => "alt", - "area" => "alt" - } - - def value - value_class_pattern || element_value || text_value - end - - def value_class_pattern - # TODO - end - - def element_value - element.attribute(attribute).to_s if attribute - end - - def text_value - element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip - end - - def attribute - ATTR_MAP[element.name] - end + "area" => "alt" } end end end diff --git a/lib/microformats2/property/url.rb b/lib/microformats2/property/url.rb index bd7bae8..5d100da 100644 --- a/lib/microformats2/property/url.rb +++ b/lib/microformats2/property/url.rb @@ -1,39 +1,14 @@ module Microformats2 module Property class Url < Property::Parser - def parse_flat_element(element) - Element.new(element).value - end - - class Element < Struct.new(:element) - ATTR_MAP = { + def attr_map + @attr_map = { "a" => "href", "area" => "href", "img" => "src", "object" => "data", "abbr" => "title", - "data" => "value" - } - - def value - value_class_pattern || element_value || text_value - end - - def value_class_pattern - # TODO - end - - def element_value - element.attribute(attribute).to_s if attribute - end - - def text_value - element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip - end - - def attribute - ATTR_MAP[element.name] - end + "data" => "value" } end end end