PERF: Move oneboxing from cook method "email" to postprocessing

This commit is contained in:
Gerhard Schlager 2017-10-17 20:36:57 +02:00
parent d3003592cd
commit 1481462cbf
2 changed files with 12 additions and 11 deletions

View File

@ -21,12 +21,11 @@ class EmailCook
str.scan(EmailCook.url_regexp).each do |m|
url = m[0]
val = "<a href='#{url}'>#{url}</a>"
# Onebox consideration
if str.strip == url
oneboxed = Oneboxer.onebox(url)
val = oneboxed if oneboxed.present?
# this could be oneboxed
val = %|<a href="#{url}" class="onebox" target="_blank">#{url}</a>|
else
val = %|<a href="#{url}">#{url}</a>|
end
str.gsub!(url, val)

View File

@ -27,17 +27,19 @@ LONG_COOKED
expect(EmailCook.new(long).cook).to eq(long_cooked.strip)
end
it 'autolinks' do
stub_request(:get, "https://www.eviltrout.com").to_return(body: "")
stub_request(:head, "https://www.eviltrout.com").to_return(body: "")
expect(EmailCook.new("https://www.eviltrout.com").cook).to eq("<a href='https://www.eviltrout.com'>https://www.eviltrout.com</a><br>")
it 'creates oneboxed link when the line contains only a link' do
expect(EmailCook.new("https://www.eviltrout.com").cook).to eq('<a href="https://www.eviltrout.com" class="onebox" target="_blank">https://www.eviltrout.com</a><br>')
end
it 'autolinks without the beginning of a line' do
expect(EmailCook.new("my site: https://www.eviltrout.com").cook).to eq("my site: <a href='https://www.eviltrout.com'>https://www.eviltrout.com</a><br>")
expect(EmailCook.new("my site: https://www.eviltrout.com").cook).to eq('my site: <a href="https://www.eviltrout.com">https://www.eviltrout.com</a><br>')
end
it 'autolinks without the end of a line' do
expect(EmailCook.new("https://www.eviltrout.com is my site").cook).to eq('<a href="https://www.eviltrout.com">https://www.eviltrout.com</a> is my site<br>')
end
it 'links even within a quote' do
expect(EmailCook.new("> https://www.eviltrout.com").cook).to eq("<blockquote><a href='https://www.eviltrout.com'>https://www.eviltrout.com</a><br></blockquote>")
expect(EmailCook.new("> https://www.eviltrout.com").cook).to eq('<blockquote><a href="https://www.eviltrout.com">https://www.eviltrout.com</a><br></blockquote>')
end
end