Sha256: 2e4bc177301705ef918a8020c42e8ed6c1b45d1848cd7cd6a0ab9cd7f71b82e5

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'gemoji'
require 'magnet/markdown/filter'

class Magnet::Markdown::Filter::Emoji < HTML::Pipeline::Filter
  def self.emojis
    Emoji.all.map(&:aliases).flatten.sort
  end

  def self.regexp_pattern
    @regexp_pattern ||= /:(?<name>#{ (emojis.map { |n| Regexp.escape(n) }).join('|') }):/xo
  end

  def self.img_tag
    '<img class="emoji" alt=":%{name}:" title=":%{name}:" style="width: 1em; height: 1em;" align="absmiddle" src="%{src}">'
  end

  def call
    doc.search('.//text()').each do |node|
      content = node.text
      next unless content.include?(':')
      next if has_ancestor?(node, %w(pre code tt))
      html = emojify(content)
      next if html == content
      node.replace(html)
    end
    doc
  end

  def validate
    needs :emoji_root
  end

  def emojify(html)
    html.gsub(self.class.regexp_pattern) do |_match|
      name = Regexp.last_match['name']
      self.class.img_tag % { name: name, src: src_url(name) }
    end
  end

  def src_url(emoji_name)
    File.join(context[:emoji_root], src_path(emoji_name))
  end

  def src_path(emoji_name)
    if context[:emoji_path]
      context[:emoji_path].sub(':filename', emoji_filename(emoji_name))
    else
      File.join('emoji', emoji_filename(emoji_name))
    end
  end

  def emoji_filename(emoji_name)
    Emoji.find_by_alias(emoji_name).image_filename
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
magnet-markdown-0.0.5 lib/magnet/markdown/filter/emoji.rb