parse durations as strings. rename class as klass
This commit is contained in:
parent
89b88c2c41
commit
cbc353e785
4 changed files with 35 additions and 2 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -36,7 +36,7 @@
|
|||
<div class="p-organizer h-card">
|
||||
Managed by: <span class="p-fn p-org">Example Company</span>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue