Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Commit

Permalink
First pass at hatom support.
Browse files Browse the repository at this point in the history
* reltag extract not working yet (placeholdered)
* Only simple examples, not following spec 100% yet
* Also includes refactoring microformat map some to avoid class loading
  issues
* Also had to add yaml require for tests to run
  • Loading branch information
sentientmonkey committed Apr 27, 2013
1 parent 2830c5d commit a1084ba
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 12 deletions.
30 changes: 19 additions & 11 deletions lib/prism/microformat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ module Microformat

def self.map(name)
map = microformats[Prism.normalize(name)]
map = format_to_class(map) if map
raise "#{name} is not a recognized microformat." unless map
map
end

def self.microformats
{ :hcard => Prism::Microformat::HCard,
:hcalendar => Prism::Microformat::HCalendar,
:geo => Prism::Microformat::Geo,
:adr => Prism::Microformat::Adr,
:rellicense => Prism::Microformat::RelLicense,
:reltag => Prism::Microformat::RelTag,
:votelinks => Prism::Microformat::VoteLinks,
:xfn => Prism::Microformat::XFN,
:xmdp => Prism::Microformat::XMDP,
:xoxo => Prism::Microformat::XOXO }
{ :hatom => "HAtom",
:hcard => "HCard",
:hcalendar => "HCalendar",
:geo => "Geo",
:adr => "Adr",
:rellicense => "RelLicense",
:reltag => "RelTag",
:votelinks => "VoteLinks",
:xfn => "XFN",
:xmdp => "XMDP",
:xoxo => "XOXO" }
end

def self.format_to_class(format)
Prism::Microformat.const_get(format)
end

def self.find(html, uformat = nil)
Expand All @@ -31,7 +37,8 @@ def self.find(html, uformat = nil)
def self.find_all(html)
doc = Prism.get(html)
uformats = microformats.values.collect do |uf|
uf.parse(doc)
klass = format_to_class(uf)
klass.parse(doc)
end
uformats.compact.flatten
end
Expand All @@ -49,3 +56,4 @@ def self.find_all(html)
require 'prism/microformat/adr'
require 'prism/microformat/hcalendar'
require 'prism/microformat/hcard'
require 'prism/microformat/hatom'
70 changes: 70 additions & 0 deletions lib/prism/microformat/hatom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Prism
module Microformat
class HAtom < POSH
FRIENDLY_NAME = "hAtom"
WIKI_URL = "http://microformats.org/wiki/hatom"
XMDP = 'http://microformats.org/profile/hatom'

name :hfeed

search do |doc|
hfeed = doc.css("#{name}")
if hfeed.empty? && !doc.css('.hfeed').empty?
doc
else
hfeed
end
end

#TODO move to reltags
has_many :category do
search do |node|
node.css('a[rel~="tag"]')
end
extract do |node|
node['href'].split('/').last
end
end

has_many :hentry do
has_one :entry_title, :entry_summary

has_one :updated, :published do
extract :typevalue
end

has_one :author do
extract :hcard
end

has_one :entry_content do
extract do |node|
node.inner_html.strip
end
end

#TODO move to reltags
has_many :tags do
search do |node|
node.css('a[rel~="tag"]')
end
extract do |node|
node['href'].split('/').last
end
end

has_one :bookmark do
search do |node|
node.css('a[rel~="bookmark"]')
end
extract do |node|
node['href']
end
end

required!
end

end
end
end
40 changes: 40 additions & 0 deletions test/fixtures/test-fixture/hatom/example1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!-- from http://microformats.org/wiki/hatom-examples -->
<body>
<div id="uf">
<div class="hfeed" id="content">
<div class="hentry entry" id="post-60">
<h3 class="entry-title">
<a href="http://www.microformats.org/blog/..." rel="bookmark" title="...">Wiki Attack</a>
</h3>
<div class="entry-content">
<p>We had a bit of trouble with ...</p>
<p>We've restored the wiki and ...</p>
<p>If anyone is working to combat said spammers ...</p>
</div>

<h4 class="tags">Technorati Tags:</h4>
<ul class="tags">
<li><a href="http://technorati.com/tag/mediawiki" rel="tag">MediaWiki</a></li>
<li><a href="http://technorati.com/tag/microformats" rel="tag">MicroFormats</a></li>
<li><a href="http://technorati.com/tag/spam" rel="tag">Spam</a></li>
</ul>

