simplifies properties

This commit is contained in:
Jessica Lynn Suttles 2013-02-12 17:26:12 -08:00
parent 915250f5b0
commit 113d95af17
7 changed files with 57 additions and 110 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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