From 7daf6c5e115e50363aa2aa05d46e8d269131209a Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Sun, 15 Sep 2013 11:52:53 +0100 Subject: [PATCH] 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