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.
This commit is contained in:
parent
38e0ae313f
commit
7daf6c5e11
2 changed files with 52 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue