diff --git a/app/models/status.rb b/app/models/status.rb index 12324b59a0..e62c588331 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -28,6 +28,10 @@ def set_author(user) self.user = user end + def html_postprocess(field, html) + PublifyApp::Textfilter::Twitterfilter.filtertext(nil,nil,html,nil).nofollowify + end + def initialize(*args) super # Yes, this is weird - PDC diff --git a/lib/publify_textfilter_twitterfilter.rb b/lib/publify_textfilter_twitterfilter.rb new file mode 100644 index 0000000000..8bcbb9b9c9 --- /dev/null +++ b/lib/publify_textfilter_twitterfilter.rb @@ -0,0 +1,22 @@ +class PublifyApp + class Textfilter + class Twitterfilter < TextFilterPlugin::PostProcess + plugin_display_name "HTML Filter" + plugin_description 'Strip HTML tags' + + def self.filtertext(blog,content,text,params) + text.to_s.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}") + end + + 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}") + end + return text + end + end + end +end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 42d1714a49..f8b2b3190b 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -11,6 +11,19 @@ end end +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 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" + end +end + describe 'Given the factory :status' do before(:each) do FactoryGirl.create(:blog) diff --git a/spec/models/text_filter_spec.rb b/spec/models/text_filter_spec.rb index 0ef3618ad6..a120ce13cf 100644 --- a/spec/models/text_filter_spec.rb +++ b/spec/models/text_filter_spec.rb @@ -19,6 +19,7 @@ it { should include(PublifyApp::Textfilter::Flickr) } it { should include(PublifyApp::Textfilter::Code) } it { should include(PublifyApp::Textfilter::Lightbox) } + it { should include(PublifyApp::Textfilter::Twitterfilter) } it { should_not include(TextFilterPlugin::Markup) } it { should_not include(TextFilterPlugin::Macro) } end @@ -32,6 +33,7 @@ it { should include(PublifyApp::Textfilter::Flickr) } it { should include(PublifyApp::Textfilter::Code) } it { should include(PublifyApp::Textfilter::Lightbox) } + it { should_not include(PublifyApp::Textfilter::Twitterfilter) } it { should_not include(TextFilterPlugin::Markup) } it { should_not include(TextFilterPlugin::Macro) } end @@ -47,6 +49,11 @@ def filter_text(text, filters, filterparams={}) text.should == '*foo*' end + it "Twitter" do + text = filter_text("A test tweet with a #hashtag and a @mention", [:twitterfilter]) + text.should == "A test tweet with a #hashtag and a @mention" + end + it "smartypants" do build_stubbed(:smartypants) text = filter_text('"foo"',[:smartypants])