diff --git a/README.md b/README.md index 5a3f4ee..f073462 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,12 @@ Implemented: * nested properties * nested microformat with associated property * dynamic creation of properties +* [rel](http://microformats.org/wiki/rel) Not Implemented: * [normalize u-* property values](http://microformats.org/wiki/microformats2-parsing-faq#normalizing_u-.2A_property_values) * nested microformat without associated property -* [rel](http://microformats.org/wiki/rel) * [value-class-pattern](http://microformats.org/wiki/value-class-pattern) * [include-pattern](http://microformats.org/wiki/include-pattern) * recognition of [vendor extensions](http://microformats.org/wiki/microformats2#VENDOR_EXTENSIONS) diff --git a/lib/microformats2/collection.rb b/lib/microformats2/collection.rb index 84f713d..09d106a 100644 --- a/lib/microformats2/collection.rb +++ b/lib/microformats2/collection.rb @@ -2,13 +2,25 @@ module Microformats2 class Collection attr_reader :all - def initialize(element) + def initialize(element, url = nil) @element = element + + @base = nil + if url != nil + @base = url + end + if @element.search("base").size > 0 + @base = @element.search("base")[0].attribute("href") + end + @format_names = [] + @rels = {} + @alternates = [] end def parse all + parse_rels self end @@ -29,10 +41,12 @@ module Microformats2 end def to_hash - hash = { items: [] } + hash = { items: [], rels: @rels } all.each do |format| hash[:items] << format.to_hash end + hash[:alternates] = @alternates unless @alternates.nil? || @alternates.empty? + hash end @@ -67,5 +81,40 @@ module Microformats2 send("#{mn.pluralize}=", [value]) end end + + def parse_rels + @element.search("*[@rel]").each do |rel| + rel_values = rel.attribute("rel").text.split(" ") + + if rel_values.member?("alternate") + alternate_inst = {} + alternate_inst["url"] = absolutize(rel.attribute("href").text) + alternate_inst["rel"] = (rel_values - ["alternate"]).join(" ") + unless rel.attribute("media").nil? + alternate_inst["media"] = rel.attribute("media").text + end + unless rel.attribute("hreflang").nil? + alternate_inst["hreflang"] = rel.attribute("hreflang").text + end + unless rel.attribute("type").nil? + alternate_inst["type"] = rel.attribute("type").text + end + @alternates << alternate_inst + else + rel_values.each do |rel_value| + @rels[rel_value] = [] unless @rels.has_key?(rel_value) + @rels[rel_value] << absolutize(rel.attribute("href").text) + end + end + end + end + + def absolutize(href) + if URI.parse(href).absolute? + href + else + URI.join(@base, href).to_s + end + end end end diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index e5d3be1..dfca7f3 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -176,6 +176,20 @@ describe Microformats2::Collection do end end end + + describe "rels.html" do + before do + html = "spec/support/lib/microformats2/rels.html" + @collection = Microformats2.parse(html) + end + describe "#to_json" do + it "should match rels.js" do + json = "spec/support/lib/microformats2/rels.js" + json = open(json).read + JSON.parse(@collection.to_json).should == JSON.parse(json) + end + end + end end diff --git a/spec/support/lib/microformats2/nested-format-with-property.js b/spec/support/lib/microformats2/nested-format-with-property.js index f38e461..1007383 100644 --- a/spec/support/lib/microformats2/nested-format-with-property.js +++ b/spec/support/lib/microformats2/nested-format-with-property.js @@ -11,5 +11,6 @@ }], "name": ["Jessica Lynn Suttles"] } - }] + }], + "rels": {} } diff --git a/spec/support/lib/microformats2/nested-property.js b/spec/support/lib/microformats2/nested-property.js index b29a1d8..1e5d8cf 100644 --- a/spec/support/lib/microformats2/nested-property.js +++ b/spec/support/lib/microformats2/nested-property.js @@ -4,6 +4,7 @@ "name": ["jlsuttles"], "nickname": ["jlsuttles"] } - }] + }], + "rels": {} } diff --git a/spec/support/lib/microformats2/rels.html b/spec/support/lib/microformats2/rels.html new file mode 100644 index 0000000..908e4d4 --- /dev/null +++ b/spec/support/lib/microformats2/rels.html @@ -0,0 +1,12 @@ + + + +Rels Test + + + + +
  • Jeremy Keith
  • +
  • Tantek Çelik
  • + + \ No newline at end of file diff --git a/spec/support/lib/microformats2/rels.js b/spec/support/lib/microformats2/rels.js new file mode 100644 index 0000000..9053dbe --- /dev/null +++ b/spec/support/lib/microformats2/rels.js @@ -0,0 +1,13 @@ +{ + "items": [], + "rels": { + "friend": ["http://adactio.com/", "http://tantek.com/"], + "met": ["http://adactio.com/", "http://tantek.com/"] + }, + "alternates": [ + {"url": "http://tantek.com/updates.atom", + "type": "application/atom+xml", + "rel": "updates" + } + ] +} \ No newline at end of file diff --git a/spec/support/lib/microformats2/simple.js b/spec/support/lib/microformats2/simple.js index 9d02f66..c549abf 100644 --- a/spec/support/lib/microformats2/simple.js +++ b/spec/support/lib/microformats2/simple.js @@ -6,5 +6,6 @@ "bday": ["1990-10-15"], "content": ["

    Vegan. Cat lover. Coder.

    "] } - }] + }], + "rels": {} }