Skip to content

Commit

Permalink
Adds a text filter to turn @mention and #hashtags into links to Twitt…
Browse files Browse the repository at this point in the history
…er inside short statuses.

This is done using the html_postprocess method included in the content model.

html_postprocess is applied when views call @content.html(:field) (usually :body). In article / pages it just returns the html, but is used in the feedback model to sanitize user posted HTML.

On a more or less related thing, I've just dug into the text filter code for the first time since I took over the project. I've been willing to add Wordpress like pre and post hooks to allow user defined plugins to change the text on the fly. This was actually already implemented into Typo with the preprocess and post process text filters. Currently, the simplest way to do is to add filters params to the existing 4 text filters (none, markdown, textile and markdown + smartypants). I'll come with an elegant way to do it, already have 2-3 ideas about that.
  • Loading branch information
Frédéric de Villamil committed Aug 2, 2013
1 parent a3adb8d commit f60a644
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/status.rb
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions 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</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>")
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, "<a href='#{uri}'>#{item.strip_html}</a>")
end
return text
end
end
end
end
13 changes: 13 additions & 0 deletions spec/models/status_spec.rb
Expand Up @@ -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 <a href='https://twitter.com/search?q=%23hashtag&src=tren&mode=realtime'>#hashtag</a> and a <a href='https://twitter.com/mention'>@mention</a>"
end
end

describe 'Given the factory :status' do
before(:each) do
FactoryGirl.create(:blog)
Expand Down
7 changes: 7 additions & 0 deletions spec/models/text_filter_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 <a href='https://twitter.com/search?q=%23hashtag&src=tren&mode=realtime'>#hashtag</a> and a <a href='https://twitter.com/mention'>@mention</a>"
end

it "smartypants" do
build_stubbed(:smartypants)
text = filter_text('"foo"',[:smartypants])
Expand Down

0 comments on commit f60a644

Please sign in to comment.