summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-10-29 08:46:56 +0000
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-11-10 16:14:40 +0100
commitc8f807083d90d0d9382b0997643a04c0d5476f36 (patch)
treec2b7d9b91fb0a82a544b7fae34b66407fb9d5550
parente98cdeebd8ad328529e8e3263c4f506562d99b6e (diff)
downloadgitlab-ce-zj-fuck-it-lets-have-custom-emoji.tar.gz
WIP: trying to update the regexzj-fuck-it-lets-have-custom-emoji
[ci skip]
-rw-r--r--TODO2
-rw-r--r--lib/banzai/filter/emoji_filter.rb36
2 files changed, 36 insertions, 2 deletions
diff --git a/TODO b/TODO
new file mode 100644
index 00000000000..eb08baa7a42
--- /dev/null
+++ b/TODO
@@ -0,0 +1,2 @@
+- Clean cache for project when a custom emoji is added/deleted?
+
diff --git a/lib/banzai/filter/emoji_filter.rb b/lib/banzai/filter/emoji_filter.rb
index a8c1ca0c60a..721ad9b12a8 100644
--- a/lib/banzai/filter/emoji_filter.rb
+++ b/lib/banzai/filter/emoji_filter.rb
@@ -24,6 +24,7 @@ module Banzai
node.replace(html)
end
+
doc
end
@@ -33,10 +34,21 @@ module Banzai
#
# Returns a String with :emoji: replaced with images.
def emoji_name_image_filter(text)
- text.gsub(emoji_pattern) do |match|
+ with_emoji = text.dup
+
+ with_emoji.gsub!(emoji_pattern) do |match|
name = $1
emoji_image_tag(name, emoji_url(name))
end
+
+ if custom_emoji_pattern
+ with_emoji.gsub!(custom_emoji_pattern) do
+ name = $1
+ custom_emoji_image_tag(name)
+ end
+ end
+
+ with_emoji
end
# Replace unicode emoji with corresponding images if they exist.
@@ -54,9 +66,21 @@ module Banzai
"<img class='emoji' title=':#{emoji_name}:' alt=':#{emoji_name}:' src='#{emoji_url}' height='20' width='20' align='absmiddle' />"
end
+ def custom_emoji_image_tag(name)
+ emoji_url = project.custom_emoji.find_by(name: name).emoji_url
+
+ emoji_image_tag(name, emoji_url)
+ end
+
+ def custom_emoji_pattern
+ return nil unless project
+
+ @custom_emoji_pattern ||= self.class.emoji_detect_regex(project.custom_emoji.map(&:name))
+ end
+
# Build a regexp that matches all valid :emoji: names.
def self.emoji_pattern
- @emoji_pattern ||= /:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/
+ @emoji_pattern ||= emoji_detect_regex(Gitlab::Emoji.emojis_names)
end
# Build a regexp that matches all valid unicode emojis names.
@@ -64,6 +88,10 @@ module Banzai
@emoji_unicode_pattern ||= /(#{Gitlab::Emoji.emojis_unicodes.map { |moji| Regexp.escape(moji) }.join('|')})/
end
+ def self.emoji_detect_regex(names)
+ /:(#{names.map { |name| Regexp.escape(name) }.join('|')}):/
+ end
+
private
def emoji_url(name)
@@ -93,6 +121,10 @@ module Banzai
end
end
+ def project
+ context[:project]
+ end
+
def url_to_image(image)
ActionController::Base.helpers.url_to_image(image)
end