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