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
:asset_path (optional) - url path to link to emoji sprite. :file_name can be used as a placeholder for the sprite file name. If no asset_path is set "emoji/:file_name" is used.
:ignored_ancestor_tags (optional) - Tags to stop the emojification. Node has matched ancestor HTML will not be emojified. Default to pre, code, and tt . Extra please pass in the form of array, e.g., %w(blockquote summary).
:img_attrs (optional) - Attributes for generated img tag. E.g. Pass { "draggble" => true, "height" => nil } to set draggable attribute to "true" and clear height attribute of generated img tag.
Constant Summary collapse
- DEFAULT_IGNORED_ANCESTOR_TAGS =
%w(pre code tt).freeze
Instance Attribute Summary
Attributes inherited from Filter
Instance Method Summary collapse
-
#asset_path(name) ⇒ Object
The url path to link emoji sprites.
-
#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_path(name) ⇒ Object
The url path to link emoji sprites
:file_name can be used in the asset_path as a placeholder for the sprite file name. If no asset_path is set in the context “emoji/:file_name” is used. Returns the context’s asset_path or the default path if no context asset_path is given.
63 64 65 66 67 68 69 |
# File 'lib/html/pipeline/emoji_filter.rb', line 63 def asset_path(name) if context[:asset_path] context[:asset_path].gsub(":file_name", emoji_filename(name)) else File.join("emoji", emoji_filename(name)) end end |
#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
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/html/pipeline/emoji_filter.rb', line 22 def call doc.search('.//text()').each do |node| content = node.to_html next unless content.include?(':') next if has_ancestor?(node, ) 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.
45 46 47 48 49 |
# File 'lib/html/pipeline/emoji_filter.rb', line 45 def emoji_image_filter(text) text.gsub(emoji_pattern) do |match| emoji_image_tag($1) end end |
#validate ⇒ Object
Implementation of validate hook. Errors should raise exceptions or use an existing validator.
36 37 38 |
# File 'lib/html/pipeline/emoji_filter.rb', line 36 def validate needs :asset_root end |