class HTML::Pipeline::EmojiFilter

HTML filter that replaces :emoji: with images.

Context:

:asset_root (required) - base url to link to emoji sprite

Constants

EmojiPattern

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

Public Instance Methods

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 49
def asset_root
  context[:asset_root]
end
call() click to toggle source
# File lib/html/pipeline/emoji_filter.rb, line 13
def call
  doc.search('text()').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/emoji_filter.rb, line 36
def emoji_image_filter(text)
  return text unless text.include?(':')

  text.gsub EmojiPattern do |match|
    name = $1
    "<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{File.join(asset_root, "emoji", "#{name}.png")}' 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 27
def validate
  needs :asset_root
end