Emoji images and names.
Перейти к файлу
Mislav Marohnić 584939127e Add `script/regenerate` to rebuild `db/emoji.json` database
Uses AppleScript to open Safari and filter the emojis by whether they
render as actual emoji on OS X or not.
2014-08-05 23:25:35 -07:00
db Add `script/regenerate` to rebuild `db/emoji.json` database 2014-08-05 23:25:35 -07:00
images/emoji Rename PNGs on disk to not contain VARIATION_SELECTOR_16 2014-08-05 22:16:27 -07:00
lib Rename PNGs on disk to not contain VARIATION_SELECTOR_16 2014-08-05 22:16:27 -07:00
script Add `script/regenerate` to rebuild `db/emoji.json` database 2014-08-05 23:25:35 -07:00
test Ruby 1.9 compat in tests 2014-08-05 22:27:01 -07:00
.gitignore Add task to download Unicode 6.3 names list 2014-06-27 11:21:45 +08:00
.travis.yml Configure Travis CI to use `script/test` 2014-08-05 23:07:39 -07:00
Gemfile Actually give up on 1.8.7 compatbility 2014-06-27 19:02:05 +08:00
Gemfile.lock Reflect current 2.0.0 release in Gemfile.lock 2014-07-17 17:50:05 -07:00
LICENSE Update LICENSE for the source code 2013-06-24 14:25:43 -06:00
MAINTAINING.md Fix syntax 2012-11-16 13:17:54 -06:00
README.md Change find_by_* to return nil rather than raise an exception. 2014-07-18 09:16:21 -07:00
Rakefile Add dump script to inspect Unicode descriptions & aliases 2014-06-27 11:21:45 +08:00
gemoji.gemspec gemoji 2.0.0 2014-07-04 17:41:23 +08:00

README.md

gemoji

Emoji images and names. See the LICENSE for copyright information.

Installation

Add gemoji to your Gemfile.

gem 'gemoji'

Sync images

Images can be copied to your public directory with rake emoji in your app. This is the recommended approach since the images will be available at a consistent location. This works best with cached formatted user content generated by tools like html-pipeline.

# Rakefile
load 'tasks/emoji.rake'
$ rake emoji

Assets Precompiling

If you must, you can manually add all the images to your asset load path.

# config/application.rb
config.assets.paths << Emoji.images_path

Then have them compiled to public on deploy.

# config/application.rb
config.assets.precompile << "emoji/**/*.png"

WARNING Since there are a ton of images, just adding the path may slow down other lookups if you aren't using it. Compiling all the emojis on deploy will add overhead to your deploy if even the images haven't changed. Theres just so many more superfluous files to iterate over. Also, the urls will be fingerprinted which may not be ideal for referencing from cached content.

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="#{asset_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.

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