-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Several improvements in Uri and HTML metadata
- Loading branch information
Showing
2 changed files
with
108 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,85 @@ | ||
{ 'hpricot' => 'HTML introspection', | ||
'mofo' => 'Microformats' }.each_pair do |gem, support| | ||
begin | ||
require gem | ||
rescue MissingSourceFile | ||
Rails.logger.info "Station Info: You need '#{ gem }' gem for #{ support } support" | ||
end | ||
end | ||
|
||
module Station #:nodoc: | ||
# Manage HTML documents | ||
class Html | ||
require 'hpricot' | ||
attr_reader :text | ||
|
||
def initialize(text = "") | ||
@text = text | ||
def initialize(text) | ||
@text = text || "" | ||
end | ||
|
||
def doc | ||
@doc ||= Hpricot(@text) | ||
end | ||
|
||
def head_links | ||
doc.search('//link') | ||
end | ||
|
||
def feeds | ||
doc.search('//link').select{ |l| | ||
head_links.select{ |l| | ||
l['rel'].match(/alternate/i) | ||
} | ||
end | ||
|
||
def rdf_links | ||
head_links.select{ |l| | ||
l['rel'].match(/meta/i) && l['type'].match(/application\/rdf\+xml/) | ||
} | ||
end | ||
|
||
def foaf_links | ||
rdf_links.select{ |l| | ||
l['title'].match(/^foaf$/i) | ||
} | ||
end | ||
|
||
def atom_service_links | ||
head_links.select{ |l| | ||
l['rel'].match(/^service$/i) | ||
} | ||
end | ||
|
||
def rsd_links | ||
head_links.select{ |l| | ||
l['rel'].match(/^EditURI$/i) | ||
} | ||
end | ||
|
||
def foaf? | ||
foaf_links.any? | ||
end | ||
|
||
def microformats | ||
Microformat.find :text => text | ||
rescue | ||
Array.new | ||
end | ||
|
||
# Find hCard in this HTML | ||
# | ||
# Needs the {mofo}[http://mofo.rubyforge.org/] gem | ||
def hcard | ||
hCard.find :text => text | ||
rescue | ||
nil | ||
end | ||
|
||
# Does this URI has a hCard attached? | ||
def hcard? | ||
hcard.present? | ||
end | ||
|
||
def to_s | ||
@text | ||
end | ||
end | ||
end |