Module: Twemoji

Defined in:
lib/twemoji.rb,
lib/twemoji/map.rb,
lib/twemoji/png.rb,
lib/twemoji/svg.rb,
lib/twemoji/version.rb,
lib/twemoji/configuration.rb,
lib/twemoji/utils/unicode.rb

Overview

Twemoji is a Ruby implementation, parses your text, replace emoji text with corresponding emoji image. Default emoji images are from Twiiter CDN.

Defined Under Namespace

Modules: Utils Classes: Configuration

Constant Summary collapse

VERSION =
"3.1.8"

Class Method Summary collapse

Class Method Details

.codes ⇒ Hash<String => String>

Emoji Text to Codepoint mappings.

Examples:

Usage

codes[":heart_eyes:"] # => "1f60d"
codes[":notebook_with_decorative_cover:"] # => "1f4d4"

Returns:

  • (Hash<String => String>)


12
13
14
# File 'lib/twemoji/map.rb', line 12

def self.codes
  CODES
end

.configuration ⇒ Object



6
7
8
# File 'lib/twemoji/configuration.rb', line 6

def self.configuration
  @configuration ||= Configuration.new
end

.configuration=(configuration) ⇒ Object



10
11
12
# File 'lib/twemoji/configuration.rb', line 10

def self.configuration=(configuration)
  @configuration = configuration
end

.configure {|configuration| ... } ⇒ Object

Yields:



14
15
16
# File 'lib/twemoji/configuration.rb', line 14

def self.configure
  yield configuration
end

.emoji_pattern ⇒ RegExp

Return all emoji patterns' regular expressions.

Returns:

  • (RegExp) —

    A Regular expression consists of all emojis text.



128
129
130
# File 'lib/twemoji.rb', line 128

def self.emoji_pattern
  EMOJI_PATTERN
end

.emoji_pattern_all ⇒ RegExp

Return all emoji patterns' regular expressions in unicode and name.

Returns:

  • (RegExp) —

    A Regular expression consists of all emojis unicode codepoint and names.



143
144
145
# File 'lib/twemoji.rb', line 143

def self.emoji_pattern_all
  EMOJI_PATTERN_ALL
end

.emoji_pattern_unicode ⇒ RegExp

Return all emoji patterns' regular expressions in unicode. e.g '1f1f2-1f1fe' will be converted to \u1f1f2\u1f1fe for RegExp matching.

Returns:

  • (RegExp) —

    A Regular expression consists of all emojis unicode.



136
137
138
# File 'lib/twemoji.rb', line 136

def self.emoji_pattern_unicode
  EMOJI_PATTERN_UNICODE
end

.find_by(text: nil, code: nil, unicode: nil) ⇒ String

Find code by text, find text by code, and find text by unicode.

Examples:

Usage

Twemoji.find_by(text: ":heart_eyes:") # => "1f60d"
Twemoji.find_by(code: "1f60d")      # => ":heart_eyes:"
Twemoji.find_by(unicode: "😍")        # => ":heart_eyes:"
Twemoji.find_by(unicode: "\u{1f60d}") # => ":heart_eyes:"

Parameters:

  • options (Hash) —

    a customizable set of options

Returns:

  • (String) —

    Emoji text or code.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/twemoji.rb', line 26

def self.find_by(text: nil, code: nil, unicode: nil)
  if [ text, code, unicode ].compact!.size > 1
    fail ArgumentError, "Can only specify text, code or unicode one at a time"
  end

  case
  when text
    find_by_text text
  when unicode
    find_by_unicode unicode
  else
    find_by_code code
  end
end

.find_by_code(code) ⇒ String

Find emoji text by emoji code.

Examples:

Usage

Twemoji.find_by_code "1f60d"
=> ":heart_eyes:"

Parameters:

  • code (String) —

    Emoji code to find text.

Returns:

  • (String) —

    Emoji Text.



61
62
63
# File 'lib/twemoji.rb', line 61

def self.find_by_code(code)
  invert_codes[must_str(code)]
end

.find_by_text(text) ⇒ String

Find emoji code by emoji text.

Examples:

Usage

Twemoji.find_by_text ":heart_eyes:"
=> "1f60d"

Parameters:

  • text (String) —

    Text to find emoji code.

Returns:

  • (String) —

    Emoji Code.



49
50
51
# File 'lib/twemoji.rb', line 49

def self.find_by_text(text)
  codes[must_str(text)]
end

.find_by_unicode(raw) ⇒ String

Find emoji text by raw emoji unicode.

Examples:

Usage

Twemoji.find_by_unicode "😍"
=> ":heart_eyes:"

Parameters:

  • raw (String) —

    Emoji raw unicode to find text.

Returns:

  • (String) —

    Emoji Text.



73
74
75
# File 'lib/twemoji.rb', line 73

def self.find_by_unicode(raw)
  invert_codes[unicode_to_str(raw)]
end

.invert_codes ⇒ Hash<String => String>

Emoji Codepoint to Text mappings. This hash is frozen.

Examples:

Usage

invert_codes["1f60d"] # => ":heart_eyes:"
invert_codes["1f4d4"] # => ":notebook_with_decorative_cover:"

Returns:

  • (Hash<String => String>)


23
24
25
# File 'lib/twemoji/map.rb', line 23

def self.invert_codes
  codes.invert.freeze
end

.load_yaml(path) ⇒ Object



18
19
20
# File 'lib/twemoji/configuration.rb', line 18

def self.load_yaml(path)
  YAML.safe_load(IO.read(path))
end

.parse(text, asset_root: Twemoji.configuration.asset_root, file_ext: Twemoji.configuration.file_ext, class_name: Twemoji.configuration.class_name, img_attrs: Twemoji.configuration.img_attrs) ⇒ String

Parse string, replace emoji text with image. Parse DOM, replace emoji with image.

replaced by emoji image according to given options.

Examples:

Usage

Twemoji.parse("I like chocolate :heart_eyes:!")
=> 'I like chocolate <img draggable="false" title=":heart_eyes:" alt="😍" src="https://twemoji.maxcdn.com/2/svg/1f60d.svg" class="emoji">!'

Parameters:

  • text (String) —

    Source text to parse.

  • options (Hash) —

    a customizable set of options

Returns:

  • (String) —

    Original text with all occurrences of emoji text



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/twemoji.rb', line 109

def self.parse(text, asset_root: Twemoji.configuration.asset_root,
                     file_ext:   Twemoji.configuration.file_ext,
                     class_name: Twemoji.configuration.class_name,
                     img_attrs:  Twemoji.configuration.img_attrs)

  options[:asset_root] = asset_root
  options[:file_ext]   = file_ext
  options[:img_attrs]  = { class: class_name }.merge(img_attrs)

  if text.is_a?(Nokogiri::HTML::DocumentFragment)
    parse_document(text)
  else
    parse_html(text)
  end
end

.png ⇒ Object



4
5
6
# File 'lib/twemoji/png.rb', line 4

def self.png
  PNG
end

.render_unicode(text_or_code) ⇒ String

Render raw emoji unicode from emoji text or emoji code.

Examples:

Usage

Twemoji.render_unicode ":heart_eyes:"
=> "😍"
Twemoji.render_unicode "1f60d"
=> "😍"

Parameters:

  • text_or_code (String) —

    Emoji text or code to render as unicode.

Returns:

  • (String) —

    Emoji UTF-8 Text.



87
88
89
90
91
# File 'lib/twemoji.rb', line 87

def self.render_unicode(text_or_code)
  text_or_code = find_by_text(text_or_code) if text_or_code[0] == ?:
  unicodes = text_or_code.split(?-)
  unicodes.map(&:hex).pack(?U*unicodes.size)
end

.svg ⇒ Object



4
5
6
# File 'lib/twemoji/svg.rb', line 4

def self.svg
  SVG
end