negarmoji 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Emoji
4
+ class Character
5
+ # Inspect individual Unicode characters in a string by dumping its
6
+ # codepoints in hexadecimal format.
7
+ def self.hex_inspect(str)
8
+ str.codepoints.map { |c| c.to_s(16).rjust(4, '0') }.join('-')
9
+ end
10
+
11
+ # True if the emoji is not a standard Emoji character.
12
+ def custom?() !raw end
13
+
14
+ # A list of names uniquely referring to this emoji.
15
+ attr_reader :aliases
16
+
17
+ # The category for this emoji as per Apple's character palette
18
+ attr_accessor :category
19
+
20
+ # The Unicode description text
21
+ attr_accessor :description
22
+
23
+ # The Unicode spec version where this emoji first debuted
24
+ attr_accessor :unicode_version
25
+
26
+ # The iOS version where this emoji first debuted
27
+ attr_accessor :ios_version
28
+
29
+ def name() aliases.first end
30
+
31
+ def add_alias(name)
32
+ aliases << name
33
+ end
34
+
35
+ # A list of Unicode strings that uniquely refer to this emoji.
36
+ attr_reader :unicode_aliases
37
+
38
+ # Raw Unicode string for an emoji. Nil if emoji is non-standard.
39
+ def raw() unicode_aliases.first end
40
+
41
+ def add_unicode_alias(str)
42
+ unicode_aliases << str
43
+ end
44
+
45
+ # A list of tags associated with an emoji. Multiple emojis can share the
46
+ # same tags.
47
+ attr_reader :tags
48
+
49
+ def add_tag(tag)
50
+ tags << tag
51
+ end
52
+
53
+ def initialize(name)
54
+ @aliases = Array(name)
55
+ @unicode_aliases = []
56
+ @tags = []
57
+ end
58
+
59
+ def inspect
60
+ hex = '(%s)' % hex_inspect unless custom?
61
+ %(#<#{self.class.name}:#{name}#{hex}>)
62
+ end
63
+
64
+ def hex_inspect
65
+ self.class.hex_inspect(raw)
66
+ end
67
+
68
+ attr_writer :image_filename
69
+
70
+ def image_filename
71
+ if defined? @image_filename
72
+ @image_filename
73
+ else
74
+ default_image_filename
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def default_image_filename
81
+ if custom?
82
+ '%s.svg' % name
83
+ else
84
+ hex_name = hex_inspect.gsub(/-(fe0f|200d)\b/, '')
85
+ 'unicode/%s.svg' % hex_name
86
+ end
87
+ end
88
+ end
89
+ end
data/lib/emoji.rb ADDED
@@ -0,0 +1,213 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'emoji/character'
5
+ require 'json'
6
+
7
+ module Emoji
8
+ extend self
9
+
10
+ def data_file
11
+ File.expand_path('../../db/emoji.json', __FILE__)
12
+ end
13
+
14
+ def all
15
+ return @all if defined? @all
16
+ @all = []
17
+ parse_data_file
18
+ @all
19
+ end
20
+
21
+ # Public: Initialize an Emoji::Character instance and yield it to the block.
22
+ # The character is added to the `Emoji.all` set.
23
+ def create(name)
24
+ emoji = Emoji::Character.new(name)
25
+ self.all << edit_emoji(emoji) { yield emoji if block_given? }
26
+ emoji
27
+ end
28
+
29
+ # Public: Yield an emoji to the block and update the indices in case its
30
+ # aliases or unicode_aliases lists changed.
31
+ def edit_emoji(emoji)
32
+ @names_index ||= Hash.new
33
+ @unicodes_index ||= Hash.new
34
+
35
+ yield emoji
36
+
37
+ emoji.aliases.each do |name|
38
+ @names_index[name] = emoji
39
+ end
40
+ emoji.unicode_aliases.each do |unicode|
41
+ @unicodes_index[unicode] = emoji
42
+ end
43
+
44
+ emoji
45
+ end
46
+
47
+ # Public: Find an emoji by its aliased name. Return nil if missing.
48
+ def find_by_alias(name)
49
+ names_index[name]
50
+ end
51
+
52
+ # Public: Find an emoji by its unicode character. Return nil if missing.
53
+ def find_by_unicode(unicode)
54
+ unicodes_index[unicode]
55
+ end
56
+
57
+ private
58
+ VARIATION_SELECTOR_16 = "\u{fe0f}".freeze
59
+
60
+ # Characters which must have VARIATION_SELECTOR_16 to render as color emoji:
61
+ TEXT_GLYPHS = [
62
+ "\u{1f237}", # Japanese “monthly amount” button
63
+ "\u{1f202}", # Japanese “service charge” button
64
+ "\u{1f170}", # A button (blood type)
65
+ "\u{1f171}", # B button (blood type)
66
+ "\u{1f17e}", # O button (blood type)
67
+ "\u{00a9}", # copyright
68
+ "\u{00ae}", # registered
69
+ "\u{2122}", # trade mark
70
+ "\u{3030}", # wavy dash
71
+ "\u{263a}", # smiling face
72
+ "\u{261D}", # index pointing up
73
+ "\u{270C}", # victory hand
74
+ "\u{270D}", # writing hand
75
+ "\u{2764}", # red heart
76
+ "\u{2763}", # heavy heart exclamation
77
+ "\u{2668}", # hot springs
78
+ "\u{2708}", # airplane
79
+ "\u{2600}", # sun
80
+ "\u{2601}", # cloud
81
+ "\u{2602}", # umbrella
82
+ "\u{2744}", # snowflake
83
+ "\u{2603}", # snowman
84
+ "\u{2660}", # spade suit
85
+ "\u{2665}", # heart suit
86
+ "\u{2666}", # diamond suit
87
+ "\u{2663}", # club suit
88
+ "\u{260e}", # telephone
89
+ "\u{2709}", # envelope
90
+ "\u{270F}", # pencil
91
+ "\u{2712}", # black nib
92
+ "\u{2702}", # scissors
93
+ "\u{26a0}", # warning
94
+ "\u{2B06}", # up arrow
95
+ "\u{2197}", # up-right arrow
96
+ "\u{27A1}", # right arrow
97
+ "\u{2198}", # down-right arrow
98
+ "\u{2B07}", # down arrow
99
+ "\u{2199}", # down-left arrow
100
+ "\u{2B05}", # left arrow
101
+ "\u{2196}", # up-left arrow
102
+ "\u{2195}", # up-down arrow
103
+ "\u{2194}", # left-right arrow
104
+ "\u{21A9}", # right arrow curving left
105
+ "\u{21AA}", # left arrow curving right
106
+ "\u{2934}", # right arrow curving up
107
+ "\u{2935}", # right arrow curving down
108
+ "\u{2721}", # star of David
109
+ "\u{262F}", # yin yang
110
+ "\u{271D}", # latin cross
111
+ "\u{25B6}", # play button
112
+ "\u{25C0}", # reverse button
113
+ "\u{23CF}", # eject button
114
+ "\u{2640}", # female sign
115
+ "\u{2642}", # male sign
116
+ "\u{267B}", # recycling symbol
117
+ "\u{2611}", # ballot box with check
118
+ "\u{2714}", # heavy check mark
119
+ "\u{2716}", # heavy multiplication x
120
+ "\u{303D}", # part alternation mark
121
+ "\u{2733}", # eight-spoked asterisk
122
+ "\u{2734}", # eight-pointed star
123
+ "\u{2747}", # sparkle
124
+ "\u{203C}", # double exclamation mark
125
+ "\u{2049}", # exclamation question mark
126
+ "\u{23}\u{20E3}", # keycap: #
127
+ "\u{2A}\u{20E3}", # keycap: *
128
+ "\u{30}\u{20E3}", # keycap: 0
129
+ "\u{31}\u{20E3}", # keycap: 1
130
+ "\u{32}\u{20E3}", # keycap: 2
131
+ "\u{33}\u{20E3}", # keycap: 3
132
+ "\u{34}\u{20E3}", # keycap: 4
133
+ "\u{35}\u{20E3}", # keycap: 5
134
+ "\u{36}\u{20E3}", # keycap: 6
135
+ "\u{37}\u{20E3}", # keycap: 7
136
+ "\u{38}\u{20E3}", # keycap: 8
137
+ "\u{39}\u{20E3}", # keycap: 9
138
+ "\u{2139}", # information
139
+ "\u{24C2}", # circled M
140
+ "\u{1F17F}", # P button
141
+ "\u{3297}", # Japanese “congratulations” button
142
+ "\u{3299}", # Japanese “secret” button
143
+ "\u{25AA}", # black small square
144
+ "\u{25AB}", # white small square
145
+ "\u{25FB}", # white medium square
146
+ "\u{25FC}", # black medium square
147
+ ].freeze
148
+
149
+ private_constant :VARIATION_SELECTOR_16, :TEXT_GLYPHS
150
+
151
+ def parse_data_file
152
+ data = File.open(data_file, 'r:UTF-8') do |file|
153
+ JSON.parse(file.read, symbolize_names: true)
154
+ end
155
+
156
+ if "".respond_to?(:-@)
157
+ # Ruby >= 2.3 this is equivalent to .freeze
158
+ # Ruby >= 2.5 this will freeze and dedup
159
+ dedup = lambda { |str| -str }
160
+ else
161
+ dedup = lambda { |str| str.freeze }
162
+ end
163
+
164
+ append_unicode = lambda do |emoji, raw|
165
+ unless TEXT_GLYPHS.include?(raw) || emoji.unicode_aliases.include?(raw)
166
+ emoji.add_unicode_alias(dedup.call(raw))
167
+ end
168
+ end
169
+
170
+ data.each do |raw_emoji|
171
+ self.create(nil) do |emoji|
172
+ raw_emoji.fetch(:aliases).each { |name| emoji.add_alias(dedup.call(name)) }
173
+ if raw = raw_emoji[:emoji]
174
+ append_unicode.call(emoji, raw)
175
+ start_pos = 0
176
+ while found_index = raw.index(VARIATION_SELECTOR_16, start_pos)
177
+ # register every variant where one VARIATION_SELECTOR_16 is removed
178
+ raw_alternate = raw.dup
179
+ raw_alternate[found_index] = ""
180
+ append_unicode.call(emoji, raw_alternate)
181
+ start_pos = found_index + 1
182
+ end
183
+ if start_pos > 0
184
+ # register a variant with all VARIATION_SELECTOR_16 removed
185
+ append_unicode.call(emoji, raw.gsub(VARIATION_SELECTOR_16, ""))
186
+ else
187
+ # register a variant where VARIATION_SELECTOR_16 is added
188
+ append_unicode.call(emoji, "#{raw}#{VARIATION_SELECTOR_16}")
189
+ end
190
+ end
191
+ raw_emoji.fetch(:tags).each { |tag| emoji.add_tag(dedup.call(tag)) }
192
+
193
+ emoji.category = dedup.call(raw_emoji[:category])
194
+ emoji.description = dedup.call(raw_emoji[:description])
195
+ emoji.unicode_version = dedup.call(raw_emoji[:unicode_version])
196
+ emoji.ios_version = dedup.call(raw_emoji[:ios_version])
197
+ end
198
+ end
199
+ end
200
+
201
+ def names_index
202
+ all unless defined? @all
203
+ @names_index
204
+ end
205
+
206
+ def unicodes_index
207
+ all unless defined? @all
208
+ @unicodes_index
209
+ end
210
+ end
211
+
212
+ # Preload emoji into memory
213
+ Emoji.all
data/lib/negarmoji.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'emoji'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: negarmoji
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mohammad Mahdi Bgahbani Pourvahid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Character information and metadata for Unicode emoji.
14
+ email: MahdiBaghbani@protonmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - db/emoji.json
22
+ - lib/emoji.rb
23
+ - lib/emoji/character.rb
24
+ - lib/negarmoji.rb
25
+ homepage: https://gitlab.com/Azadeh-Afzar/Web-Development/Negareh-Emoji-Library
26
+ licenses:
27
+ - GPL-3.0
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">"
36
+ - !ruby/object:Gem::Version
37
+ version: '1.9'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.0.4
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Unicode emoji library
48
+ test_files: []