Do some console tests with that just to be sure it matches what you
want, but it's the best regexp I've found for URL matching... i.e. the
most complete. Note, you can grab various parts using \1, \2, etc. \0
just grabs the whole match.
As for the smilies, I have no idea... that would be a much more
complex matching algorithm. It sounds like an ideal candidate for a
plugin... i.e. "acts_as_smiley".
Is there a more efficient way of doing GSUB, instead of doing gsub on
entire text for each smiley? As the list of Smileys grows big, gsub in
loop will be a overhead.
Is there a more efficient way of doing GSUB, instead of doing gsub on
entire text for each smiley? As the list of Smileys grows big, gsub in
loop will be a overhead.
well first of all, i'd worry about that when that became a problem rather than assuming it might.
Secondly you could probably use something like
smileys = {'smiley1' => 'xxx', 'smiley2' => 'yyy', 'smiley1' => 'zzz'
text.gsub /(smiley1)|(smiley2)|(smiley3)/ do |match|
smileys[match]
end
text = (text).gsub
/(8-O)|(:-\))|(:-\()|(:-\/)|(:-\\)|(:-\?)|(:-D)|(:-P)|(:-o)|(:-O)|(:-X)|(:-x)|(:-\|)|(;-\))|(\^_\^)|(:\?:)|(:!:)|(LOL)/
do |match| image_tag(image_path("smileys"+"/"+smileys[match])) end
It occurs to me that YAML could be a real boon here. I.e. define your
mappings via YAML (or XML but who does that anymore? *grin*). Probably
define it as something simple like:
mappings:
icon_eek.gif: 8-O
icon_sad.gif:
etc.
then read it into a very similar hash table to what you have, maybe
reversing the keys if need be.
I think it would be cool to have an "acts_as_smiley" plugin. Maybe
something where you could do:
class Post < ActiveRecord::Base
acts_as_smiley :body, :icons => "myicons" # (this would be a subdir
of "images" in public)
end
Then, "underneath the covers", it redefines the body accessor so that
it returns the smiley-parsed value of :body.
I could even imagine some other hooks like defining additional
accessors, like :body_nosmiley or :body_smiley that skips or runs the
parser.