class HTML::Pipeline::NegarMojiHtmlPipeline::NegarehEmojiFilter

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 ":file_name" is used.

:extension (optional) - extension to be use for emoji files, default
extension is svg.

: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

Public Class Methods

emoji_names() click to toggle source
# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 102
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/negarmoji-pipeline/filter.rb, line 96
def self.emoji_pattern
  @emoji_pattern ||= %r{
  :(#{emoji_names.map { |name| Regexp.escape(name) }.join("|")}):
}x
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 “: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/negarmoji-pipeline/filter.rb, line 80
def asset_path(name)
  if context[:asset_path]
    context[:asset_path].gsub(":file_name", emoji_filename(name))
  else
    File.join(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/negarmoji-pipeline/filter.rb, line 69
def asset_root
  context[:asset_root]
end
call() click to toggle source
# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 32
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
emoji_extension() click to toggle source

Emoji file extension.

Extension to be use for emoji files, default extension is svg.

# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 91
def emoji_extension
  context[:extension] || "svg"
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/negarmoji-pipeline/filter.rb, line 59
def emoji_image_filter(text)
  text.gsub(emoji_pattern) do |_match|
    emoji_image_tag(Regexp.last_match(1))
  end
end
validate() click to toggle source

Implementation of validate hook. Errors should raise exceptions or use an existing validator.

# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 50
def validate
  needs :asset_root
end

Private Instance Methods

default_img_attrs(name) click to toggle source

Default attributes for img tag.

# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 123
def default_img_attrs(name)
  # extract emoji info.
  emoji = Emoji.find_by_alias(name)
  # default attrs.
  {
    :class  => "emoji",
    :title  => emoji.name.to_s,
    :alt    => emoji.raw.to_s,
    :src    => emoji_url(name).to_s,
    :height => "20",
    :width  => "20",
    :align  => "absmiddle"
  }
end
emoji_filename(name) click to toggle source
# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 146
def emoji_filename(name)
  Emoji.find_by_alias(name).image_filename(emoji_extension)
end
emoji_image_tag(name) click to toggle source

Build an emoji image tag.

# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 109
def emoji_image_tag(name)
  require "active_support/core_ext/hash/indifferent_access"
  html_attrs = default_img_attrs(name)
                 .merge!((context[:img_attrs] || {}))
                 .map do |attr, value|
    call_respond = value.respond_to?(:call)
    !value.nil? && %(#{attr}="#{call_respond && value.call(name) || value}")
  end
                 .reject(&:blank?).join(" ")

  "<img #{html_attrs}>"
end
emoji_pattern() click to toggle source
# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 142
def emoji_pattern
  self.class.emoji_pattern
end
emoji_url(name) click to toggle source
# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 138
def emoji_url(name)
  File.join(asset_root, asset_path(name))
end
ignored_ancestor_tags() click to toggle source

Return ancestor tags to stop the emojification.

@return [Array<String>] Ancestor tags.

# File lib/html/pipeline/negarmoji-pipeline/filter.rb, line 153
def ignored_ancestor_tags
  if context[:ignored_ancestor_tags]
    DEFAULT_IGNORED_ANCESTOR_TAGS | context[:ignored_ancestor_tags]
  else
    DEFAULT_IGNORED_ANCESTOR_TAGS
  end
end