fixes whitespace issue. uses two spaces instead of tabs
This commit is contained in:
parent
b4084d5325
commit
1d8e46ce95
8 changed files with 181 additions and 141 deletions
|
@ -1,59 +1,59 @@
|
|||
module Microformats2
|
||||
class FormatParser
|
||||
class << self
|
||||
def parse(element)
|
||||
parse_node(element).flatten.compact
|
||||
end
|
||||
class << self
|
||||
def parse(element)
|
||||
parse_node(element).flatten.compact
|
||||
end
|
||||
|
||||
def parse_node(node)
|
||||
case
|
||||
def parse_node(node)
|
||||
case
|
||||
when node.is_a?(Nokogiri::HTML::Document) then parse_node(node.children)
|
||||
when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node)
|
||||
when node.is_a?(Nokogiri::XML::Element) then [parse_for_microformats(node)]
|
||||
end
|
||||
end
|
||||
when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node)
|
||||
when node.is_a?(Nokogiri::XML::Element) then [parse_for_microformats(node)]
|
||||
end
|
||||
end
|
||||
|
||||
def parse_nodeset(nodeset)
|
||||
nodeset.map { |node| parse_node(node) }
|
||||
end
|
||||
def parse_nodeset(nodeset)
|
||||
nodeset.map { |node| parse_node(node) }
|
||||
end
|
||||
|
||||
def parse_for_microformats(element)
|
||||
if format_classes(element).length >= 1
|
||||
parse_microformat(element)
|
||||
else
|
||||
parse_nodeset(element.children)
|
||||
end
|
||||
end
|
||||
def parse_for_microformats(element)
|
||||
if format_classes(element).length >= 1
|
||||
parse_microformat(element)
|
||||
else
|
||||
parse_nodeset(element.children)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_microformat(element)
|
||||
# only create ruby object for first format class
|
||||
html_class = format_classes(element).first
|
||||
def parse_microformat(element)
|
||||
# only create ruby object for first format class
|
||||
html_class = format_classes(element).first
|
||||
const_name = constant_name(html_class)
|
||||
klass = find_or_create_ruby_class(const_name)
|
||||
|
||||
klass.new(element).parse
|
||||
end
|
||||
klass.new(element).parse
|
||||
end
|
||||
|
||||
def format_classes(element)
|
||||
element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Format::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
def format_classes(element)
|
||||
element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Format::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
|
||||
def constant_name(html_class)
|
||||
# html-Class -> html-class -> html_class -> Html_class -> HtmlClass
|
||||
html_class.downcase.gsub("-","_").gsub(/^([a-z])/){$1.upcase}.gsub(/_(.)/){$1.upcase}
|
||||
# html-Class -> html-class -> html_class -> Html_class -> HtmlClass
|
||||
html_class.downcase.gsub("-","_").gsub(/^([a-z])/){$1.upcase}.gsub(/_(.)/){$1.upcase}
|
||||
end
|
||||
|
||||
def find_or_create_ruby_class(const_name)
|
||||
if Object.const_defined?(const_name)
|
||||
klass = Object.const_get(const_name)
|
||||
else
|
||||
klass = Class.new(Microformats2::Format)
|
||||
Object.const_set const_name, klass
|
||||
end
|
||||
if Object.const_defined?(const_name)
|
||||
klass = Object.const_get(const_name)
|
||||
else
|
||||
klass = Class.new(Microformats2::Format)
|
||||
Object.const_set const_name, klass
|
||||
end
|
||||
klass
|
||||
end
|
||||
end # class << self
|
||||
end # class << self
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
module Property
|
||||
class DateTime < Foundation
|
||||
def value
|
||||
::DateTime.parse(super)
|
||||
rescue ArgumentError => e
|
||||
super
|
||||
end
|
||||
def value
|
||||
::DateTime.parse(super)
|
||||
rescue ArgumentError => e
|
||||
super
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
@attr_map ||= {
|
||||
"time" => "datetime",
|
||||
"ins" => "datetime",
|
||||
"abbr" => "title",
|
||||
"data" => "value" }
|
||||
end
|
||||
def attr_map
|
||||
@attr_map ||= {
|
||||
"time" => "datetime",
|
||||
"ins" => "datetime",
|
||||
"abbr" => "title",
|
||||
"data" => "value" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
module Property
|
||||
class Embedded < Foundation
|
||||
def value
|
||||
@value ||= @element.inner_html.strip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,23 +3,23 @@ module Microformats2
|
|||
class Foundation
|
||||
attr_reader :method_name
|
||||
|
||||
def initialize(element, html_class)
|
||||
@element = element
|
||||
def initialize(element, html_class)
|
||||
@element = element
|
||||
@method_name = to_method_name(html_class)
|
||||
end
|
||||
|
||||
def parse
|
||||
value
|
||||
formats
|
||||
self
|
||||
end
|
||||
|
||||
def value
|
||||
@value ||= value_class_pattern || element_value || text_value
|
||||
end
|
||||
def parse
|
||||
value
|
||||
formats
|
||||
self
|
||||
end
|
||||
|
||||
def value
|
||||
@value ||= value_class_pattern || element_value || text_value
|
||||
end
|
||||
|
||||
def formats
|
||||
@formats ||= format_classes.length >=1 ? FormatParser.parse(@element) : []
|
||||
@formats ||= format_classes.length >=1 ? FormatParser.parse(@element) : []
|
||||
end
|
||||
|
||||
def to_hash
|
||||
|
@ -36,25 +36,25 @@ module Microformats2
|
|||
|
||||
protected
|
||||
|
||||
def value_class_pattern
|
||||
# TODO
|
||||
end
|
||||
def value_class_pattern
|
||||
# TODO
|
||||
end
|
||||
|
||||
def element_value
|
||||
@element.attribute(attribute).to_s if attribute
|
||||
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 text_value
|
||||
@element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
||||
end
|
||||
|
||||
def attribute
|
||||
attr_map[@element.name]
|
||||
end
|
||||
def attribute
|
||||
attr_map[@element.name]
|
||||
end
|
||||
|
||||
def attr_map
|
||||
{}
|
||||
end
|
||||
def attr_map
|
||||
{}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
@ -66,11 +66,11 @@ module Microformats2
|
|||
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
|
||||
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,16 +1,16 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
module Property
|
||||
class Text < Foundation
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
@attr_map = {
|
||||
"abbr" => "title",
|
||||
"data" => "value",
|
||||
"img" => "alt",
|
||||
"area" => "alt" }
|
||||
end
|
||||
def attr_map
|
||||
@attr_map = {
|
||||
"abbr" => "title",
|
||||
"data" => "value",
|
||||
"img" => "alt",
|
||||
"area" => "alt" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
module Microformats2
|
||||
module Property
|
||||
module Property
|
||||
class Url < Foundation
|
||||
|
||||
protected
|
||||
|
||||
def attr_map
|
||||
@attr_map = {
|
||||
"a" => "href",
|
||||
"area" => "href",
|
||||
"img" => "src",
|
||||
"object" => "data",
|
||||
"abbr" => "title",
|
||||
"data" => "value" }
|
||||
end
|
||||
def attr_map
|
||||
@attr_map = {
|
||||
"a" => "href",
|
||||
"area" => "href",
|
||||
"img" => "src",
|
||||
"object" => "data",
|
||||
"abbr" => "title",
|
||||
"data" => "value" }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,45 +1,54 @@
|
|||
module Microformats2
|
||||
class PropertyParser
|
||||
class << self
|
||||
def parse(element)
|
||||
parse_node(element).flatten.compact
|
||||
end
|
||||
class << self
|
||||
def parse(element)
|
||||
parse_node(element).flatten.compact
|
||||
end
|
||||
|
||||
def parse_node(node)
|
||||
case
|
||||
when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node)
|
||||
when node.is_a?(Nokogiri::XML::Element) then [parse_for_properties(node)]
|
||||
end
|
||||
end
|
||||
def parse_node(node)
|
||||
case
|
||||
when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node)
|
||||
when node.is_a?(Nokogiri::XML::Element) then [parse_for_properties(node)]
|
||||
end
|
||||
end
|
||||
|
||||
def parse_nodeset(nodeset)
|
||||
nodeset.map { |node| parse_node(node) }
|
||||
end
|
||||
def parse_nodeset(nodeset)
|
||||
nodeset.map { |node| parse_node(node) }
|
||||
end
|
||||
|
||||
def parse_for_properties(element)
|
||||
if property_classes(element).length >= 1
|
||||
parse_property(element)
|
||||
else
|
||||
parse_nodeset(element.children)
|
||||
end
|
||||
end
|
||||
def parse_for_properties(element)
|
||||
if property_classes(element).length >= 1
|
||||
parse_property(element)
|
||||
else
|
||||
parse_nodeset(element.children)
|
||||
end
|
||||
end
|
||||
|
||||
def parse_property(element)
|
||||
property_classes(element).map do |property_class|
|
||||
# p-class-name -> p
|
||||
prefix = property_class.split("-").first
|
||||
# find ruby class for kind of property
|
||||
klass = Microformats2::Property::PREFIX_CLASS_MAP[prefix]
|
||||
def parse_property(element)
|
||||
property_classes(element).map do |property_class|
|
||||
# p-class-name -> p
|
||||
prefix = property_class.split("-").first
|
||||
# find ruby class for kind of property
|
||||
klass = Microformats2::Property::PREFIX_CLASS_MAP[prefix]
|
||||
|
||||
klass.new(element, property_class).parse
|
||||
end
|
||||
end
|
||||
property = klass.new(element, property_class).parse
|
||||
properties = format_classes(element).empty? ? PropertyParser.parse(element.children) : []
|
||||
|
||||
def property_classes(element)
|
||||
element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Property::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
properties << property
|
||||
end
|
||||
end
|
||||
|
||||
def property_classes(element)
|
||||
element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Property::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
|
||||
def format_classes(element)
|
||||
element.attribute("class").to_s.split.select do |html_class|
|
||||
html_class =~ Format::CLASS_REG_EXP
|
||||
end
|
||||
end
|
||||
end # class << self
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue