class HTML::Pipeline::Gitlab::GitlabEmojiFilter

HTML filter that replaces :emoji: with images.

Private Class Methods

emoji_pattern() click to toggle source

Build a regexp that matches all valid :emoji: names.

# File lib/html/pipeline/gitlab/gitlab_emoji_filter.rb, line 63
def self.emoji_pattern
  @emoji_pattern ||= /:(#{Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/
end

Public Instance Methods

call() click to toggle source
# File lib/html/pipeline/gitlab/gitlab_emoji_filter.rb, line 11
def call
  search_text_nodes(doc).each do |node|
    content = node.to_html
    next if !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/gitlab/gitlab_emoji_filter.rb, line 33
def emoji_image_filter(text)
  return text unless text.include?(':')

  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/gitlab/gitlab_emoji_filter.rb, line 25
def validate
end

Private Instance Methods

emoji_filename(name) click to toggle source
# File lib/html/pipeline/gitlab/gitlab_emoji_filter.rb, line 71
def emoji_filename(name)
  "#{Emoji.emoji_filename(name)}.png"
end
emoji_pattern() click to toggle source
# File lib/html/pipeline/gitlab/gitlab_emoji_filter.rb, line 67
def emoji_pattern
  self.class.emoji_pattern
end
emoji_url(name) click to toggle source
# File lib/html/pipeline/gitlab/gitlab_emoji_filter.rb, line 44
def emoji_url(name)
  emoji_path = "emoji/#{emoji_filename(name)}"
  if context[:asset_host]
    # Asset host is specified.
    url_to_image(emoji_path)
  elsif context[:asset_root]
    # Gitlab url is specified
    File.join(context[:asset_root], url_to_image(emoji_path))
  else
    # All other cases
    url_to_image(emoji_path)
  end
end
url_to_image(image) click to toggle source
# File lib/html/pipeline/gitlab/gitlab_emoji_filter.rb, line 58
def url_to_image(image)
  ActionController::Base.helpers.url_to_image(image)
end