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
|
end
|
||||||
|
|
||||||
def parse_microformat(element, html_classes)
|
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 = html_classes.select { |html_class| html_class =~ Microformats2::Property::PrefixesRegEx }
|
||||||
|
|
||||||
property_classes.each do |property_class|
|
property_classes.each do |property_class|
|
||||||
|
@ -42,7 +41,7 @@ module Microformats2
|
||||||
method_name = "klass" if method_name == "class"
|
method_name = "klass" if method_name == "class"
|
||||||
|
|
||||||
# parse property
|
# 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
|
# save property under custom method
|
||||||
define_method_and_set_value(method_name, value)
|
define_method_and_set_value(method_name, value)
|
||||||
|
|
|
@ -42,7 +42,7 @@ module Microformats2
|
||||||
value = klass.new.parse(microformat)
|
value = klass.new.parse(microformat)
|
||||||
|
|
||||||
# save microformat in array in order
|
# save microformat in array in order
|
||||||
@formats << value
|
formats << value
|
||||||
|
|
||||||
# save microformat under custom method
|
# save microformat under custom method
|
||||||
define_method_and_set_value(method_name, value)
|
define_method_and_set_value(method_name, value)
|
||||||
|
@ -77,8 +77,8 @@ module Microformats2
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_method_name(method_name)
|
def save_method_name(method_name)
|
||||||
unless @added_methods.include?(method_name)
|
unless added_methods.include?(method_name)
|
||||||
@added_methods << method_name
|
added_methods << method_name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,46 +1,18 @@
|
||||||
module Microformats2
|
module Microformats2
|
||||||
module Property
|
module Property
|
||||||
class DateTime < Property::Parser
|
class DateTime < Property::Parser
|
||||||
def hash_safe_value
|
def value
|
||||||
@value.to_s
|
::DateTime.parse(super)
|
||||||
end
|
rescue ArgumentError => e
|
||||||
|
super
|
||||||
def parse_flat_element(element)
|
|
||||||
value = Element.new(element).value
|
|
||||||
begin
|
|
||||||
::DateTime.parse(value)
|
|
||||||
rescue ArgumentError => e
|
|
||||||
value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Element < Struct.new(:element)
|
def attr_map
|
||||||
ATTR_MAP = {
|
@attr_map ||= {
|
||||||
"time" => "datetime",
|
"time" => "datetime",
|
||||||
"ins" => "datetime",
|
"ins" => "datetime",
|
||||||
"abbr" => "title",
|
"abbr" => "title",
|
||||||
"data" => "value"
|
"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
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module Microformats2
|
module Microformats2
|
||||||
module Property
|
module Property
|
||||||
class Embedded < Property::Parser
|
class Embedded < Property::Parser
|
||||||
def parse_flat_element(element)
|
def value
|
||||||
element.inner_html.strip
|
@value ||= @element.inner_html.strip
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,26 +1,52 @@
|
||||||
module Microformats2
|
module Microformats2
|
||||||
module Property
|
module Property
|
||||||
class Parser < Microformats2::Parser
|
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
|
if format_classes.length >= 1
|
||||||
parse_microformat(element, format_classes)
|
parse_microformat(element, format_classes)
|
||||||
end
|
end
|
||||||
@value = parse_flat_element(element)
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def value
|
||||||
if @formats.empty?
|
@value ||= value_class_pattern || element_value || text_value
|
||||||
hash_safe_value
|
end
|
||||||
else
|
|
||||||
{ value: hash_safe_value }.merge @formats.first.to_hash
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def hash_safe_value
|
def value_class_pattern
|
||||||
@value
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,37 +1,12 @@
|
||||||
module Microformats2
|
module Microformats2
|
||||||
module Property
|
module Property
|
||||||
class Text < Property::Parser
|
class Text < Property::Parser
|
||||||
def parse_flat_element(element)
|
def attr_map
|
||||||
Element.new(element).value
|
@attr_map = {
|
||||||
end
|
|
||||||
|
|
||||||
class Element < Struct.new(:element)
|
|
||||||
ATTR_MAP = {
|
|
||||||
"abbr" => "title",
|
"abbr" => "title",
|
||||||
"data" => "value",
|
"data" => "value",
|
||||||
"img" => "alt",
|
"img" => "alt",
|
||||||
"area" => "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
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,39 +1,14 @@
|
||||||
module Microformats2
|
module Microformats2
|
||||||
module Property
|
module Property
|
||||||
class Url < Property::Parser
|
class Url < Property::Parser
|
||||||
def parse_flat_element(element)
|
def attr_map
|
||||||
Element.new(element).value
|
@attr_map = {
|
||||||
end
|
|
||||||
|
|
||||||
class Element < Struct.new(:element)
|
|
||||||
ATTR_MAP = {
|
|
||||||
"a" => "href",
|
"a" => "href",
|
||||||
"area" => "href",
|
"area" => "href",
|
||||||
"img" => "src",
|
"img" => "src",
|
||||||
"object" => "data",
|
"object" => "data",
|
||||||
"abbr" => "title",
|
"abbr" => "title",
|
||||||
"data" => "value"
|
"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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue