Class: HTML::Pipeline::NegarMojiHtmlPipeline::NegarehEmojiFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/html/pipeline/negarmoji-pipeline/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 ":file_name" is used.

:extension (optional) - extension to be use for emoji files, default
extension is svg.

:ignored_ancestor_tags (optional) - Tags to stop the emojification. Node
has matched ancestor HTML tags will not be emojified. Default to pre, code,
and tt tags. Extra tags 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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emoji_namesObject



102
103
104
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 102

def self.emoji_names
  Emoji.all.map(&:aliases).flatten.sort
end

.emoji_patternObject

Build a regexp that matches all valid :emoji: names.



96
97
98
99
100
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 96

def self.emoji_pattern
  @emoji_pattern ||= %r{
  :(#{emoji_names.map { |name| Regexp.escape(name) }.join("|")}):
}x
end

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 “:file_name” is used. Returns the context’s asset_path or the default path if no context asset_path is given.



80
81
82
83
84
85
86
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 80

def asset_path(name)
  if context[:asset_path]
    context[:asset_path].gsub(":file_name", emoji_filename(name))
  else
    File.join(emoji_filename(name))
  end
end

#asset_rootObject

The base url to link emoji sprites.

Raises ArgumentError if context option has not been provided. Returns the context’s asset_root.



69
70
71
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 69

def asset_root
  context[:asset_root]
end

#callObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 32

def call
  doc.search(".//text()").each do |node|
    content = node.text

    next unless content.include?(":")
    next if has_ancestor?(node, ignored_ancestor_tags)

    html = emoji_image_filter(content)

    next if html == content

    node.replace(html)
  end
  doc
end

#emoji_extensionObject

Emoji file extension.

Extension to be use for emoji files, default extension is svg.



91
92
93
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 91

def emoji_extension
  context[:extension] || "svg"
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.



59
60
61
62
63
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 59

def emoji_image_filter(text)
  text.gsub(emoji_pattern) do |_match|
    emoji_image_tag(Regexp.last_match(1))
  end
end

#validateObject

Implementation of validate hook. Errors should raise exceptions or use an existing validator.



50
51
52
# File 'lib/html/pipeline/negarmoji-pipeline/filter.rb', line 50

def validate
  needs :asset_root
end