Skip to content

Commit ba2fee0

Browse files
committed
Feature-detect gemoji v2 on load time, not runtime
Allows subclasses to augment: - EmojiFilter.emoji_names - EmojiFilter#emoji_pattern - EmojiFilter#emoji_filename(name)
1 parent 50142e2 commit ba2fee0

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

lib/html/pipeline/emoji_filter.rb

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class Pipeline
1414
# :asset_root (required) - base url to link to emoji sprite
1515
# :asset_path (optional) - url path to link to emoji sprite. :file_name can be used as a placeholder for the sprite file name. If no asset_path is set "emoji/:file_name" is used.
1616
class EmojiFilter < Filter
17-
# Build a regexp that matches all valid :emoji: names.
18-
emoji_names = Emoji.respond_to?(:all) ? Emoji.all.map(&:name) : Emoji.names
19-
EmojiPattern = /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
20-
2117
def call
2218
doc.search('text()').each do |node|
2319
content = node.to_html
@@ -44,7 +40,7 @@ def validate
4440
def emoji_image_filter(text)
4541
return text unless text.include?(':')
4642

47-
text.gsub EmojiPattern do |match|
43+
text.gsub(emoji_pattern) do |match|
4844
name = $1
4945
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
5046
end
@@ -63,17 +59,10 @@ def asset_root
6359
# :file_name can be used in the asset_path as a placeholder for the sprite file name. If no asset_path is set in the context "emoji/:file_name" is used.
6460
# Returns the context's asset_path or the default path if no context asset_path is given.
6561
def asset_path(name)
66-
if Emoji.respond_to?(:find_by_alias)
67-
emoji = Emoji.find_by_alias(name)
68-
image_filename = emoji.image_filename
69-
else
70-
image_filename = "#{::CGI.escape(name)}.png"
71-
end
72-
7362
if context[:asset_path]
74-
context[:asset_path].gsub(":file_name", image_filename)
63+
context[:asset_path].gsub(":file_name", emoji_filename(name))
7564
else
76-
File.join("emoji", image_filename)
65+
File.join("emoji", emoji_filename(name))
7766
end
7867
end
7968

@@ -82,6 +71,35 @@ def asset_path(name)
8271
def emoji_url(name)
8372
File.join(asset_root, asset_path(name))
8473
end
74+
75+
# Build a regexp that matches all valid :emoji: names.
76+
def self.emoji_pattern
77+
@emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
78+
end
79+
80+
def emoji_pattern
81+
self.class.emoji_pattern
82+
end
83+
84+
# Detect gemoji v2 which has a new API
85+
# https://github.com/jch/html-pipeline/pull/129
86+
if Emoji.respond_to?(:all)
87+
def self.emoji_names
88+
Emoji.all.map(&:aliases).flatten.sort
89+
end
90+
91+
def emoji_filename(name)
92+
Emoji.find_by_alias(name).image_filename
93+
end
94+
else
95+
def self.emoji_names
96+
Emoji.names
97+
end
98+
99+
def emoji_filename(name)
100+
"#{::CGI.escape(name)}.png"
101+
end
102+
end
85103
end
86104
end
87105
end

0 commit comments

Comments
 (0)