Refactor regular expression used to find tags so that it supports unicode in both ruby 1.8 and ruby 1.9. NEVER SPEAK OF THIS AGAIN.

This commit is contained in:
Sarah Mei 2011-11-05 19:57:51 -07:00
parent 3e70398c40
commit 2143e0f9ba
3 changed files with 17 additions and 5 deletions

View file

@ -1,8 +1,13 @@
class ActsAsTaggableOn::Tag
def followed_count
@followed_count ||= TagFollowing.where(:tag_id => self.id).count
end
def self.tag_text_regexp
@@tag_text_regexp ||= (RUBY_VERSION.include?('1.9') ? "[[:alnum:]]_-" : "\\w-")
end
def self.autocomplete(name)
where("name LIKE ?", "#{name.downcase}%")
end
@ -12,7 +17,7 @@ class ActsAsTaggableOn::Tag
# Special case for love, because the world needs more love.
'<3'
elsif name
name.gsub(/[^\w-]/, '')
name.gsub(/[^#{self.tag_text_regexp}]/, '')
end
end
end

View file

@ -25,7 +25,7 @@ module Diaspora
end
def tag_strings
regex = /(?:^|\s)#([\w-]+|<3)/
regex = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u
matches = self.
send( self.class.field_with_tags ).
scan(regex).
@ -41,7 +41,7 @@ module Diaspora
return text if opts[:plain_text]
text = ERB::Util.h(text) unless opts[:no_escape]
regex = /(^|\s|>)#([\w-]+|&lt;3)/
regex =/(^|\s|>)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|&lt;3)/u
text.to_str.gsub(regex) { |matched_string|
pre, url_bit, clickable = $1, $2, "##{$2}"

View file

@ -24,8 +24,15 @@ describe Diaspora::Taggable do
@object.save!
end
it 'links the tag to /p' do
Diaspora::Taggable.format_tags(@str).should include( tag_link('vöglein') )
it "supports non-ascii characters" do
@object.tags(true).map(&:name).should include('vöglein')
end
it 'links each tag' do
formatted_string = Diaspora::Taggable.format_tags(@str)
formatted_string.should include(tag_link('what'))
formatted_string.should include(tag_link('hey'))
formatted_string.should include(tag_link('vöglein'))
end
it 'responds to plain_text' do