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
-
.codes ⇒ Hash<String => String>
Emoji Text to Codepoint mappings.
- .configuration ⇒ Object
- .configuration=(configuration) ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.emoji_pattern ⇒ RegExp
Return all emoji patterns' regular expressions.
-
.emoji_pattern_all ⇒ RegExp
Return all emoji patterns' regular expressions in unicode and name.
-
.emoji_pattern_unicode ⇒ RegExp
Return all emoji patterns' regular expressions in unicode.
-
.find_by(text: nil, code: nil, unicode: nil) ⇒ String
Find code by text, find text by code, and find text by unicode.
-
.find_by_code(code) ⇒ String
Find emoji text by emoji code.
-
.find_by_text(text) ⇒ String
Find emoji code by emoji text.
-
.find_by_unicode(raw) ⇒ String
Find emoji text by raw emoji unicode.
-
.invert_codes ⇒ Hash<String => String>
Emoji Codepoint to Text mappings.
- .load_yaml(path) ⇒ Object
-
.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.
- .png ⇒ Object
-
.render_unicode(text_or_code) ⇒ String
Render raw emoji unicode from emoji text or emoji code.
- .svg ⇒ Object
Class Method Details
.codes ⇒ Hash<String => String>
Emoji Text to Codepoint mappings.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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) [:asset_root] = asset_root [:file_ext] = file_ext [: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.
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 |