Class: Banzai::Filter::EmojiFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/banzai/filter/emoji_filter.rb

Overview

HTML filter that replaces :emoji: with images.

Based on HTML::Pipeline::EmojiFilter

Context options:

:asset_root
:asset_host

Constant Summary collapse

IGNORED_ANCESTOR_TAGS =
%w(pre code tt).to_set

Instance Method Summary collapse

Instance Method Details

#callObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/banzai/filter/emoji_filter.rb', line 13

def call
  search_text_nodes(doc).each do |node|
    content = node.to_html
    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_image_filter(text) ⇒ Object

Replace :emoji: with corresponding images.

text - String text to replace :emoji: in.

Returns a String with :emoji: replaced with images.


34
35
36
37
38
39
# File 'lib/banzai/filter/emoji_filter.rb', line 34

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