emojis.rake 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/5.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. group.each_key do |key|
  38. map[codepoints_to_unicode(key)] = codepoints_to_filename(existing_one)
  39. end
  40. end
  41. map = map.sort { |a, b| a[0].size <=> b[0].size }.to_h
  42. File.write(dest, Oj.dump(map))
  43. puts "Wrote emojo to destination! (#{dest})"
  44. end
  45. end