debian-mirror-gitlab/lib/banzai/filter/emoji_filter.rb

83 lines
2.5 KiB
Ruby
Raw Normal View History

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
2021-01-29 00:20:46 +05:30
IGNORE_UNICODE_EMOJIS = %w(™ © ®).freeze
2015-09-11 14:41:01 +05:30
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|
2020-11-24 15:15:51 +05:30
name = Regexp.last_match(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
2021-01-29 00:20:46 +05:30
@emoji_unicode_pattern ||=
begin
filtered_emojis = Gitlab::Emoji.emojis_unicodes - IGNORE_UNICODE_EMOJIS
/(#{filtered_emojis.map { |moji| Regexp.escape(moji) }.join('|')})/
end
2016-11-03 12:29:30 +05:30
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