2017-07-14 13:47:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module EmojiHelper
|
|
|
|
def emojify(text)
|
|
|
|
return text if text.blank?
|
|
|
|
|
2017-07-20 22:27:40 -04:00
|
|
|
text.gsub(emoji_pattern) do |match|
|
|
|
|
emoji = Emoji.instance.unicode($1) # rubocop:disable Style/PerlBackrefs
|
2017-07-14 13:47:53 -04:00
|
|
|
|
|
|
|
if emoji
|
2017-07-20 22:27:40 -04:00
|
|
|
emoji
|
2017-07-14 13:47:53 -04:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-20 22:27:40 -04:00
|
|
|
|
|
|
|
def emoji_pattern
|
|
|
|
@emoji_pattern ||=
|
|
|
|
/(?<=[^[:alnum:]:]|\n|^)
|
|
|
|
(#{Emoji.instance.names.map { |name| Regexp.escape(name) }.join('|')})
|
|
|
|
(?=[^[:alnum:]:]|$)/x
|
|
|
|
end
|
2017-07-14 13:47:53 -04:00
|
|
|
end
|