Simplify regex madness

This commit is contained in:
Raphael Sofaer 2011-03-10 17:12:04 -08:00
parent c1bcfef90c
commit f19857cc40

View file

@ -44,15 +44,14 @@ class StatusMessage < Post
regex = /@\{([^;]+); ([^\}]+)\}/ regex = /@\{([^;]+); ([^\}]+)\}/
escaped_message = opts[:plain_text] ? raw_message : ERB::Util.h(raw_message) escaped_message = opts[:plain_text] ? raw_message : ERB::Util.h(raw_message)
form_message = escaped_message.gsub(regex) do |matched_string| form_message = escaped_message.gsub(regex) do |matched_string|
inner_captures = matched_string.match(regex).captures
person = people.detect{ |p| person = people.detect{ |p|
p.diaspora_handle == inner_captures.last p.diaspora_handle == $~[2]
} }
if opts[:plain_text] if opts[:plain_text]
person ? ERB::Util.h(person.name) : ERB::Util.h(inner_captures.first) person ? ERB::Util.h(person.name) : ERB::Util.h($~[1])
else else
person ? "<a href=\"/people/#{person.id}\" class=\"mention\">@#{ERB::Util.h(person.name)}</a>" : ERB::Util.h(inner_captures.first) person ? "<a href=\"/people/#{person.id}\" class=\"mention\">@#{ERB::Util.h(person.name)}</a>" : ERB::Util.h($~[1])
end end
end end
form_message form_message
@ -94,7 +93,7 @@ class StatusMessage < Post
end end
def tag_strings def tag_strings
regex = /(^|\s)#(\w+)/ regex = /(?:^|\s)#(\w+)/
matches = self.raw_message.scan(regex).map do |match| matches = self.raw_message.scan(regex).map do |match|
match.last match.last
end end