2013-02-05 13:16:51 -06:00
|
|
|
module Oneboxer
|
2014-01-27 14:09:09 -06:00
|
|
|
|
2013-04-29 21:43:21 -05:00
|
|
|
# keep reloaders happy
|
|
|
|
unless defined? Oneboxer::Result
|
|
|
|
Result = Struct.new(:doc, :changed) do
|
|
|
|
def to_html
|
|
|
|
doc.to_html
|
|
|
|
end
|
2013-04-10 02:52:38 -05:00
|
|
|
|
2013-04-29 21:43:21 -05:00
|
|
|
def changed?
|
|
|
|
changed
|
|
|
|
end
|
2013-04-10 02:52:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-27 14:09:09 -06:00
|
|
|
def self.preview(url, options=nil)
|
|
|
|
options ||= {}
|
|
|
|
Oneboxer.invalidate(url) if options[:invalidate_oneboxes]
|
|
|
|
Onebox.preview(url, cache: Rails.cache).placeholder_html
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-01-27 14:09:09 -06:00
|
|
|
def self.onebox(url, options=nil)
|
|
|
|
options ||= {}
|
|
|
|
Oneboxer.invalidate(url) if options[:invalidate_oneboxes]
|
|
|
|
Onebox.preview(url, cache: Rails.cache).to_s
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
|
2014-01-28 12:18:19 -06:00
|
|
|
def self.oneboxer_exists_for_url?(url)
|
2014-01-27 14:09:09 -06:00
|
|
|
Onebox.has_matcher?(url)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-01-28 12:18:19 -06:00
|
|
|
def self.invalidate(url)
|
2014-01-27 14:09:09 -06:00
|
|
|
Rails.cache.delete(url)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-01-28 12:18:19 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Parse URLs out of HTML, returning the document when finished.
|
|
|
|
def self.each_onebox_link(string_or_doc)
|
|
|
|
doc = string_or_doc
|
2013-04-10 02:52:38 -05:00
|
|
|
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
onebox_links = doc.search("a.onebox")
|
|
|
|
if onebox_links.present?
|
|
|
|
onebox_links.each do |link|
|
|
|
|
if link['href'].present?
|
|
|
|
yield link['href'], link
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2013-04-10 02:52:38 -05:00
|
|
|
def self.apply(string_or_doc)
|
|
|
|
doc = string_or_doc
|
|
|
|
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
|
|
|
|
changed = false
|
|
|
|
|
|
|
|
Oneboxer.each_onebox_link(doc) do |url, element|
|
|
|
|
onebox, preview = yield(url,element)
|
|
|
|
if onebox
|
|
|
|
parsed_onebox = Nokogiri::HTML::fragment(onebox)
|
2013-05-01 01:37:27 -05:00
|
|
|
next unless parsed_onebox.children.count > 0
|
2013-04-10 02:52:38 -05:00
|
|
|
|
|
|
|
# special logic to strip empty p elements
|
2013-05-01 01:37:27 -05:00
|
|
|
if element.parent &&
|
|
|
|
element.parent.node_name.downcase == "p" &&
|
|
|
|
element.parent.children.count == 1 &&
|
2013-04-10 02:52:38 -05:00
|
|
|
parsed_onebox.children.first.name.downcase == "div"
|
2013-05-01 01:37:27 -05:00
|
|
|
element = element.parent
|
2013-04-10 02:52:38 -05:00
|
|
|
end
|
|
|
|
changed = true
|
|
|
|
element.swap parsed_onebox.to_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Result.new(doc, changed)
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-01-27 14:09:09 -06:00
|
|
|
|