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