Class: HTMLPipeline::NodeFilter::EmojiFilter
- Inherits:
-
HTMLPipeline::NodeFilter
- Object
- Filter
- HTMLPipeline::NodeFilter
- HTMLPipeline::NodeFilter::EmojiFilter
- Defined in:
- lib/html_pipeline/node_filter/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 =
["pre", "code", "tt"].freeze
Instance Attribute Summary
Attributes inherited from HTMLPipeline::NodeFilter
Attributes inherited from Filter
Instance Method Summary collapse
-
#after_initialize ⇒ Object
Build a regexp that matches all valid :emoji: names.
-
#asset_path(name) ⇒ Object
The url path to link emoji sprites.
-
#asset_root ⇒ Object
The base url to link emoji sprites.
-
#emoji_image_filter(text) ⇒ Object
Replace :emoji: with corresponding images.
- #emoji_names ⇒ Object
- #handle_text_chunk(text) ⇒ Object
- #selector ⇒ Object
-
#validate ⇒ Object
Implementation of validate hook.
Methods inherited from HTMLPipeline::NodeFilter
call, #html, #initialize, #reset!
Methods inherited from Filter
#base_url, #call, call, #has_ancestor?, #initialize, #needs
Constructor Details
This class inherits a constructor from HTMLPipeline::NodeFilter
Instance Method Details
#after_initialize ⇒ Object
Build a regexp that matches all valid :emoji: names.
19 20 21 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 19 def after_initialize @emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join("|")}):/ end |
#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.
62 63 64 65 66 67 68 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 62 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.
54 55 56 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 54 def asset_root context[:asset_root] 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.
44 45 46 47 48 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 44 def emoji_image_filter(text) text.gsub(@emoji_pattern) do emoji_image_tag(Regexp.last_match(1)) end end |
#emoji_names ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 85 def emoji_names if self.class.gemoji_loaded? Emoji.all.map(&:aliases) else Gemojione::Index.new.all.map { |i| i[1]["name"] } end.flatten.sort end |
#handle_text_chunk(text) ⇒ Object
27 28 29 30 31 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 27 def handle_text_chunk(text) return unless text.to_s.include?(":") text.replace(emoji_image_filter(text.to_s), as: :html) end |
#selector ⇒ Object
23 24 25 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 23 def selector Selma::Selector.new(match_text_within: "*", ignore_text_within: ) end |
#validate ⇒ Object
Implementation of validate hook. Errors should raise exceptions or use an existing validator.
35 36 37 |
# File 'lib/html_pipeline/node_filter/emoji_filter.rb', line 35 def validate needs(:asset_root) end |