<ul class="post-info">
<li>
<a href="http://www.microformats.org/blog/..." rel="bookmark"
title="..."><abbr class="published" title="2005-10-10T14:07:00-07:00">October 10th, 2005</abbr></a>
</li>
<li>
<address class="vcard author"><a class="url fn" href="http://theryanking.com">Ryan King</a></address>
</li>
<li>
<a href="http://www.microformats.org/blog/...">4 Comments</a>
</li>
</ul>
</div>

<div class="hentry entry" id="post-59">
</div>
</div>
</div>
</body>
35 changes: 35 additions & 0 deletions test/fixtures/test-fixture/hatom/example2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!-- http://microformats.org/wiki/hatom-examples -->
<body>
<div id="uf">
<div class="hfeed">
<div class="hentry posts" id="112993192128302715">
<strong class="entry-title entry-content">
Nelson's final prayer
</strong>
<span class="entry-content">
written on the night before Trafalgar:<blockquote>May the Great God, ... heart.
</span>
<div>
<span class="byline">posted by <span class="author vcard"><span class="fn">Natalie</span></span> at
<a rel="bookmark" href="2005_10_16_nataliesolent_archive.html#112993192128302715">
<abbr class="published" title="2005-10-24t09:49:00-00:00">9:49 PM</abbr></a>
</span>
</div>
</div>

<div class="hentry posts" id="112993022840118939">
<strong class="heading content>"I really, truly </strong>
<span class="entry-content">
didn't go ... view.
</span>
<div>
<span class="byline">
posted by <span class="author vcard"><span class="fn">Natalie</span></span> at
<a rel="bookmark" href="2005_10_16_nataliesolent_archive.html#112993022840118939">
<abbr class="published" title="2005-10-24t09:49:00-00:00">9:28 pm</abbr></a>
</span>
</div>
</div>
</div>
</div>
</body>
81 changes: 81 additions & 0 deletions test/microformat/hatom_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require File.join(File.dirname(File.absolute_path(__FILE__)),'..','test_helper')

class HAtomTest < Test::Unit::TestCase
@@klass = Prism::Microformat::HAtom

describe 'first example test' do
def self.before_all
@doc ||= test_fixture('hatom/example1.html')
@hatom ||= @@klass.parse(@doc)
end

setup do
@hatom ||= self.class.before_all
end

test 'The title is a singular value' do
hentry = @hatom.hentry
assert_equal "Wiki Attack", hentry[0].entry_title
end

test 'The content contains html' do
hentry = @hatom.hentry
content = <<-EOS
<p>We had a bit of trouble with ...</p>
<p>We've restored the wiki and ...</p>
<p>If anyone is working to combat said spammers ...</p>
EOS
assert_equal content.strip, hentry[0].entry_content
end

test 'The published is a time' do
hentry = @hatom.hentry
assert_equal Time.parse('2005-10-10 14:07:00 -0700'), hentry[0].published
end

test 'The author is an hcard' do
hentry = @hatom.hentry
hcard = hentry[0].author
assert_equal Prism::Microformat::HCard, hcard.class
assert_equal 'Ryan King', hcard.fn
assert_equal 'King', hcard.n[:family_name]
assert_equal 'Ryan', hcard.n[:given_name]
assert_equal 'http://theryanking.com/', hcard.url[0]
end

test 'The tags are a list of values' do
hentry = @hatom.hentry
assert_equal ['mediawiki', 'microformats', 'spam'], hentry[0].tags
end

end

describe 'second example test' do
def self.before_all
@doc ||= test_fixture('hatom/example2.html')
@hatom ||= @@klass.parse(@doc)
end

setup do
@hatom ||= self.class.before_all
end

test 'The title is a singular value' do
hentry = @hatom.hentry
assert_equal "Nelson's final prayer", hentry[0].entry_title
end

test 'The content is an html value' do
hentry = @hatom.hentry
assert_equal "Nelson's final prayer", hentry[0].entry_content
end

test 'The bookmark is a rel-bookmark' do
hentry = @hatom.hentry
assert_equal "2005_10_16_nataliesolent_archive.html#112993192128302715", hentry[0].bookmark
end

end

end

3 changes: 2 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Bundler.setup
end

require 'yaml'
require 'test/unit'
require 'contest'
require 'fakeweb'
Expand All @@ -34,4 +35,4 @@ def get_fixture(filename)
def test_fixture(filename)
html = get_fixture('test-fixture/' + filename)
doc = Nokogiri::HTML.parse(html).css('#uf').first
end
end

0 comments on commit a1084ba

Please sign in to comment.