class SimpleFormat::Emoji
Public Class Methods
new(mapping=nil)
click to toggle source
# File lib/simple_format/emoji.rb, line 7 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
Public Instance Methods
asset_delimiter()
click to toggle source
分隔符
# File lib/simple_format/emoji.rb, line 53 def asset_delimiter @asset_delimiter || '_' end
asset_delimiter=(delimiter)
click to toggle source
# File lib/simple_format/emoji.rb, line 56 def asset_delimiter=(delimiter) @asset_delimiter = delimiter end
asset_host()
click to toggle source
主机地址
# File lib/simple_format/emoji.rb, line 29 def asset_host @asset_host || 'emoji.qiniudn.com' end
asset_host=(host)
click to toggle source
设置主机地址
# File lib/simple_format/emoji.rb, line 33 def asset_host=(host) @asset_host = host end
asset_path()
click to toggle source
资源路径
# File lib/simple_format/emoji.rb, line 37 def asset_path @asset_path || '/' end
asset_path=(path)
click to toggle source
设置路径
# File lib/simple_format/emoji.rb, line 41 def asset_path=(path) @asset_path = path end
asset_size()
click to toggle source
图标尺寸
# File lib/simple_format/emoji.rb, line 45 def asset_size @asset_size || '' end
asset_size=(size)
click to toggle source
设置图标尺寸
# File lib/simple_format/emoji.rb, line 49 def asset_size=(size) @asset_size = size end
find_by_name(name)
click to toggle source
通过(名称)表情
# File lib/simple_format/emoji.rb, line 112 def find_by_name(name) @emoji_by_name[name] end
find_by_unicode(moji)
click to toggle source
通过(字符)找表情
# File lib/simple_format/emoji.rb, line 108 def find_by_unicode(moji) @emoji_by_unicode[moji] end
image_url_for_name(name)
click to toggle source
通过(名称)合成图片地址
# File lib/simple_format/emoji.rb, line 93 def image_url_for_name(name) image_url = "#{asset_host}#{ File.join(asset_path, name) }.png" if image_url.present? if asset_size.present? && asset_size.in?(sizes) image_url = [image_url, asset_size].join(asset_delimiter) end end return image_url end
image_url_for_unicode(unicode)
click to toggle source
通过(字符)合成图片地址
# File lib/simple_format/emoji.rb, line 103 def image_url_for_unicode(unicode) emoji = find_by_unicode(unicode) image_url_for_name(emoji[:name]) unless emoji.nil? end
names()
click to toggle source
名称列表
# File lib/simple_format/emoji.rb, line 116 def names @emoji_by_name.keys end
names_regex()
click to toggle source
名称匹配表达式
# File lib/simple_format/emoji.rb, line 124 def names_regex @emoji_name_regex end
replace_emoji_with_images(string)
click to toggle source
通过(名称、字符)替换表情
# File lib/simple_format/emoji.rb, line 60 def replace_emoji_with_images(string) return string unless string html ||= string.dup html = replace_name_with_images(html) html = replace_unicode_with_images(html.to_str) return html end
replace_name_with_images(string)
click to toggle source
通过(名称)替换表情
# File lib/simple_format/emoji.rb, line 68 def replace_name_with_images(string) 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) }" />} else match end end end
replace_unicode_with_images(string)
click to toggle source
通过(字符)替换表情
# File lib/simple_format/emoji.rb, line 82 def replace_unicode_with_images(string) 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) }" />} end end
unicodes()
click to toggle source
字符列表
# File lib/simple_format/emoji.rb, line 120 def unicodes @emoji_by_unicode.keys end
unicodes_regex()
click to toggle source
字符匹配表达式
# File lib/simple_format/emoji.rb, line 128 def unicodes_regex @emoji_unicode_regex end
Private Instance Methods
sizes()
click to toggle source
尺寸
# File lib/simple_format/emoji.rb, line 135 def sizes %W(16x16 24x24 32x32 48x48 56x56) end