Emoji images and names.
Перейти к файлу
Mislav Marohnić a8d55fd539
Disallow dashes in new emoji aliases
2022-11-15 18:46:23 +01:00
.github/workflows Update Ruby CI testing setup 2022-11-15 18:03:48 +01:00
db Add tags to Unicode 14 2022-11-15 18:25:34 +01:00
lib Revert "Merge pull request #164 from github/mislav/text-glyphs" 2019-11-29 13:24:35 +01:00
script 🔥 obsolete scripts 2019-05-05 01:58:35 +02:00
test Disallow dashes in new emoji aliases 2022-11-15 18:46:23 +01:00
vendor Import Emoji 14.0 characters 2022-07-26 12:43:16 +03:00
.gitignore 🔥 extractor logic 2019-05-05 02:28:19 +02:00
CONTRIBUTING.md Remove obsolete information 2019-05-05 02:28:19 +02:00
Gemfile Convert none ascii characters safely to their ASCII representation 2020-07-30 22:06:23 +02:00
Gemfile.lock gemoji 4.0.0.rc3 2021-07-26 12:08:11 +01:00
LICENSE Remove obsolete information 2019-05-05 02:28:19 +02:00
README.md 🔥 extractor logic 2019-05-05 02:28:19 +02:00
Rakefile Import Emoji 14.0 characters 2022-07-26 12:43:16 +03:00
gemoji.gemspec gemoji 4.0.0.rc3 2021-07-26 12:08:11 +01:00

README.md

gemoji

This library contains character information about native emojis.

Installation

Add gemoji to your Gemfile.

gem 'gemoji'

Example Rails Helper

This would allow emojifying content such as: it's raining :cat:s and :dog:s!

See the Emoji cheat sheet for more examples.

module EmojiHelper
  def emojify(content)
    h(content).to_str.gsub(/:([\w+-]+):/) do |match|
      if emoji = Emoji.find_by_alias($1)
        %(<img alt="#$1" src="#{image_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
      else
        match
      end
    end.html_safe if content.present?
  end
end

Unicode mapping

Translate emoji names to unicode and vice versa.

>> Emoji.find_by_alias("cat").raw
=> "🐱"  # Don't see a cat? That's U+1F431.

>> Emoji.find_by_unicode("\u{1f431}").name
=> "cat"

Adding new emoji

You can add new emoji characters to the Emoji.all list:

emoji = Emoji.create("music") do |char|
  char.add_alias "song"
  char.add_unicode_alias "\u{266b}"
  char.add_tag "notes"
end

emoji.name #=> "music"
emoji.raw  #=> "♫"
emoji.image_filename #=> "unicode/266b.png"

# Creating custom emoji (no Unicode aliases):
emoji = Emoji.create("music") do |char|
  char.add_tag "notes"
end

emoji.custom? #=> true
emoji.image_filename #=> "music.png"

As you create new emoji, you must ensure that you also create and put the images they reference by their image_filename to your assets directory.

You can customize image_filename with:

emoji = Emoji.create("music") do |char|
  char.image_filename = "subdirectory/my_emoji.gif"
end

For existing emojis, you can edit the list of aliases or add new tags in an edit block:

emoji = Emoji.find_by_alias "musical_note"

Emoji.edit_emoji(emoji) do |char|
  char.add_alias "music"
  char.add_unicode_alias "\u{266b}"
  char.add_tag "notes"
end

Emoji.find_by_alias "music"       #=> emoji
Emoji.find_by_unicode "\u{266b}"  #=> emoji