2017-02-06 02:07:18 +08:00
|
|
|
EMOJI_LIST_URL ||= "http://unicode.org/emoji/charts/full-emoji-list.html"
|
|
|
|
EMOJI_KEYWORDS_URL ||= "https://raw.githubusercontent.com/muan/emojilib/master/emojis.json"
|
2016-09-19 19:43:15 +08:00
|
|
|
|
2016-09-20 01:39:46 +08:00
|
|
|
# until MS release the emoji flags, we'll use custom made flags
|
2017-02-06 02:07:18 +08:00
|
|
|
WINDOWS_FLAGS ||= Set.new ["1f1e8_1f1f3", "1f1e9_1f1ea", "1f1ea_1f1f8", "1f1eb_1f1f7", "1f1ec_1f1e7", "1f1ee_1f1f9", "1f1ef_1f1f5", "1f1f0_1f1f7", "1f1f7_1f1fa", "1f1fa_1f1f8"]
|
2016-09-20 01:39:46 +08:00
|
|
|
|
2014-12-12 00:08:47 +08:00
|
|
|
desc "update emoji images"
|
|
|
|
task "emoji:update" => :environment do
|
2016-09-19 19:43:15 +08:00
|
|
|
emojis = {}
|
2014-12-12 00:08:47 +08:00
|
|
|
|
2016-09-19 19:43:15 +08:00
|
|
|
puts "Loading local emoji database..."
|
|
|
|
db = JSON.parse(File.read("lib/emoji/db.json"))
|
|
|
|
db["emojis"].each do |e|
|
|
|
|
emojis[e["code"].tr("-", "_")] = { name: e["name"] }
|
|
|
|
end
|
|
|
|
aliases = db["aliases"].to_h
|
|
|
|
|
|
|
|
puts "Enhancing emoji database with emojilib keywords..."
|
|
|
|
keywords = JSON.parse(open(EMOJI_KEYWORDS_URL).read)
|
|
|
|
keywords.keys.each do |k|
|
|
|
|
next unless char = keywords[k]["char"].presence
|
|
|
|
|
|
|
|
code = char.codepoints
|
2016-09-20 01:39:46 +08:00
|
|
|
.map { |c| c.to_s(16).rjust(4, "0") }
|
|
|
|
.join("_")
|
|
|
|
.downcase
|
|
|
|
.gsub(/_fe0f$/, "")
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
emojis[code] ||= { name: k }
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Retrieving remote emoji list..."
|
|
|
|
list = open(EMOJI_LIST_URL).read
|
|
|
|
|
|
|
|
puts "Parsing remote emoji list..."
|
|
|
|
doc = Nokogiri::HTML(list)
|
|
|
|
doc.css("tr").each do |row|
|
|
|
|
cells = row.css("td")
|
|
|
|
next if cells.size == 0
|
|
|
|
|
|
|
|
code = cells[1].at_css("a")["name"]
|
|
|
|
|
2017-01-24 16:11:20 +08:00
|
|
|
unless emojis[code]
|
|
|
|
code = code.gsub(/_fe0f/, "")
|
|
|
|
next unless emojis[code]
|
|
|
|
end
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
apple = cell_to_image(cells[4])
|
|
|
|
google = cell_to_image(cells[5])
|
|
|
|
twitter = cell_to_image(cells[6])
|
|
|
|
one = cell_to_image(cells[7])
|
2016-09-20 01:39:46 +08:00
|
|
|
|
|
|
|
if WINDOWS_FLAGS.include?(code)
|
|
|
|
windows = custom_windows_flag(code)
|
|
|
|
else
|
2017-01-24 16:11:20 +08:00
|
|
|
windows = cell_to_image(cells[11])
|
2016-09-20 01:39:46 +08:00
|
|
|
end
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
if apple.blank? || google.blank? || twitter.blank? || one.blank? || windows.blank?
|
|
|
|
emojis.delete(code)
|
|
|
|
next
|
2014-12-12 00:08:47 +08:00
|
|
|
end
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
emojis[code][:apple] = apple
|
|
|
|
emojis[code][:google] = google
|
|
|
|
emojis[code][:twitter] = twitter
|
|
|
|
emojis[code][:one] = one
|
|
|
|
emojis[code][:windows] = windows
|
2014-12-12 00:08:47 +08:00
|
|
|
end
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
puts "Writing emojis..."
|
|
|
|
write_emojis(emojis, aliases, :apple, "apple")
|
|
|
|
write_emojis(emojis, aliases, :google, "google")
|
|
|
|
write_emojis(emojis, aliases, :twitter, "twitter")
|
|
|
|
write_emojis(emojis, aliases, :one, "emoji_one")
|
|
|
|
write_emojis(emojis, aliases, :windows, "win10")
|
|
|
|
|
|
|
|
puts "Updating db.json..."
|
|
|
|
db = {
|
|
|
|
"emojis" => emojis.keys.map { |k| { "code" => k.tr("_", "-"), "name" => emojis[k][:name] } },
|
|
|
|
"aliases" => aliases,
|
|
|
|
}
|
|
|
|
|
|
|
|
File.write("lib/emoji/db.json", JSON.pretty_generate(db))
|
|
|
|
|
|
|
|
puts "Done!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def cell_to_image(cell)
|
|
|
|
return unless img = cell.at_css("img")
|
|
|
|
Base64.decode64(img["src"][/base64,(.+)$/, 1])
|
2014-12-12 00:08:47 +08:00
|
|
|
end
|
|
|
|
|
2016-09-20 01:39:46 +08:00
|
|
|
def custom_windows_flag(code)
|
|
|
|
name = code.upcase.tr("_", "-")
|
|
|
|
open("https://github.com/discourse/discourse-emoji-extractor/raw/master/win10/72x72/windows_#{name}.png").read
|
|
|
|
end
|
|
|
|
|
2016-09-19 19:43:15 +08:00
|
|
|
def write_emojis(emojis, aliases, style, folder)
|
2017-01-24 16:11:20 +08:00
|
|
|
path = "public/images/emoji/#{folder}"
|
2016-09-19 19:43:15 +08:00
|
|
|
|
2017-01-24 16:11:20 +08:00
|
|
|
# Uncomment to recreate all emojis
|
|
|
|
# FileUtils.rm_f Dir.glob("#{path}/*")
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
puts folder
|
|
|
|
|
|
|
|
emojis.values.each do |emoji|
|
2017-01-24 16:11:20 +08:00
|
|
|
next if emoji[style].nil?
|
|
|
|
|
2016-09-19 19:43:15 +08:00
|
|
|
write_emoji("#{path}/#{emoji[:name]}.png", emoji[style])
|
|
|
|
if aliases[emoji[:name]]
|
|
|
|
aliases[emoji[:name]].each do |new_name|
|
|
|
|
write_emoji("#{path}/#{new_name}.png", emoji[style])
|
2014-12-15 19:27:45 +08:00
|
|
|
end
|
2014-12-12 00:08:47 +08:00
|
|
|
end
|
|
|
|
end
|
2016-09-19 19:43:15 +08:00
|
|
|
|
|
|
|
puts
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_emoji(path, emoji)
|
2017-01-24 16:11:20 +08:00
|
|
|
open(path, "wb") { |f| f << emoji }
|
|
|
|
`pngout #{path}`
|
|
|
|
putc "."
|
|
|
|
ensure
|
|
|
|
raise "Failed to write emoji: #{path}" if File.exists?(path) && !File.size?(path)
|
2014-12-12 00:08:47 +08:00
|
|
|
end
|