yay the world is fixed
This commit is contained in:
parent
40fcdf22d9
commit
b4084d5325
10 changed files with 164 additions and 95 deletions
|
@ -7,6 +7,8 @@ module Microformats2
|
|||
super
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
@attr_map ||= {
|
||||
"time" => "datetime",
|
||||
|
|
|
@ -1,60 +1,26 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
class Foundation
|
||||
attr_accessor :element, :name, :value, :formats
|
||||
attr_reader :method_name
|
||||
|
||||
def initialize(element, html_class)
|
||||
@element = element
|
||||
@name = to_method_name(html_class)
|
||||
@method_name = to_method_name(html_class)
|
||||
end
|
||||
|
||||
def to_method_name(html_class)
|
||||
# p-class-name -> class_name
|
||||
method_name = html_class.downcase.split("-")[1..-1].join("_")
|
||||
# avoid overriding Object#class
|
||||
method_name = "klass" if method_name == "class"
|
||||
method_name
|
||||
end
|
||||
|
||||
def parse
|
||||
formats
|
||||
value
|
||||
formats
|
||||
self
|
||||
end
|
||||
|
||||
def formats
|
||||
@formats ||= format_classes.length >=1 ? FormatParser.parse(element) : []
|
||||
end
|
||||
|
||||
def format_classes
|
||||
@format_classes = element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Format::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
|
||||
def value
|
||||
@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
|
||||
|
||||
def attr_map
|
||||
{}
|
||||
end
|
||||
def formats
|
||||
@formats ||= format_classes.length >=1 ? FormatParser.parse(@element) : []
|
||||
end
|
||||
|
||||
def to_hash
|
||||
if formats.empty?
|
||||
|
@ -63,6 +29,48 @@ module Microformats2
|
|||
{ value: value.to_s }.merge(formats.first.to_hash)
|
||||
end
|
||||
end
|
||||
|
||||
def to_json
|
||||
to_hash.to_json
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def to_method_name(html_class)
|
||||
# p-class-name -> class_name
|
||||
mn = html_class.downcase.split("-")[1..-1].join("_")
|
||||
# avoid overriding Object#class
|
||||
mn = "klass" if mn == "class"
|
||||
mn
|
||||
end
|
||||
|
||||
def format_classes
|
||||
@format_classes = @element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Format::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
class Text < Foundation
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
@attr_map = {
|
||||
"abbr" => "title",
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
class Url < Foundation
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
@attr_map = {
|
||||
"a" => "href",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue