BigW Consortium Gitlab

emoji.rb 1.51 KB
Newer Older
1 2 3
module Gitlab
  module Emoji
    extend self
4

5 6 7 8 9 10 11
    def emojis
      Gemojione.index.instance_variable_get(:@emoji_by_name)
    end

    def emojis_by_moji
      Gemojione.index.instance_variable_get(:@emoji_by_moji)
    end
12

13
    def emojis_unicodes
14
      emojis_by_moji.keys
15
    end
16

17
    def emojis_names
18
      emojis.keys
19 20
    end

21
    def emojis_aliases
Eric Eastwood committed
22
      @emoji_aliases ||= JSON.parse(File.read(Rails.root.join('fixtures', 'emojis', 'aliases.json')))
23 24
    end

25 26 27
    def emoji_filename(name)
      emojis[name]["unicode"]
    end
28

29 30 31
    def emoji_unicode_filename(moji)
      emojis_by_moji[moji]["unicode"]
    end
32 33

    def emoji_unicode_version(name)
34
      @emoji_unicode_versions_by_name ||= JSON.parse(File.read(Rails.root.join('fixtures', 'emojis', 'emoji-unicode-version-map.json')))
Eric Eastwood committed
35 36 37 38 39
      @emoji_unicode_versions_by_name[name]
    end

    def normalize_emoji_name(name)
      emojis_aliases[name] || name
40 41 42 43 44 45 46
    end

    def emoji_image_tag(name, src)
      "<img class='emoji' title=':#{name}:' alt=':#{name}:' src='#{src}' height='20' width='20' align='absmiddle' />"
    end

    # CSS sprite fallback takes precedence over image fallback
47
    def gl_emoji_tag(name)
48 49
      emoji_name = emojis_aliases[name] || name
      emoji_info = emojis[emoji_name]
50
      return unless emoji_info
Eric Eastwood committed
51 52 53 54 55

      data = {
        name: emoji_name,
        unicode_version: emoji_unicode_version(emoji_name)
      }
56

57
      ActionController::Base.helpers.content_tag('gl-emoji', emoji_info['moji'], title: emoji_info['description'], data: data)
58
    end
59 60
  end
end