Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with gemoji v2 #129

Merged
merged 2 commits into from
Jul 7, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class Pipeline
# :asset_root (required) - base url to link to emoji sprite
# :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.
class EmojiFilter < Filter
# Build a regexp that matches all valid :emoji: names.
EmojiPattern = /:(#{Emoji.names.map { |name| Regexp.escape(name) }.join('|')}):/

def call
doc.search('text()').each do |node|
content = node.to_html
Expand All @@ -43,7 +40,7 @@ def validate
def emoji_image_filter(text)
return text unless text.include?(':')

text.gsub EmojiPattern do |match|
text.gsub(emoji_pattern) do |match|
name = $1
"<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />"
end
Expand All @@ -63,9 +60,9 @@ def asset_root
# Returns the context's asset_path or the default path if no context asset_path is given.
def asset_path(name)
if context[:asset_path]
context[:asset_path].gsub(":file_name", "#{::CGI.escape(name)}.png")
context[:asset_path].gsub(":file_name", emoji_filename(name))
else
File.join("emoji", "#{::CGI.escape(name)}.png")
File.join("emoji", emoji_filename(name))
end
end

Expand All @@ -74,6 +71,35 @@ def asset_path(name)
def emoji_url(name)
File.join(asset_root, asset_path(name))
end

# Build a regexp that matches all valid :emoji: names.
def self.emoji_pattern
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why'd you refactor EmojiPattern into methods? 👍 the interface, and maybe we can refactor other filters with Regexp constants? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. You reference emoji_names. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, one can now override the method in a subclass 😉

@emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/
end

def emoji_pattern
self.class.emoji_pattern
end

# Detect gemoji v2 which has a new API
# https://github.com/jch/html-pipeline/pull/129
if Emoji.respond_to?(:all)
def self.emoji_names
Emoji.all.map(&:aliases).flatten.sort
end

def emoji_filename(name)
Emoji.find_by_alias(name).image_filename
end
else
def self.emoji_names
Emoji.names
end

def emoji_filename(name)
"#{::CGI.escape(name)}.png"
end
end
end
end
end