class HTML::Pipeline::EmojiFilter
HTML filter that replaces :emoji: with images.
Context:
: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.
Private Class Methods
emoji_names()
click to toggle source
# File lib/html/pipeline/emoji_filter.rb, line 85 def self.emoji_names Emoji.all.map(&:aliases).flatten.sort end
emoji_pattern()
click to toggle source
Build a regexp that matches all valid :emoji: names.
# File lib/html/pipeline/emoji_filter.rb, line 74 def self.emoji_pattern @emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/ end
Public Instance Methods
asset_path(name)
click to toggle source
The url path to link emoji sprites
: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. Returns the context's #asset_path or the default path if no context #asset_path is given.
# File lib/html/pipeline/emoji_filter.rb, line 59 def asset_path(name) if context[:asset_path] context[:asset_path].gsub(":file_name", emoji_filename(name)) else File.join("emoji", emoji_filename(name)) end end
asset_root()
click to toggle source
The base url to link emoji sprites
Raises ArgumentError if context option has not been provided. Returns the context's asset_root.
# File lib/html/pipeline/emoji_filter.rb, line 51 def asset_root context[:asset_root] end
call()
click to toggle source
# File lib/html/pipeline/emoji_filter.rb, line 17 def call search_text_nodes(doc).each do |node| content = node.to_html next unless content.include?(':') next if has_ancestor?(node, %w(pre code)) html = emoji_image_filter(content) next if html == content node.replace(html) end doc end
emoji_image_filter(text)
click to toggle source
Replace :emoji: with corresponding images.
text - String text to replace :emoji: in.
Returns a String with :emoji: replaced with images.
# File lib/html/pipeline/emoji_filter.rb, line 40 def emoji_image_filter(text) 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 end
validate()
click to toggle source
Implementation of validate hook. Errors should raise exceptions or use an existing validator.
# File lib/html/pipeline/emoji_filter.rb, line 31 def validate needs :asset_root end
Private Instance Methods
emoji_filename(name)
click to toggle source
# File lib/html/pipeline/emoji_filter.rb, line 89 def emoji_filename(name) Emoji.find_by_alias(name).image_filename end
emoji_pattern()
click to toggle source
# File lib/html/pipeline/emoji_filter.rb, line 78 def emoji_pattern self.class.emoji_pattern end
emoji_url(name)
click to toggle source
# File lib/html/pipeline/emoji_filter.rb, line 69 def emoji_url(name) File.join(asset_root, asset_path(name)) end