Class: HTML::Pipeline::EmojiFilter
- Defined in:
- lib/html/pipeline/emoji_filter.rb
Overview
HTML filter that replaces :emoji: with images.
Context:
:asset_root (required) - base url to link to emoji sprite
Constant Summary collapse
- EmojiPattern =
Build a regexp that matches all valid :emoji: names.
/:(#{Emoji.names.map { |name| Regexp.escape(name) }.join('|')}):/
Instance Attribute Summary
Attributes inherited from Filter
Instance Method Summary collapse
-
#asset_root ⇒ Object
The base url to link emoji sprites.
- #call ⇒ Object
-
#emoji_image_filter(text) ⇒ Object
Replace :emoji: with corresponding images.
-
#validate ⇒ Object
Implementation of validate hook.
Methods inherited from Filter
#base_url, call, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, to_document, to_html
Constructor Details
This class inherits a constructor from HTML::Pipeline::Filter
Instance Method Details
#asset_root ⇒ Object
The base url to link emoji sprites
Raises ArgumentError if context option has not been provided. Returns the context’s asset_root.
55 56 57 |
# File 'lib/html/pipeline/emoji_filter.rb', line 55 def asset_root context[:asset_root] end |
#call ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/html/pipeline/emoji_filter.rb', line 19 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) ⇒ Object
Replace :emoji: with corresponding images.
text - String text to replace :emoji: in.
Returns a String with :emoji: replaced with images.
42 43 44 45 46 47 48 49 |
# File 'lib/html/pipeline/emoji_filter.rb', line 42 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='#{emoji_url(name)}' height='20' width='20' align='absmiddle' />" end end |
#validate ⇒ Object
Implementation of validate hook. Errors should raise exceptions or use an existing validator.
33 34 35 |
# File 'lib/html/pipeline/emoji_filter.rb', line 33 def validate needs :asset_root end |