2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/emoji.js
|
2015-12-23 02:04:40 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
2016-11-03 12:29:30 +05:30
|
|
|
# HTML filter that replaces :emoji: and unicode with images.
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
|
|
|
# Based on HTML::Pipeline::EmojiFilter
|
|
|
|
class EmojiFilter < HTML::Pipeline::Filter
|
|
|
|
IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set
|
|
|
|
|
|
|
|
def call
|
2018-05-09 12:01:36 +05:30
|
|
|
doc.search(".//text()").each do |node|
|
2015-09-11 14:41:01 +05:30
|
|
|
content = node.to_html
|
|
|
|
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
next unless content.include?(':') || node.text.match(emoji_unicode_pattern)
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
html = emoji_unicode_element_unicode_filter(content)
|
|
|
|
html = emoji_name_element_unicode_filter(html)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
next if html == content
|
|
|
|
|
|
|
|
node.replace(html)
|
|
|
|
end
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Replace :emoji: with corresponding gl-emoji unicode.
|
2015-09-11 14:41:01 +05:30
|
|
|
#
|
|
|
|
# text - String text to replace :emoji: in.
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
# Returns a String with :emoji: replaced with gl-emoji unicode.
|
|
|
|
def emoji_name_element_unicode_filter(text)
|
2015-09-11 14:41:01 +05:30
|
|
|
text.gsub(emoji_pattern) do |match|
|
|
|
|
name = $1
|
2017-08-17 22:00:37 +05:30
|
|
|
Gitlab::Emoji.gl_emoji_tag(name)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Replace unicode emoji with corresponding gl-emoji unicode.
|
2016-11-03 12:29:30 +05:30
|
|
|
#
|
|
|
|
# text - String text to replace unicode emoji in.
|
|
|
|
#
|
2017-08-17 22:00:37 +05:30
|
|
|
# Returns a String with unicode emoji replaced with gl-emoji unicode.
|
|
|
|
def emoji_unicode_element_unicode_filter(text)
|
2016-11-03 12:29:30 +05:30
|
|
|
text.gsub(emoji_unicode_pattern) do |moji|
|
2017-08-17 22:00:37 +05:30
|
|
|
emoji_info = Gitlab::Emoji.emojis_by_moji[moji]
|
|
|
|
Gitlab::Emoji.gl_emoji_tag(emoji_info['name'])
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# Build a regexp that matches all valid :emoji: names.
|
|
|
|
def self.emoji_pattern
|
2017-08-17 22:00:37 +05:30
|
|
|
@emoji_pattern ||=
|
2018-03-17 18:26:18 +05:30
|
|
|
%r{(?<=[^[:alnum:]:]|\n|^)
|
2017-08-17 22:00:37 +05:30
|
|
|
:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):
|
2018-03-17 18:26:18 +05:30
|
|
|
(?=[^[:alnum:]:]|$)}x
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
# Build a regexp that matches all valid unicode emojis names.
|
|
|
|
def self.emoji_unicode_pattern
|
|
|
|
@emoji_unicode_pattern ||= /(#{Gitlab::Emoji.emojis_unicodes.map { |moji| Regexp.escape(moji) }.join('|')})/
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def emoji_pattern
|
|
|
|
self.class.emoji_pattern
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def emoji_unicode_pattern
|
|
|
|
self.class.emoji_unicode_pattern
|
|
|
|
end
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|