implements property parsers as defined in wiki
This commit is contained in:
parent
83b469597f
commit
915250f5b0
7 changed files with 166 additions and 53 deletions
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
47
lib/microformats2/property/date_time.rb
Normal file
47
lib/microformats2/property/date_time.rb
Normal file
|
@ -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
|
9
lib/microformats2/property/embedded.rb
Normal file
9
lib/microformats2/property/embedded.rb
Normal file
|
@ -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
|
27
lib/microformats2/property/parser.rb
Normal file
27
lib/microformats2/property/parser.rb
Normal file
|
@ -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
|
38
lib/microformats2/property/text.rb
Normal file
38
lib/microformats2/property/text.rb
Normal file
|
@ -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
|
40
lib/microformats2/property/url.rb
Normal file
40
lib/microformats2/property/url.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue