forked from carlosgaldino/alfred-emoji-workflow
/
generate.rb
39 lines (28 loc) · 1.33 KB
/
generate.rb
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
require 'nokogiri'
require 'open-uri'
require 'base64'
require 'json'
doc = Nokogiri::HTML(open('http://unicode.org/emoji/charts/full-emoji-list.html'))
# doc = File.open('full-emoji-list.html') { |f| Nokogiri::HTML(f) }
rows = doc.xpath('//table/tr')
related = {}
symbols = {}
custom_related = JSON.load(File.read('custom_related.json'))
rows.each do |row|
# Skip header row of table
next if 'Count' == row.at_xpath('th[1]/text()').to_s.strip
# Replace spaces with _ in emoji name to make image filename
filename = row.at_xpath('td[13]/text()').to_s.strip.gsub(/\s/, '_').downcase
# Decode base64 image data for Apple icon and save to file
icon = Base64.decode64(row.css('td.andr')[1].css('img').attr('src').to_s[22..-1])
File.open("images/emoji/#{filename}.png", 'wb') { |f| f.write(icon) }
# Use annotations for related words
annotations = row.css('td[16]/a').children.collect { |a| a.to_s }
# Combine annotations with custom related words
related[filename] = annotations.concat(custom_related[filename] || []).uniq
# Read the unicode symbol
symbols[filename] = [row.at_xpath('td[2]/a/text()').to_s[2..-1].to_s.hex].pack('U')
end
# Write data files used by emoji.rb
File.open('symbols.json', 'wb') { |f| f.write(JSON.pretty_generate(symbols)) }
File.open('related.json', 'wb') { |f| f.write(JSON.pretty_generate(related)) }