simplifies properties
This commit is contained in:
parent
915250f5b0
commit
113d95af17
7 changed files with 57 additions and 110 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue