Skip to content

Commit

Permalink
Fix issue with hastag parsing (https://github.com/hotsh/rstat.us/issu…
Browse files Browse the repository at this point in the history
  • Loading branch information
crossblaim committed Mar 26, 2011
1 parent 41c5afd commit 8b63e32
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 5 additions & 3 deletions models/update.rb
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ class Update

key :remote_url
key :referral_id

def referral
Update.first(:id => referral_id)
end
@@ -31,7 +31,7 @@ def to_html

# we let almost anything be in a username, except those that mess with urls. but you can't end in a .:;, or !
#also ignore container chars [] () "" '' {}
# XXX: the _correct_ solution will be to use an email validator
# XXX: the _correct_ solution will be to use an email validator
out.gsub!(/(^|[ \t\n\r\f"'\(\[{]+)@([^ \t\n\r\f&?=@%\/\#]*[^ \t\n\r\f&?=@%\/\#.!:;,"'\]}\)])/) do |match|
if u = User.first(:username => /^#{$2}$/i)
"#{$1}<a href='/users/#{u.username}'>@#{$2}</a>"
@@ -40,7 +40,9 @@ def to_html
end
end
out.gsub!(/(http[s]?:\/\/\S+[a-zA-Z0-9\/])/, "<a href='\\1'>\\1</a>")
out.gsub!(/#(\w+)/, "<a href='/hashtags/\\1'>#\\1</a>")
out.gsub!(/(^|\s+)#(\w+)/) do |match|
"#{$1}<a href='/hashtags/#{$2}'>##{$2}</a>"
end
out
end

4 changes: 4 additions & 0 deletions test/update_test.rb
Original file line number Diff line number Diff line change
@@ -30,11 +30,15 @@ def test_at_replies
def test_links
u = Update.new(:text => "This is a message mentioning http://rstat.us/.")
assert_match /<a href='http:\/\/rstat.us\/'>http:\/\/rstat.us\/<\/a>/, u.to_html
u = Update.new(:text => "https://github.com/hotsh/rstat.us/issues#issue/11")
assert_equal "<a href='https://github.com/hotsh/rstat.us/issues#issue/11'>https://github.com/hotsh/rstat.us/issues#issue/11</a>", u.to_html
end

def test_hashtags
u = Update.new(:text => "This is a message with a #hashtag.")
assert_match /<a href='\/hashtags\/hashtag'>#hashtag<\/a>/, u.to_html
u = Update.new(:text => "This is a message with a#hashtag.")
assert_equal "This is a message with a#hashtag.", u.to_html
end

end

0 comments on commit 8b63e32

Please sign in to comment.