From 915250f5b067d8c83675cd062d6c6151e557c103 Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Tue, 12 Feb 2013 16:54:13 -0800 Subject: [PATCH] implements property parsers as defined in wiki --- lib/microformats2.rb | 5 +++ lib/microformats2/property.rb | 53 ------------------------- lib/microformats2/property/date_time.rb | 47 ++++++++++++++++++++++ lib/microformats2/property/embedded.rb | 9 +++++ lib/microformats2/property/parser.rb | 27 +++++++++++++ lib/microformats2/property/text.rb | 38 ++++++++++++++++++ lib/microformats2/property/url.rb | 40 +++++++++++++++++++ 7 files changed, 166 insertions(+), 53 deletions(-) create mode 100644 lib/microformats2/property/date_time.rb create mode 100644 lib/microformats2/property/embedded.rb create mode 100644 lib/microformats2/property/parser.rb create mode 100644 lib/microformats2/property/text.rb create mode 100644 lib/microformats2/property/url.rb diff --git a/lib/microformats2.rb b/lib/microformats2.rb index c0afa50..a693420 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -5,6 +5,11 @@ require "microformats2/version" require "microformats2/parser" require "microformats2/collection" require "microformats2/format" +require "microformats2/property/parser" +require "microformats2/property/text" +require "microformats2/property/url" +require "microformats2/property/date_time" +require "microformats2/property/embedded" require "microformats2/property" module Microformats2 diff --git a/lib/microformats2/property.rb b/lib/microformats2/property.rb index ee2208e..a217631 100644 --- a/lib/microformats2/property.rb +++ b/lib/microformats2/property.rb @@ -1,58 +1,5 @@ module Microformats2 module Property - class Parser < Microformats2::Parser - attr_accessor :value - - def parse(element, format_classes=[]) - 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 hash_safe_value - @value - end - end - - class Text < Property::Parser - def parse_flat_element(element) - element.text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip - end - end - - class Url < Property::Parser - def parse_flat_element(element) - (element.attribute("href") || element.attribute("src") || element.text).to_s - end - end - - class DateTime < Property::Parser - def parse_flat_element(element) - ::DateTime.parse(element.attribute("datetime") || element.text) - rescue ArgumentError => e - (element.attribute("datetime") || element.text).to_s - end - def hash_safe_value - @value.to_s - end - end - - class Embedded < Property::Parser - def parse_flat_element(element) - element.inner_html.strip - end - end - Parsers = { "p" => Text, "u" => Url, diff --git a/lib/microformats2/property/date_time.rb b/lib/microformats2/property/date_time.rb new file mode 100644 index 0000000..fae9556 --- /dev/null +++ b/lib/microformats2/property/date_time.rb @@ -0,0 +1,47 @@ +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 + end + + class Element < Struct.new(:element) + 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 + end + end + end +end diff --git a/lib/microformats2/property/embedded.rb b/lib/microformats2/property/embedded.rb new file mode 100644 index 0000000..2afd448 --- /dev/null +++ b/lib/microformats2/property/embedded.rb @@ -0,0 +1,9 @@ +module Microformats2 + module Property + class Embedded < Property::Parser + def parse_flat_element(element) + element.inner_html.strip + end + end + end +end diff --git a/lib/microformats2/property/parser.rb b/lib/microformats2/property/parser.rb new file mode 100644 index 0000000..770ce1f --- /dev/null +++ b/lib/microformats2/property/parser.rb @@ -0,0 +1,27 @@ +module Microformats2 + module Property + class Parser < Microformats2::Parser + attr_accessor :value + + def parse(element, format_classes=[]) + 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 hash_safe_value + @value + end + end + end +end diff --git a/lib/microformats2/property/text.rb b/lib/microformats2/property/text.rb new file mode 100644 index 0000000..0dd93e8 --- /dev/null +++ b/lib/microformats2/property/text.rb @@ -0,0 +1,38 @@ +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 = { + "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 + end + end + end +end diff --git a/lib/microformats2/property/url.rb b/lib/microformats2/property/url.rb new file mode 100644 index 0000000..bd7bae8 --- /dev/null +++ b/lib/microformats2/property/url.rb @@ -0,0 +1,40 @@ +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 = { + "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 + end + end + end +end