fixes whitespace issue. uses two spaces instead of tabs

This commit is contained in:
Jessica Lynn Suttles 2013-02-13 17:28:08 -08:00
parent b4084d5325
commit 1d8e46ce95
8 changed files with 181 additions and 141 deletions

View file

@ -1,59 +1,59 @@
module Microformats2 module Microformats2
class FormatParser class FormatParser
class << self class << self
def parse(element) def parse(element)
parse_node(element).flatten.compact parse_node(element).flatten.compact
end end
def parse_node(node) def parse_node(node)
case case
when node.is_a?(Nokogiri::HTML::Document) then parse_node(node.children) 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::NodeSet) then parse_nodeset(node)
when node.is_a?(Nokogiri::XML::Element) then [parse_for_microformats(node)] when node.is_a?(Nokogiri::XML::Element) then [parse_for_microformats(node)]
end end
end end
def parse_nodeset(nodeset) def parse_nodeset(nodeset)
nodeset.map { |node| parse_node(node) } nodeset.map { |node| parse_node(node) }
end end
def parse_for_microformats(element) def parse_for_microformats(element)
if format_classes(element).length >= 1 if format_classes(element).length >= 1
parse_microformat(element) parse_microformat(element)
else else
parse_nodeset(element.children) parse_nodeset(element.children)
end end
end end
def parse_microformat(element) def parse_microformat(element)
# only create ruby object for first format class # only create ruby object for first format class
html_class = format_classes(element).first html_class = format_classes(element).first
const_name = constant_name(html_class) const_name = constant_name(html_class)
klass = find_or_create_ruby_class(const_name) klass = find_or_create_ruby_class(const_name)
klass.new(element).parse klass.new(element).parse
end end
def format_classes(element) def format_classes(element)
element.attribute("class").to_s.split.select do |html_class| element.attribute("class").to_s.split.select do |html_class|
html_class =~ Format::CLASS_REG_EXP html_class =~ Format::CLASS_REG_EXP
end end
end end
def constant_name(html_class) def constant_name(html_class)
# html-Class -> html-class -> html_class -> Html_class -> HtmlClass # html-Class -> html-class -> html_class -> Html_class -> HtmlClass
html_class.downcase.gsub("-","_").gsub(/^([a-z])/){$1.upcase}.gsub(/_(.)/){$1.upcase} html_class.downcase.gsub("-","_").gsub(/^([a-z])/){$1.upcase}.gsub(/_(.)/){$1.upcase}
end end
def find_or_create_ruby_class(const_name) def find_or_create_ruby_class(const_name)
if Object.const_defined?(const_name) if Object.const_defined?(const_name)
klass = Object.const_get(const_name) klass = Object.const_get(const_name)
else else
klass = Class.new(Microformats2::Format) klass = Class.new(Microformats2::Format)
Object.const_set const_name, klass Object.const_set const_name, klass
end end
klass klass
end end
end # class << self end # class << self
end end
end end

View file

@ -1,21 +1,21 @@
module Microformats2 module Microformats2
module Property module Property
class DateTime < Foundation class DateTime < Foundation
def value def value
::DateTime.parse(super) ::DateTime.parse(super)
rescue ArgumentError => e rescue ArgumentError => e
super super
end end
protected protected
def attr_map def attr_map
@attr_map ||= { @attr_map ||= {
"time" => "datetime", "time" => "datetime",
"ins" => "datetime", "ins" => "datetime",
"abbr" => "title", "abbr" => "title",
"data" => "value" } "data" => "value" }
end end
end end
end end
end end

View file

@ -1,9 +1,9 @@
module Microformats2 module Microformats2
module Property module Property
class Embedded < Foundation class Embedded < Foundation
def value def value
@value ||= @element.inner_html.strip @value ||= @element.inner_html.strip
end end
end end
end end
end end

View file

