Skip to content

Commit

Permalink
Ported "mofo" to use Nokogiri for Ruby 1.9.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
igal committed Oct 17, 2011
1 parent 1b103e6 commit 4586301
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
23 changes: 14 additions & 9 deletions vendor/gems/mofo-0.2.8/lib/microformat.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%w(rubygems set hpricot microformat/string microformat/array open-uri ostruct timeout).each { |f| require f }
gem 'hpricot', '>=0.4.59'
%w(rubygems set nokogiri microformat/string microformat/array open-uri ostruct timeout).each { |f| require f }

class Microformat
module Base
Expand Down Expand Up @@ -139,16 +138,18 @@ def build_doc(source)
when String, File, StringIO
result = ''
Timeout.timeout(@@timeout) { result = open(source) }
Hpricot(result)
when Hpricot, Hpricot::Elements
Nokogiri::XML.fragment(result)
when Nokogiri, Nokogiri::XML::Element
source
when Hash
Hpricot(source[:text]) if source[:text]
Nokogiri::XML.fragment(source[:text]) if source[:text]
else
raise ArgumentError, "Unknown source class: #{source.class.name}"
end
end

def find_occurences(doc)
doc/".#{@container}"
doc.css ".#{@container}"
end

def build_class(microformat)
Expand Down Expand Up @@ -260,7 +261,7 @@ def extract_license(doc)

def extract_tags(doc)
return unless (tags = doc.search("[@rel=tag]")).size.nonzero?
tags.inject([]) { |array, tag| array + [tag.innerText] }
tags.inject([]) { |array, tag| array + [tag.text] }
end

def parse_element(element, target = nil)
Expand All @@ -285,7 +286,7 @@ def parse_element(element, target = nil)
when 'img' then element['alt']
end || ''

(value.empty? ? element.innerHTML : value).strip.strip_html.coerce
(value.empty? ? element.inner_html : value).strip.strip_html.coerce
end
end

Expand Down Expand Up @@ -316,5 +317,9 @@ class MicroformatNotFound < Exception; end
Mofo = Microformat

# type & id are used a lot in uformats and deprecated in ruby. no loss.
OpenStruct.class_eval { undef :type, :id }
OpenStruct.class_eval do
[:type, :id].each do |name|
undef_method(name) if self.respond_to?(name)
end
end
Symbol.class_eval { def no_bang() to_s.sub('!','').to_sym end }
2 changes: 1 addition & 1 deletion vendor/gems/mofo-0.2.8/lib/microformat/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.find_occurences(doc)
end

def self.build_class(tag)
new(tag.innerText)
new(tag.text)
end

def self.from(options)
Expand Down
5 changes: 3 additions & 2 deletions vendor/gems/mofo-0.2.8/lib/mofo/xfn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def to_s
attr_accessor :links

def self.find_occurences(doc)
raise NotImplementedError, "XFN not supported yet. What's the Nokogiri equivalent of Hpricot::Doc?"
case doc
when Hpricot::Doc then @occurences = XFN.new(doc)
when Nokogiri::XML::Element then @occurences = XFN.new(doc)
else @occurences
end
end
Expand All @@ -36,7 +37,7 @@ class << self
def initialize(doc)
@links = doc.search("a[@rel]").map do |rl|
relation = rl[:rel].include?(' ') ? rl[:rel].split(' ') : rl[:rel]
Link.new(:name => rl.innerHTML, :link => rl[:href], :relation => relation)
Link.new(:name => rl.inner_html, :link => rl[:href], :relation => relation)
end
end

Expand Down
2 changes: 1 addition & 1 deletion vendor/gems/mofo-0.2.8/lib/mofo/xoxo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.container?(el)

def self.build_label(node)
if node.elem?
label = Label.new(node.innerHTML.strip)
label = Label.new(node.inner_html.strip)
label.url = node['href'] if node.name == 'a'
label
elsif node.text? && !node.to_s.strip.empty?
Expand Down

0 comments on commit 4586301

Please sign in to comment.