Welcome to mirror list, hosted at ThFree Co, Russian Federation.

emoji_filter.rb « filter « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8c1ca0c60a8a88d8171a961d614d9a9481fca47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module Banzai
  module Filter
    # HTML filter that replaces :emoji: and unicode with images.
    #
    # Based on HTML::Pipeline::EmojiFilter
    #
    # Context options:
    #   :asset_root
    #   :asset_host
    class EmojiFilter < HTML::Pipeline::Filter
      IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set

      def call
        search_text_nodes(doc).each do |node|
          content = node.to_html
          next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)

          next unless content.include?(':') || node.text.match(emoji_unicode_pattern)

          html = emoji_name_image_filter(content)
          html = emoji_unicode_image_filter(html)

          next if html == content

          node.replace(html)
        end
        doc
      end

      # Replace :emoji: with corresponding images.
      #
      # text - String text to replace :emoji: in.
      #
      # Returns a String with :emoji: replaced with images.
      def emoji_name_image_filter(text)
        text.gsub(emoji_pattern) do |match|
          name = $1
          emoji_image_tag(name, emoji_url(name))
        end
      end

      # Replace unicode emoji with corresponding images if they exist.
      #
      # text - String text to replace unicode emoji in.
      #
      # Returns a String with unicode emoji replaced with images.
      def emoji_unicode_image_filter(text)
        text.gsub(emoji_unicode_pattern) do |moji|
          emoji_image_tag(Gitlab::Emoji.emojis_by_moji[moji]['name'], emoji_unicode_url(moji))
        end
      end

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

      # Build a regexp that matches all valid :emoji: names.
      def self.emoji_pattern
        @emoji_pattern ||= /:(#{Gitlab::Emoji.emojis_names.map { |name| Regexp.escape(name) }.join('|')}):/
      end

      # Build a regexp that matches all valid unicode emojis names.
      def self.emoji_unicode_pattern
        @emoji_unicode_pattern ||= /(#{Gitlab::Emoji.emojis_unicodes.map { |moji| Regexp.escape(moji) }.join('|')})/
      end

      private

      def emoji_url(name)
        emoji_path = emoji_filename(name)

        if context[:asset_host]
          # Asset host is specified.
          url_to_image(emoji_path)
        elsif context[:asset_root]
          # Gitlab url is specified
          File.join(context[:asset_root], url_to_image(emoji_path))
        else
          # All other cases
          url_to_image(emoji_path)
        end
      end

      def emoji_unicode_url(moji)
        emoji_unicode_path = emoji_unicode_filename(moji)

        if context[:asset_host]
          url_to_image(emoji_unicode_path)
        elsif context[:asset_root]
          File.join(context[:asset_root], url_to_image(emoji_unicode_path))
        else
          url_to_image(emoji_unicode_path)
        end
      end

      def url_to_image(image)
        ActionController::Base.helpers.url_to_image(image)
      end

      def emoji_pattern
        self.class.emoji_pattern
      end

      def emoji_filename(name)
        "#{Gitlab::Emoji.emoji_filename(name)}.png"
      end

      def emoji_unicode_pattern
        self.class.emoji_unicode_pattern
      end

      def emoji_unicode_filename(name)
        "#{Gitlab::Emoji.emoji_unicode_filename(name)}.png"
      end
    end
  end
end