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. :ignored_ancestor_tags (optional) - Tags to stop the emojification. Node has matched ancestor HTML tags will not be emojified. Default to pre, code, and tt tags. Extra tags please pass in the form of array, e.g., %w(blockquote summary). :img_attrs (optional) - Attributes for generated img tag. E.g. Pass { "draggble" => true, "height" => nil } to set draggable attribute to "true" and clear height attribute of generated img tag.
Constants
- DEFAULT_IGNORED_ANCESTOR_TAGS
Private Class Methods
# File lib/html/pipeline_plus/emoji_filter.rb, line 105 def self.emoji_names Emoji.all.map(&:aliases).flatten.sort end
Build a regexp that matches all valid :emoji: names.
# File lib/html/pipeline_plus/emoji_filter.rb, line 97 def self.emoji_pattern @emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/ end
Public Instance Methods
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_plus/emoji_filter.rb, line 57 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
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_plus/emoji_filter.rb, line 49 def asset_root context[:asset_root] end
# File lib/html/pipeline_plus/emoji_filter.rb, line 16 def call doc.search('.//text()').each do |node| content = node.text next unless content.include?(':') next if has_ancestor?(node, ignored_ancestor_tags) html = emoji_image_filter(content) next if html == content node.replace(html) end doc end
Replace :emoji: with corresponding images.
text - String text to replace :emoji: in.
Returns a String with :emoji: replaced with images.
# File lib/html/pipeline_plus/emoji_filter.rb, line 39 def emoji_image_filter(text) text.gsub(emoji_pattern) do |_match| emoji_image_tag(Regexp.last_match(1)) end end
Implementation of validate hook. Errors should raise exceptions or use an existing validator.
# File lib/html/pipeline_plus/emoji_filter.rb, line 30 def validate needs :asset_root end
Private Instance Methods
Default attributes for img tag
# File lib/html/pipeline_plus/emoji_filter.rb, line 80 def default_img_attrs(name) { 'class' => 'emoji'.freeze, 'title' => ":#{name}:", 'alt' => ":#{name}:", 'src' => emoji_url(name).to_s, 'align' => 'absmiddle'.freeze, 'width' => '20'.freeze, 'height' => '20'.freeze } end
# File lib/html/pipeline_plus/emoji_filter.rb, line 109 def emoji_filename(name) Emoji.find_by_alias(name).image_filename end
Build an emoji image tag
# File lib/html/pipeline_plus/emoji_filter.rb, line 68 def emoji_image_tag(name) require 'active_support/core_ext/hash/indifferent_access' html_attrs = default_img_attrs(name) .merge!((context[:img_attrs] || {}).with_indifferent_access) .map { |attr, value| !value.nil? && %(#{attr}="#{value.respond_to?(:call) && value.call(name) || value}") } .reject(&:blank?).join(' '.freeze) "<img #{html_attrs} />" end
# File lib/html/pipeline_plus/emoji_filter.rb, line 101 def emoji_pattern self.class.emoji_pattern end
# File lib/html/pipeline_plus/emoji_filter.rb, line 92 def emoji_url(name) File.join(asset_root, asset_path(name)) end