Merge pull request #12 from tommorris/rels-alternates
Added support for rel and alternates
This commit is contained in:
commit
71ec812511
8 changed files with 97 additions and 6 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -11,5 +11,6 @@
|
|||
}],
|
||||
"name": ["Jessica Lynn Suttles"]
|
||||
}
|
||||
}]
|
||||
}],
|
||||
"rels": {}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
"name": ["jlsuttles"],
|
||||
"nickname": ["jlsuttles"]
|
||||
}
|
||||
}]
|
||||
}],
|
||||
"rels": {}
|
||||
}
|
||||
|
||||
|
|
12
spec/support/lib/microformats2/rels.html
Normal file
12
spec/support/lib/microformats2/rels.html
Normal file
|
@ -0,0 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Rels Test</title>
|
||||
<base href="http://example.org/">
|
||||
<link rel="updates alternate" href="http://tantek.com/updates.atom" type="application/atom+xml" />
|
||||
</head>
|
||||
<body>
|
||||
<li><a rel="met friend" href="http://adactio.com/">Jeremy Keith</a></li>
|
||||
<li><a rel="met friend" href="http://tantek.com/">Tantek Çelik</a></li>
|
||||
</body>
|
||||
</html>
|
13
spec/support/lib/microformats2/rels.js
Normal file
13
spec/support/lib/microformats2/rels.js
Normal file
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -6,5 +6,6 @@
|
|||
"bday": ["1990-10-15"],
|
||||
"content": ["<p>Vegan. Cat lover. Coder.</p>"]
|
||||
}
|
||||
}]
|
||||
}],
|
||||
"rels": {}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue