From cbc353e78592247533b846d9ca78d64105f7cd32 Mon Sep 17 00:00:00 2001 From: Shane Becker Date: Tue, 28 Jun 2011 22:09:51 -0700 Subject: [PATCH] parse durations as strings. rename class as klass --- README.md | 2 ++ lib/microformats2.rb | 12 +++++++++++- test/hcalendar.html | 2 +- test/test_microformats2.rb | 21 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d47bbdf..3986f1f 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ Generic Microformats 2 Extractor * parses and extracts [Microformats 2](http://microformats.org/wiki/microformats-2) syntax * needs more test cases * needs better docs +* needs to deal with nested microformats +* needs to deal with class-value pattern ## SYNOPSIS diff --git a/lib/microformats2.rb b/lib/microformats2.rb index 75f0f9f..f74fbc7 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -72,7 +72,13 @@ module Microformats2 class Date def transform(property) - DateTime.parse((property.attribute("title") || property.text).to_s) + value = (property.attribute("title") || property.text).to_s + + if value[0..0] =~ /[a-zA-Z]/ + value + else + DateTime.parse(value) + end end end @@ -100,6 +106,10 @@ module Microformats2 css_class = css_class[2..-1].gsub("-","_") method_name = css_class.gsub("-","_") value = trans.transform(property) + + if method_name == "class" + method_name = "klass" + end add_method(obj, method_name) populate_method(obj, method_name, value) diff --git a/test/hcalendar.html b/test/hcalendar.html index 7c33616..1de7334 100644 --- a/test/hcalendar.html +++ b/test/hcalendar.html @@ -36,7 +36,7 @@
Managed by: Example Company
- + diff --git a/test/test_microformats2.rb b/test/test_microformats2.rb index 0ce65f1..5cfbfdb 100644 --- a/test/test_microformats2.rb +++ b/test/test_microformats2.rb @@ -29,6 +29,27 @@ class TestMicroformats2 < Test::Unit::TestCase assert_equal "Chris", result[:hcard].first.given_name end + def test_extracts_hcalendar_from_an_indiewebcamp_html_file + result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "IndieWebCamp.html"))) + assert_equal 1, result[:hevent].length + assert result[:hcard].map { |h| h.name }.include?("Urban Airship") + end + + def test_extracts_dates_in_an_hcalendar_from_an_html_file + result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "hcalendar.html"))) + assert_equal result[:hevent].first.dtstart, DateTime.parse("2007-09-08") + end + + def test_extracts_date_durations_as_a_string_from_an_html_file + result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "hcalendar.html"))) + assert_equal result[:hevent].first.duration, "P2D" + end + + def test_turns_class_values_of_class_to_klass_from_an_html_file + result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "hcalendar.html"))) + assert_equal result[:hevent].first.klass, "public" + end + def test_extracts_hcalendar_from_an_html_file result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "IndieWebCamp.html"))) assert_equal 1, result[:hevent].length