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