Class: SimpleFormat::Emoji

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_format/emoji.rb

Instance Method Summary collapse

Constructor Details

#initialize(mapping = nil) ⇒ Emoji

Returns a new instance of Emoji.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simple_format/emoji.rb', line 6

def initialize(mapping=nil)
  mapping ||= begin
    emoji_json = File.read(File.absolute_path(File.dirname(__FILE__) + '/../../config/index.json'))
    MultiJson.load(emoji_json, symbolize_keys: true)
  end
  
  @emoji_by_name = {}
  @emoji_by_unicode = {}

  mapping.each do |emoji_hash|
    name = emoji_hash[:name]
    @emoji_by_name[name] = emoji_hash if name

    unicode = emoji_hash[:unicode]
    @emoji_by_unicode[unicode] = emoji_hash if unicode
  end

  @emoji_name_regex = /:([a-z0-9\+\-_]+):/
  @emoji_unicode_regex = /#{@emoji_by_unicode.keys.join('|')}/
end

Instance Method Details

#asset_hostObject

主机地址



27
28
29
# File 'lib/simple_format/emoji.rb', line 27

def asset_host
  @asset_host || '//l.ruby-china.org'
end

#asset_host=(host) ⇒ Object

设置主机地址



31
32
33
# File 'lib/simple_format/emoji.rb', line 31

def asset_host=(host)
  @asset_host = host
end

#asset_pathObject

资源路径



35
36
37
# File 'lib/simple_format/emoji.rb', line 35

def asset_path
  @asset_path || '/assets/emojis/'
end

#asset_path=(path) ⇒ Object

设置路径



39
40
41
# File 'lib/simple_format/emoji.rb', line 39

def asset_path=(path)
  @asset_path = path
end

#find_by_name(name) ⇒ Object

通过(名称)表情



92
93
94
# File 'lib/simple_format/emoji.rb', line 92

def find_by_name(name)
  @emoji_by_name[name]
end

#find_by_unicode(moji) ⇒ Object

通过(字符)找表情



88
89
90
# File 'lib/simple_format/emoji.rb', line 88

def find_by_unicode(moji)
  @emoji_by_unicode[moji]
end

#image_url_for_name(name) ⇒ Object

通过(名称)合成图片地址



79
80
81
# File 'lib/simple_format/emoji.rb', line 79

def image_url_for_name(name)
  "#{asset_host}#{ File.join(asset_path, name) }.png"
end

#image_url_for_unicode(unicode) ⇒ Object

通过(字符)合成图片地址



83
84
85
86
# File 'lib/simple_format/emoji.rb', line 83

def image_url_for_unicode(unicode)
  emoji = find_by_unicode(unicode)
  image_url_for_name(emoji[:name]) unless emoji.nil?
end

#namesObject

名称列表



96
97
98
# File 'lib/simple_format/emoji.rb', line 96

def names
  @emoji_by_name.keys
end

#names_regexObject

名称匹配表达式



104
105
106
# File 'lib/simple_format/emoji.rb', line 104

def names_regex
  @emoji_name_regex
end

#replace_emoji_with_images(string, size = '64') ⇒ Object

通过(名称、字符)替换表情



43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_format/emoji.rb', line 43

def replace_emoji_with_images(string, size='64')
  return string unless string

  html ||= string.dup
  html = replace_name_with_images(html, size)
  html = replace_unicode_with_images(html.to_str, size)
  puts html
  puts '_'*88 + 'replace_unicode_with_images'
  return html
end

#replace_name_with_images(string, size = '64') ⇒ Object

通过(名称)替换表情



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/simple_format/emoji.rb', line 54

def replace_name_with_images(string, size='64')
  unless string && string.match(names_regex)
    return string
  end

  string.to_str.gsub(names_regex) do |match|
    if names.include?($1)
      %Q{<img class="emoji" src="#{ image_url_for_name($1) }" width="#{size}" height="#{size}" />}
    else
      match
    end
  end
end

#replace_unicode_with_images(string, size = '64') ⇒ Object

通过(字符)替换表情



68
69
70
71
72
73
74
75
76
77
# File 'lib/simple_format/emoji.rb', line 68

def replace_unicode_with_images(string, size='64')
  unless string && string.match(unicodes_regex)
    return string
  end

  html ||= string.dup
  html.gsub!(unicodes_regex) do |unicode|
    %Q{<img class="emoji" src="#{ image_url_for_unicode(unicode) }" width="#{size}" height="#{size}" />}
  end
end

#unicodesObject

字符列表



100
101
102
# File 'lib/simple_format/emoji.rb', line 100

def unicodes
  @emoji_by_unicode.keys
end

#unicodes_regexObject

字符匹配表达式



108
109
110
# File 'lib/simple_format/emoji.rb', line 108

def unicodes_regex
  @emoji_unicode_regex
end