From 1d8e46ce9572d4781772b3ebc568210b599fe8e2 Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Wed, 13 Feb 2013 17:28:08 -0800 Subject: [PATCH] fixes whitespace issue. uses two spaces instead of tabs --- lib/microformats2/format_parser.rb | 78 +++++++++++------------ lib/microformats2/property/date_time.rb | 28 ++++---- lib/microformats2/property/embedded.rb | 4 +- lib/microformats2/property/foundation.rb | 64 +++++++++---------- lib/microformats2/property/text.rb | 18 +++--- lib/microformats2/property/url.rb | 22 +++---- lib/microformats2/property_parser.rb | 77 ++++++++++++---------- spec/lib/microformats2/collection_spec.rb | 31 +++++++++ 8 files changed, 181 insertions(+), 141 deletions(-) diff --git a/lib/microformats2/format_parser.rb b/lib/microformats2/format_parser.rb index 32f349d..0edcb0b 100644 --- a/lib/microformats2/format_parser.rb +++ b/lib/microformats2/format_parser.rb @@ -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 diff --git a/lib/microformats2/property/date_time.rb b/lib/microformats2/property/date_time.rb index f7b7c00..d304fc0 100644 --- a/lib/microformats2/property/date_time.rb +++ b/lib/microformats2/property/date_time.rb @@ -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 diff --git a/lib/microformats2/property/embedded.rb b/lib/microformats2/property/embedded.rb index b938735..77d4785 100644 --- a/lib/microformats2/property/embedded.rb +++ b/lib/microformats2/property/embedded.rb @@ -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 diff --git a/lib/microformats2/property/foundation.rb b/lib/microformats2/property/foundation.rb index 6682f4c..2e38713 100644 --- a/lib/microformats2/property/foundation.rb +++ b/lib/microformats2/property/foundation.rb @@ -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 diff --git a/lib/microformats2/property/text.rb b/lib/microformats2/property/text.rb index 5252ee9..cf878fc 100644 --- a/lib/microformats2/property/text.rb +++ b/lib/microformats2/property/text.rb @@ -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 diff --git a/lib/microformats2/property/url.rb b/lib/microformats2/property/url.rb index dbbec49..1d45522 100644 --- a/lib/microformats2/property/url.rb +++ b/lib/microformats2/property/url.rb @@ -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 diff --git a/lib/microformats2/property_parser.rb b/lib/microformats2/property_parser.rb index 2da472a..2f71d49 100644 --- a/lib/microformats2/property_parser.rb +++ b/lib/microformats2/property_parser.rb @@ -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 diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index 839bb80..25b70b4 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -101,4 +101,35 @@ describe Microformats2::Collection do 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