Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adds support for link auto extraction for short statuses. Works with …
…http, https, mailto and gopher (yeah gopher is still cool).

For this, I had to add a new html_preprocess method into the content_model that's called right before the text filter so URLs with _something_ inside them won't be transformed into html before they're turned into a link.

When using markdown or textile, displayed URLs with _ something _ are still broken, but OK inside the link. Maybe the solution would have been to keep this postprocess and replace the html tags with _. I'll think about that and provide a fix.
  • Loading branch information
Frédéric de Villamil committed Aug 2, 2013
1 parent f60a644 commit 093ec5e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/models/content_base.rb
Expand Up @@ -46,7 +46,8 @@ def html(field = :all)
# object.
def generate_html(field, text = nil)
text ||= self[field].to_s
html = (text_filter || default_text_filter).filter_text_for_content(blog, text, self) || text
prehtml = html_preprocess(field, text).to_s
html = (text_filter || default_text_filter).filter_text_for_content(blog, prehtml, self) || prehtml
html_postprocess(field,html).to_s
end

Expand All @@ -56,6 +57,10 @@ def html_postprocess(field, html)
html
end

def html_preprocess(field, html)
html
end

def html_map field
content_fields.include? field
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/status.rb
Expand Up @@ -28,7 +28,7 @@ def set_author(user)
self.user = user
end

def html_postprocess(field, html)
def html_preprocess(field, html)
PublifyApp::Textfilter::Twitterfilter.filtertext(nil,nil,html,nil).nofollowify
end

Expand Down
15 changes: 12 additions & 3 deletions lib/publify_textfilter_twitterfilter.rb
Expand Up @@ -5,16 +5,25 @@ class Twitterfilter < TextFilterPlugin::PostProcess
plugin_description 'Strip HTML tags'

def self.filtertext(blog,content,text,params)
text.to_s.split.grep(/^#\w+/) do |item|
# First, autolink
text = text.to_s
URI.extract(text, ["http", "https", "mailto", "gopher"]) do |item|
text = text.gsub(item, "<a href='#{item}'>#{item}</a>")
end

# hashtags
text.split.grep(/^#\w+/) do |item|
# strip_html because Ruby considers "#prouddad</p>" as a word
uri = URI.escape("https://twitter.com/search?q=#{item.strip_html}&src=tren&mode=realtime")
text = text.to_s.gsub(item, "<a href='#{uri}'>#{item.strip_html}</a>")
text = text.gsub(item, "<a href='#{uri}'>#{item.strip_html}</a>")
end

# @mention
text.to_s.split.grep(/@\w+/) do |item|
uri = URI.escape("https://twitter.com/#{item.strip_html.gsub('@', '')}")
text = text.to_s.gsub(item, "<a href='#{uri}'>#{item.strip_html}</a>")
text = text.gsub(item, "<a href='#{uri}'>#{item.strip_html}</a>")
end

return text
end
end
Expand Down
27 changes: 22 additions & 5 deletions spec/models/status_spec.rb
Expand Up @@ -14,14 +14,31 @@
describe "Testing hashtag and @mention replacement in html postprocessing" do
before(:each) do
FactoryGirl.create(:blog, :dofollowify => true)
@status = FactoryGirl.create(:status, :body => "A test tweet with a #hashtag and a @mention")
end

it "should replace a hashtag with a proper URL to Twitter search" do
status = FactoryGirl.create(:status, :body => "A test tweet with a #hashtag")
text = status.html_preprocess(status.body, status.body)
text.should == "A test tweet with a <a href='https://twitter.com/search?q=%23hashtag&src=tren&mode=realtime'>#hashtag</a>"
end

it "should replace the hastag with a proper URL to Twitter" do
text = @status.html_postprocess(@status.body, @status.body)

text.should == "A test tweet with a <a href='https://twitter.com/search?q=%23hashtag&src=tren&mode=realtime'>#hashtag</a> and a <a href='https://twitter.com/mention'>@mention</a>"
it "should replace a @mention by a proper URL to the twitter account" do
status = FactoryGirl.create(:status, :body => "A test tweet with a @mention")
text = status.html_preprocess(status.body, status.body)
text.should == "A test tweet with a <a href='https://twitter.com/mention'>@mention</a>"
end

it "should replace a http URL by a proper link" do
status = FactoryGirl.create(:status, :body => "A test tweet with a http://link.com")
text = status.html_preprocess(status.body, status.body)
text.should == "A test tweet with a <a href='http://link.com'>http://link.com</a>"
end

it "should replace a https URL with a proper link" do
status = FactoryGirl.create(:status, :body => "A test tweet with a https://link.com")
text = status.html_preprocess(status.body, status.body)
text.should == "A test tweet with a <a href='https://link.com'>https://link.com</a>"
end
end

describe 'Given the factory :status' do
Expand Down
28 changes: 27 additions & 1 deletion spec/models/text_filter_spec.rb
Expand Up @@ -38,8 +38,34 @@
it { should_not include(TextFilterPlugin::Macro) }
end

describe "#filter_text" do
describe "Twitter filter" do
def filter_text(text, filters, filterparams={})
TextFilter.filter_text(blog, text, self, filters, filterparams)
end

it "should replace a hashtag with a proper URL to Twitter search" do
text = filter_text("A test tweet with a #hashtag", [:twitterfilter])
text.should == "A test tweet with a <a href='https://twitter.com/search?q=%23hashtag&src=tren&mode=realtime'>#hashtag</a>"
end

it "should replace a @mention by a proper URL to the twitter account" do
text = filter_text("A test tweet with a @mention", [:twitterfilter])
text.should == "A test tweet with a <a href='https://twitter.com/mention'>@mention</a>"
end

it "should replace a http URL by a proper link" do
text = filter_text("A test tweet with a http://link.com", [:twitterfilter])
text.should == "A test tweet with a <a href='http://link.com'>http://link.com</a>"
end

it "should replace a https URL with a proper link" do
text = filter_text("A test tweet with a https://link.com", [:twitterfilter])
text.should == "A test tweet with a <a href='https://link.com'>https://link.com</a>"
end

end

describe "#filter_text" do
def filter_text(text, filters, filterparams={})
TextFilter.filter_text(blog, text, self, filters, filterparams)
end
Expand Down

0 comments on commit 093ec5e

Please sign in to comment.