summaryrefslogtreecommitdiffstats
path: root/bin/convert_github_emojis.rb
blob: 15ed0140cf6ff71f223ffb41af13a060a8af0d35 (plain) (blame)
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
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'
require 'uri'

# emojis.json should be downloaded from https://api.github.com/emojis first.
unless File.exist?('tmp/emojis.json')
  Dir.mkdir 'tmp' unless Dir.exist?('tmp')
  system('curl', '-o', 'tmp/emojis.json', 'https://api.github.com/emojis')
end
source = JSON.parse(IO.read('tmp/emojis.json'))

data = {}
source.each do |k, v|
  filename = File.basename(URI.parse(v).path, '.png')
  if filename =~ /[g-z]+/i
    warn "#{filename} is not a valid UTF8 codepoint"
  else
    data[k] = filename.split('-').map { |c| [c.hex].pack('U') }.join('')
  end
end

output = []
data.each do |k, v|
  output << "':#{k}:' => '#{v}'"
end

IO.write('lib/emojis.rb', 'EMOJIS_LIST = {' + output.join(',') + '}.freeze')