@ -3,23 +3,23 @@ module Microformats2
class Foundation class Foundation
attr_reader :method_name attr_reader :method_name
def initialize(element, html_class) def initialize(element, html_class)
@element = element @element = element
@method_name = to_method_name(html_class) @method_name = to_method_name(html_class)
end
def parse
value
formats
self
end end
def value def parse
@value ||= value_class_pattern || element_value || text_value value
end formats
self
end
def value
@value ||= value_class_pattern || element_value || text_value
end
def formats def formats
@formats ||= format_classes.length >=1 ? FormatParser.parse(@element) : [] @formats ||= format_classes.length >=1 ? FormatParser.parse(@element) : []
end end
def to_hash def to_hash
@ -36,25 +36,25 @@ module Microformats2
protected protected
def value_class_pattern def value_class_pattern
# TODO # TODO
end end
def element_value def element_value
@element.attribute(attribute).to_s if attribute @element.attribute(attribute).to_s if attribute
end end
def text_value def text_value
@element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip @element.inner_text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
end end
def attribute def attribute
attr_map[@element.name] attr_map[@element.name]
end end
def attr_map def attr_map
{} {}
end end
private private
@ -66,11 +66,11 @@ module Microformats2
mn mn
end end
def format_classes def format_classes
@format_classes = @element.attribute("class").to_s.split.select do |html_class| @format_classes = @element.attribute("class").to_s.split.select do |html_class|
html_class =~ Format::CLASS_REG_EXP html_class =~ Format::CLASS_REG_EXP
end end
end end
end end
end end
end end

View file

@ -1,16 +1,16 @@
module Microformats2 module Microformats2
module Property module Property
class Text < Foundation class Text < Foundation
protected protected
def attr_map def attr_map
@attr_map = { @attr_map = {
"abbr" => "title", "abbr" => "title",
"data" => "value", "data" => "value",
"img" => "alt", "img" => "alt",
"area" => "alt" } "area" => "alt" }
end end
end end
end end
end end

View file

@ -1,18 +1,18 @@
module Microformats2 module Microformats2
module Property module Property
class Url < Foundation class Url < Foundation
protected protected
def attr_map def attr_map
@attr_map = { @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" }
end end
end end
end end
end end

View file

@ -1,45 +1,54 @@
module Microformats2 module Microformats2
class PropertyParser class PropertyParser
class << self class << self
def parse(element) def parse(element)
parse_node(element).flatten.compact parse_node(element).flatten.compact
end end
def parse_node(node) def parse_node(node)
case case
when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node) when node.is_a?(Nokogiri::XML::NodeSet) then parse_nodeset(node)
when node.is_a?(Nokogiri::XML::Element) then [parse_for_properties(node)] when node.is_a?(Nokogiri::XML::Element) then [parse_for_properties(node)]
end end
end end
def parse_nodeset(nodeset) def parse_nodeset(nodeset)
nodeset.map { |node| parse_node(node) } nodeset.map { |node| parse_node(node) }
end end
def parse_for_properties(element) def parse_for_properties(element)
if property_classes(element).length >= 1 if property_classes(element).length >= 1
parse_property(element) parse_property(element)
else else
parse_nodeset(element.children) parse_nodeset(element.children)
end end
end end
def parse_property(element) def parse_property(element)
property_classes(element).map do |property_class| property_classes(element).map do |property_class|
# p-class-name -> p # p-class-name -> p
prefix = property_class.split("-").first prefix = property_class.split("-").first
# find ruby class for kind of property # find ruby class for kind of property
klass = Microformats2::Property::PREFIX_CLASS_MAP[prefix] klass = Microformats2::Property::PREFIX_CLASS_MAP[prefix]
klass.new(element, property_class).parse property = klass.new(element, property_class).parse
end properties = format_classes(element).empty? ? PropertyParser.parse(element.children) : []
end
def property_classes(element) properties << property
element.attribute("class").to_s.split.select do |html_class| end
html_class =~ Property::CLASS_REG_EXP end
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 # class << self
end end
end end

View file

@ -101,4 +101,35 @@ describe Microformats2::Collection do
end end
end end
end end
describe ".h-entry .p-author.h-card nested" do
before do
html = "spec/support/hcard-pname-pnickname-nested.html"
@collection = Microformats2.parse(html)
end
describe "#parse" do
it "assigns .h-card .p-name to HCard#name" do
@collection.first.name.first.value.should == "jlsuttles"
end
it "assigns .h-card .p-nickname to HCard#nickname" do
@collection.first.nickname.first.value.should == "jlsuttles"
end
end
describe "#to_hash" do
it "returns the correct Hash" do
hash = {
:items => [{
:type => ["h-card"],
:properties => {
:name => ["jlsuttles"],
:nickname => ["jlsuttles"]
}
}]
}
@collection.to_hash.should == hash
end
end
end
end end