emojis.rake 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. def codepoints_to_filename(codepoints)
  3. codepoints.downcase.gsub(/\A[0]+/, '').tr(' ', '-')
  4. end
  5. def codepoints_to_unicode(codepoints)
  6. if codepoints.include?(' ')
  7. codepoints.split(' ').map(&:hex).pack('U*')
  8. else
  9. [codepoints.hex].pack('U')
  10. end
  11. end
  12. namespace :emojis do
  13. desc 'Generate a unicode to filename mapping'
  14. task :generate do
  15. source = 'http://www.unicode.org/Public/emoji/12.0/emoji-test.txt'
  16. codes = []
  17. dest = Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json')
  18. puts "Downloading emojos from source... (#{source})"
  19. HTTP.get(source).to_s.split("\n").each do |line|
  20. next if line.start_with? '#'
  21. parts = line.split(';').map(&:strip)
  22. next if parts.size < 2
  23. codes << [parts[0], parts[1].start_with?('fully-qualified')]
  24. end
  25. grouped_codes = codes.reduce([]) do |agg, current|
  26. if current[1]
  27. agg << [current[0]]
  28. else
  29. agg.last << current[0]
  30. agg
  31. end
  32. end
  33. existence_maps = grouped_codes.map { |c| c.map { |cc| [cc, File.exist?(Rails.root.join('public', 'emoji', codepoints_to_filename(cc) + '.svg'))] }.to_h }
  34. map = {}
  35. existence_maps.each do |group|
  36. existing_one = group.key(true)
  37. next if existing_one.nil?
  38. group.each_key do |key|
  39. map[codepoints_to_unicode(key)] = codepoints_to_filename(existing_one)
  40. end
  41. end
  42. map = map.sort { |a, b| a[0].size <=> b[0].size }.to_h
  43. File.write(dest, Oj.dump(map))
  44. puts "Wrote emojo to destination! (#{dest})"
  45. end
  46. end