gemoji Ruby Library

repository·master·Indexed 26 days ago

https://github.com/github/gemoji

A Ruby library providing character information for native emojis, including mappings between aliases, Unicode characters, and image filenames. It allows for translating emoji names to Unicode and vice versa, managing custom emojis via Emoji.create, and retrieving metadata such as categories, Unicode versions, and skin tone variants.

Tokens
1.6K
Snippets
5
Records
15
Agent score
87%

What's inside gemoji

  1. Implement an emojify Rails helper

    master

    You can implement a helper to convert text containing emoji aliases (e.g., :cat:) into HTML <img> tags. This example uses Emoji.find_by_alias to locate the emoji and generates an image tag using the emoji's image_filename.

    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
  2. Add new emoji to the list

    master

    You can programmatically add new emoji characters to the Emoji.all list using Emoji.create.

    When creating new emojis, you must ensure that you also provide the image files referenced by their image_filename in your assets directory.

    If you create a custom emoji without Unicode aliases, emoji.custom? will return true and the image_filename will default to the emoji's name with a .png extension.

    # Adding a new emoji with aliases and tags
    emoji = Emoji.create("music") do |char|
      char.add_alias "song"
      char.add_unicode_alias "\u{266b}"
      char.add_tag "notes"
    end
    
    # Creating a custom emoji (no Unicode aliases)
    emoji = Emoji.create("music") do |char|
      char.add_tag "notes"
    end
    
    # Customizing the image filename
    emoji = Emoji.create("music") do |char|
      char.image_filename = "subdirectory/my_emoji.gif"
    end
  3. Translate emoji names to Unicode and vice versa

    master

    Use the Emoji class to map between emoji aliases and their Unicode characters.

    • Use Emoji.find_by_alias(name).raw to get the Unicode character from an alias.
    • Use Emoji.find_by_unicode(unicode_string).name to get the alias from a Unicode character.
    >> Emoji.find_by_alias("cat").raw
    => "🐱"
    
    >> Emoji.find_by_unicode("\u{1f431}").name
    => "cat"
  4. Edit existing emoji

    master

    To modify an existing emoji (such as adding new aliases or tags), find the emoji first and then use Emoji.edit_emoji within 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
  5. Handle emoji skin tone variants

    master

    For emojis that support the Fitzpatrick scale, you can manage and retrieve skin tone variants:

    • skin_tones?(): Returns true if the emoji supports skin tone modifiers.
    • attr_writer :skin_tones: Allows setting whether the emoji supports skin tones.
    • raw_skin_tone_variants: Returns an array of raw Unicode strings for each skin tone variant. For emojis depicting multiple people (like couples), it applies modifiers to both persons in specific cases (e.g., PEOPLE_HOLDING_HANDS).
  6. Find an emoji by its unicode character with `Emoji.find_by_unicode`

    master
    Search for an emoji using its unicode character string. The method is designed to handle skin tone modifiers by stripping them to find the base emoji. Returns the Emoji::Character instance if found, or nil if not found.
  7. Retrieve emoji Unicode properties and metadata

    master

    You can access various metadata properties for an Emoji::Character instance:

    • raw(): Returns the raw Unicode string for the emoji. Returns nil if the emoji is non-standard (custom).
    • custom?(): Returns true if the emoji is not a standard Unicode emoji.
    • unicode_aliases: A list of Unicode strings that uniquely refer to this emoji.
    • add_unicode_alias(str): Adds a Unicode string to the unicode_aliases list.
    • category: The emoji's category (per Apple's character palette).
    • description: The Unicode description text.
    • unicode_version: The Unicode spec version where the emoji debuted.
    • ios_version: The iOS version where the emoji debuted.
  8. Create and manage custom emojis with `Emoji.create`

    master
    Initialize a new Emoji::Character instance and add it to the global Emoji.all set. You can yield the new emoji to a block to configure its aliases, tags, and other properties. Using Emoji.create automatically updates the internal name and unicode indices.
  9. Edit an existing emoji with `Emoji.edit_emoji`

    master
    Yield an existing emoji to a block and automatically update the internal names_index and unicodes_index based on any changes made to the emoji's aliases or unicode_aliases lists.