Class: HTML::Pipeline::GitLab::GitLabEmojiFilter
- Inherits:
-
Filter
- Object
- Filter
- HTML::Pipeline::GitLab::GitLabEmojiFilter
- Defined in:
- lib/html/pipeline/gitlab/gitlab_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.
Class Method Summary collapse
- .emoji_names ⇒ Object
-
.emoji_pattern ⇒ Object
Build a regexp that matches all valid :emoji: names.
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.
Class Method Details
.emoji_names ⇒ Object
72 73 74 |
# File 'lib/html/pipeline/gitlab/gitlab_emoji_filter.rb', line 72 def self.emoji_names Emoji.names end |
.emoji_pattern ⇒ Object
Build a regexp that matches all valid :emoji: names.
64 65 66 |
# File 'lib/html/pipeline/gitlab/gitlab_emoji_filter.rb', line 64 def self.emoji_pattern @emoji_pattern ||= /:(#{emoji_names.map { |name| Regexp.escape(name) }.join('|')}):/ end |
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.
53 54 55 |
# File 'lib/html/pipeline/gitlab/gitlab_emoji_filter.rb', line 53 def asset_root context[:asset_root] end |
#call ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/html/pipeline/gitlab/gitlab_emoji_filter.rb', line 17 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.
40 41 42 43 44 45 46 47 |
# File 'lib/html/pipeline/gitlab/gitlab_emoji_filter.rb', line 40 def emoji_image_filter(text) return text unless text.include?(':') text.gsub(emoji_pattern) 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.
31 32 33 |
# File 'lib/html/pipeline/gitlab/gitlab_emoji_filter.rb', line 31 def validate needs :asset_root end |