From 6b6524e4afc6da98da8a68f8e547d0f2425c8e5c Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Sun, 15 Sep 2013 11:51:24 +0100 Subject: [PATCH 1/5] adding specs for rels/alternates --- spec/lib/microformats2/collection_spec.rb | 14 ++++++++++++++ spec/support/lib/microformats2/rels.html | 12 ++++++++++++ spec/support/lib/microformats2/rels.js | 13 +++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 spec/support/lib/microformats2/rels.html create mode 100644 spec/support/lib/microformats2/rels.js 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/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 From 38e0ae313f44362445e822a4a72547e53939369f Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Sun, 15 Sep 2013 11:51:49 +0100 Subject: [PATCH 2/5] modifying existing test JSON output to add blank rel hashes rel: {} is required by the spec even if it is empty --- spec/support/lib/microformats2/nested-format-with-property.js | 3 ++- spec/support/lib/microformats2/nested-property.js | 3 ++- spec/support/lib/microformats2/simple.js | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) 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/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": {} } From 7daf6c5e115e50363aa2aa05d46e8d269131209a Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Sun, 15 Sep 2013 11:52:53 +0100 Subject: [PATCH 3/5] modified collection.rb to parse rels and alternates in order to parse relative URLs in rels, we need to parse the base/@href of the page if there, or to pass the URL in, so added the relevant stuff to support that. --- lib/microformats2/collection.rb | 53 ++++++++++++++++++++++- spec/lib/microformats2/collection_spec.rb | 2 +- 2 files changed, 52 insertions(+), 3 deletions(-) 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 dfca7f3..05a7db2 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -180,7 +180,7 @@ describe Microformats2::Collection do describe "rels.html" do before do html = "spec/support/lib/microformats2/rels.html" - @collection = Microformats2.parse(html) + @collection = Microformats2.parse(html, nil) end describe "#to_json" do it "should match rels.js" do From 2364ed13de75dc896bacba0274dc8ccdab87c63c Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Sun, 15 Sep 2013 11:56:04 +0100 Subject: [PATCH 4/5] updated README to move rels into supported section --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ff82ff7..f7c2cb7 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) From bf3bd6dee316608e8868d14c3188ed3c572a4aa8 Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Sun, 15 Sep 2013 18:20:04 +0100 Subject: [PATCH 5/5] omitting the nil - breaking the Travis build --- spec/lib/microformats2/collection_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index 05a7db2..dfca7f3 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -180,7 +180,7 @@ describe Microformats2::Collection do describe "rels.html" do before do html = "spec/support/lib/microformats2/rels.html" - @collection = Microformats2.parse(html, nil) + @collection = Microformats2.parse(html) end describe "#to_json" do it "should match rels.js" do