diff --git a/app/models/content_base.rb b/app/models/content_base.rb index bcee651879..f395362124 100644 --- a/app/models/content_base.rb +++ b/app/models/content_base.rb @@ -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 @@ -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 diff --git a/app/models/status.rb b/app/models/status.rb index e62c588331..88dbf7af14 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -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 diff --git a/lib/publify_textfilter_twitterfilter.rb b/lib/publify_textfilter_twitterfilter.rb index 8bcbb9b9c9..3df654306c 100644 --- a/lib/publify_textfilter_twitterfilter.rb +++ b/lib/publify_textfilter_twitterfilter.rb @@ -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, "#{item}") + end + + # hashtags + text.split.grep(/^#\w+/) do |item| # strip_html because Ruby considers "#prouddad

" as a word uri = URI.escape("https://twitter.com/search?q=#{item.strip_html}&src=tren&mode=realtime") - text = text.to_s.gsub(item, "#{item.strip_html}") + text = text.gsub(item, "#{item.strip_html}") 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, "#{item.strip_html}") + text = text.gsub(item, "#{item.strip_html}") end + return text end end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index f8b2b3190b..6023903f8b 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -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 #hashtag" + 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 #hashtag and a @mention" + 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 @mention" + 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 http://link.com" 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 https://link.com" + end end describe 'Given the factory :status' do diff --git a/spec/models/text_filter_spec.rb b/spec/models/text_filter_spec.rb index a120ce13cf..1993434a0b 100644 --- a/spec/models/text_filter_spec.rb +++ b/spec/models/text_filter_spec.rb @@ -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 #hashtag" + 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 @mention" + 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 http://link.com" + 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 https://link.com" + end + + end + + describe "#filter_text" do def filter_text(text, filters, filterparams={}) TextFilter.filter_text(blog, text, self, filters, filterparams) end