Sha256: 09b7c3f7fc7aea82c5fc69c1c5e3402e8e1e6a2642a0de4ea14b0dd0d4bc6849
Contents?: true
Size: 1.69 KB
Versions: 1
Compression:
Stored size: 1.69 KB
Contents
begin require "gemoji" rescue LoadError => _ abort "Missing dependency 'gemoji' for EmojiFilter. See README.md for details." end module HTML class Pipeline # HTML filter that replaces :emoji: with images. # # Context: # :asset_root (required) - base url to link to emoji sprite class EmojiFilter < Filter # Build a regexp that matches all valid :emoji: names. EmojiPattern = /:(#{Emoji.names.map { |name| Regexp.escape(name) }.join('|')}):/ 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 # Implementation of validate hook. # Errors should raise exceptions or use an existing validator. def validate needs :asset_root end # Replace :emoji: with corresponding images. # # text - String text to replace :emoji: in. # # Returns a String with :emoji: replaced with images. 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 # The base url to link emoji sprites # # Raises ArgumentError if context option has not been provided. # Returns the context's asset_root. def asset_root context[:asset_root] end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
html-pipeline-1.0.0 | lib/html/pipeline/emoji_filter.rb |