twemoji 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c06920bca31cca0d6695bd1a6cb0ead8419278c
4
- data.tar.gz: 905c191024265cf8b5727d6622b008396b705331
3
+ metadata.gz: 90100c2eb50b5ac0db62f5637bc68b75047f06b6
4
+ data.tar.gz: ef47c39ac68d556a1fe59fd0c9ceb64a42d81087
5
5
  SHA512:
6
- metadata.gz: 89a774a07e59e3f5ac582123c673b9a32e6a97d2b520e5c8b629902ee108c888076379c31ef7813a1a06b97a48dc44bf376334da7ce391e25fca9004346219b5
7
- data.tar.gz: eb9926492d5dc315d668dde24a6319db337f1d0ce89617315f0d792d83d74c06e4e95bf24f06247910d9d1e6462b18ddf87cf97fef670643e1d0c4d48384a1c4
6
+ metadata.gz: 54e064e15efb4e9ec6eec5aba88d591f89ed32c314f033f2583a64d0ec985cc02e575a328191ab5b4975f18d463b159ca3e4347b1ad3f2f943786cded3f81dd2
7
+ data.tar.gz: c295e141f0f963a00bfd3fd3af5db56c05b44514522815884410fc641410653a3640ed919cf038454d6667d894c3eacb157d479bd85a836e8eda9390b748ee27
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 3.1.0 - 2016.12.13
6
+
7
+ - `Twemoji.parse` can now parse unicode values, too [#35](https://github.com/jollygoodcode/twemoji/pull/35)
8
+ * [New API] `Twemoji.emoji_pattern_unicode` - List all emoji unicodes in a regex
9
+ * [New API] `Twemoji.emoji_pattern_all` - List all emoji names and unicodes in a regex
10
+
5
11
  ## 3.0.2 - 2016.11.15
6
12
 
7
13
  - Fix handling codepoint less than 0x1000 [#33](https://github.com/jollygoodcode/twemoji/pull/33)
data/README.md CHANGED
@@ -124,11 +124,29 @@ More options could be passed in, please see [Twemoji.parse options](https://gith
124
124
 
125
125
  #### `Twemoji.parse`
126
126
 
127
+ Parses for both name tokens (e.g. :heart_eyes:) or unicode values (e.g. `\u1f60d`).
128
+
129
+ Parsing by name token:
130
+
127
131
  ```ruby
128
132
  > Twemoji.parse "I like chocolate :heart_eyes:!"
129
133
  => 'I like chocolate <img draggable="false" title=":heart_eyes:" alt="😍" src="https://twemoji.maxcdn.com/2/svg/1f60d.svg" class="emoji">!'
130
134
  ```
131
135
 
136
+ Parsing by name unicode values:
137
+
138
+ ```ruby
139
+ > Twemoji.parse "I like chocolate 😍!"
140
+ => 'I like chocolate <img draggable="false" title=":heart_eyes:" alt="😍" src="https://twemoji.maxcdn.com/2/svg/1f60d.svg" class="emoji">!'
141
+ ```
142
+
143
+ Parsing by both name and unicode:
144
+
145
+ ```ruby
146
+ > Twemoji.parse ":cookie: 🎂"
147
+ => '<img draggable="false" title=":cookie:" alt="🍪" src="https://twemoji.maxcdn.com/2/svg/1f36a.svg" class="emoji"> <img draggable="false" title=":birthday:" alt="🎂" src="https://twemoji.maxcdn.com/2/svg/1f382.svg" class="emoji">'
148
+ ```
149
+
132
150
  ##### `Twemoji.parse` options
133
151
 
134
152
  ##### `asset_root`
@@ -185,6 +203,18 @@ attribute value can apply proc-like object, remove `:` from title attribute:
185
203
  => /(:mahjong:|:black_joker:| ... |:registered_sign:|:shibuya:)/
186
204
  ```
187
205
 
206
+ #### `Twemoji.emoji_pattern_unicode`
207
+
208
+ ```ruby
209
+ > Twemoji.emoji_pattern_unicode
210
+ ```
211
+
212
+ #### `Twemoji.emoji_pattern_all` = `emoji_pattern` + `emoji_pattern_unicode`
213
+
214
+ ```ruby
215
+ > Twemoji.emoji_pattern_all
216
+ ```
217
+
188
218
  #### JSON for your front-end
189
219
 
190
220
  We prepare two constants: [Twemoji::PNG](lib/twemoji/png.rb) and [Twemoji::SVG](lib/twemoji/svg.rb) (**not loaded by default**), you need to require them to use:
@@ -279,6 +309,11 @@ A huge THANK YOU to all our [contributors](https://github.com/jollygoodcode/twem
279
309
 
280
310
  The emoji keywords are from [jollygoodcode/emoji-keywords](https://github.com/jollygoodcode/emoji-keywords).
281
311
 
312
+ ## Guidelines
313
+
314
+ This project subscribes to the [Moya Contributors Guidelines](https://github.com/Moya/contributors)
315
+ which TLDR: means we give out push access easily and often.
316
+
282
317
  ## License
283
318
 
284
319
  Please see the [LICENSE.md](/LICENSE.md) file.
@@ -70,7 +70,7 @@ module Twemoji
70
70
  # @param raw [String] Emoji raw unicode to find text.
71
71
  # @return [String] Emoji Text.
72
72
  def self.find_by_unicode(raw)
73
- invert_codes[raw.split("").map { |r| "%x" % r.ord }.join("-")]
73
+ invert_codes[unicode_to_str(raw)]
74
74
  end
75
75
 
76
76
  # Render raw emoji unicode from emoji text or emoji code.
@@ -128,6 +128,24 @@ module Twemoji
128
128
  @emoji_pattern ||= /(#{codes.keys.map { |name| Regexp.quote(name) }.join("|") })/
129
129
  end
130
130
 
131
+ # Return all emoji patterns' regular expressions in unicode.
132
+ # e.g '1f1f2-1f1fe' will be converted to \u{1f1f2}\u{1f1fe} for RegExp matching.
133
+ #
134
+ # @return [RegExp] A Regular expression consists of all emojis unicode.
135
+ def self.emoji_pattern_unicode
136
+ @emoji_pattern_unicode ||= /(#{sorted_codepoint_values.map { |name| Regexp.quote(name.split('-').collect {|n| n.hex}.pack("U*")) }.join("|") })/
137
+ end
138
+
139
+ # Return all emoji patterns' regular expressions in unicode and name.
140
+ #
141
+ # @return [RegExp] A Regular expression consists of all emojis unicode codepoint and names.
142
+ def self.emoji_pattern_all
143
+ names = codes.keys.map { |name| Regexp.quote(name) }.join("|")
144
+ codepoints = sorted_codepoint_values.map { |name| Regexp.quote(name.split('-').collect {|n| n.hex}.pack("U*")) }.join("|")
145
+
146
+ @emoji_pattern_all ||= /(#{names}|#{codepoints})/
147
+ end
148
+
131
149
  private
132
150
 
133
151
  # Ensure text is a string.
@@ -150,26 +168,27 @@ module Twemoji
150
168
  # Parse a HTML String, replace emoji text with corresponding emoji image.
151
169
  #
152
170
  # @param text [String] Text string to be parse.
171
+ # @param filter [Symbol] Symbol of filter function to use. Available filters
172
+ # are :filter_emoji and :filter_emoji_unicode.
153
173
  # @return [String] Text with emoji text replaced by emoji image.
154
174
  # @private
155
- def self.parse_html(text)
156
- return text if !text.include?(":")
157
-
158
- filter_emojis(text)
175
+ def self.parse_html(text, filter = :filter_emojis)
176
+ self.send(filter, text)
159
177
  end
160
178
 
161
179
  # Parse a Nokogiri::HTML::DocumentFragment document, replace emoji text
162
180
  # with corresponding emoji image.
163
181
  #
164
182
  # @param doc [Nokogiri::HTML::DocumentFragment] Document to parse.
183
+ # @param filter [Symbol] Symbol of filter function to use. Available filters
184
+ # are :filter_emoji and :filter_emoji_unicode.
165
185
  # @return [Nokogiri::HTML::DocumentFragment] Parsed document.
166
186
  # @private
167
- def self.parse_document(doc)
187
+ def self.parse_document(doc, filter = :filter_emojis)
168
188
  doc.xpath(".//text() | text()").each do |node|
169
189
  content = node.to_html
170
- next if !content.include?(":")
171
190
  next if has_ancestor?(node, %w(pre code tt))
172
- html = filter_emojis(content)
191
+ html = self.send(filter, content)
173
192
  next if html == content
174
193
  node.replace(html)
175
194
  end
@@ -192,12 +211,12 @@ module Twemoji
192
211
 
193
212
  # Filter emoji text in content, replaced by corresponding emoji image.
194
213
  #
195
- # @param content [String] Contetn to filter emoji text to image.
214
+ # @param content [String] Content to filter emoji text to image.
196
215
  # @return [String] Returns a String just like content with all emoji text
197
216
  # replaced by the corresponding emoji image.
198
217
  # @private
199
- def self.filter_emojis(content)
200
- content.gsub(emoji_pattern) { |match| img_tag(match) }
218
+ def self.filter_emojis(content, pattern = emoji_pattern_all)
219
+ content.gsub(pattern) { |match| img_tag(match) }
201
220
  end
202
221
 
203
222
  # Returns emoji image tag by given name and options from `Twemoji.parse`.
@@ -206,7 +225,10 @@ module Twemoji
206
225
  # @return [String] Emoji image tag generated by name and options.
207
226
  # @private
208
227
  def self.img_tag(name)
209
- img_attrs_hash = default_attrs(name).merge! customized_attrs(name)
228
+ # choose default attributes based on name or unicode codepoint
229
+ default_attrs = name.include?(":") ? default_attrs(name) : default_attrs_unicode(name)
230
+ text_name = name.include?(":") ? name : find_by_unicode(name)
231
+ img_attrs_hash = default_attrs.merge! customized_attrs(text_name)
210
232
 
211
233
  %(<img #{hash_to_html_attrs(img_attrs_hash)}>)
212
234
  end
@@ -219,8 +241,8 @@ module Twemoji
219
241
  # @param name [String] Emoji name to generate image url.
220
242
  # @return [String] Emoji image tag generated by name and options.
221
243
  # @private
222
- def self.emoji_url(name)
223
- code = find_by_text(name)
244
+ def self.emoji_url(name, finder = :find_by_text)
245
+ code = self.send(finder, name)
224
246
 
225
247
  if options[:file_ext] == "png"
226
248
  File.join(options[:asset_root], PNG_IMAGE_SIZE, "#{code}.png")
@@ -238,12 +260,18 @@ module Twemoji
238
260
  # @return Hash of customized attributes
239
261
  # @private
240
262
  def self.customized_attrs(name)
241
- custom_img_attributes = options[:img_attrs]
263
+ custom_img_attributes = {}
242
264
 
243
- # run value filters where given
244
- custom_img_attributes.each do |key, value|
245
- custom_img_attributes[key] = value.call(name) if value.respond_to?(:call)
265
+ options[:img_attrs].each do |key, value|
266
+ # run value filters where given
267
+ if value.respond_to?(:call)
268
+ custom_img_attributes[key] = value.call(name)
269
+ else
270
+ custom_img_attributes[key] = value
271
+ end
246
272
  end
273
+
274
+ custom_img_attributes
247
275
  end
248
276
 
249
277
  # Default img attributes: draggable, title, alt, src.
@@ -260,6 +288,30 @@ module Twemoji
260
288
  }
261
289
  end
262
290
 
291
+ # Default img attributes for unicode: draggable, title, alt, src.
292
+ #
293
+ # @param unicode [String] Emoji unicode codepoint.
294
+ # @return Hash of default attributes
295
+ # @private
296
+ def self.default_attrs_unicode(unicode)
297
+ {
298
+ draggable: "false".freeze,
299
+ title: find_by_unicode(unicode),
300
+ alt: unicode,
301
+ src: emoji_url(unicode, :unicode_to_str)
302
+ }
303
+ end
304
+
305
+ # Convert raw unicode to string key version.
306
+ #
307
+ # e.g. 🇲🇾 converts to "1f1f2-1f1fe"
308
+ # @param unicode [String] Unicode codepoint.
309
+ # @return String representation of unicode codepoint
310
+ # @private
311
+ def self.unicode_to_str(unicode)
312
+ unicode.split("").map { |r| "%x" % r.ord }.join("-")
313
+ end
314
+
263
315
  # Coverts hash of attributes into HTML attributes.
264
316
  #
265
317
  # @param hash [Hash] Hash of attributes.
@@ -268,4 +320,12 @@ module Twemoji
268
320
  def self.hash_to_html_attrs(hash)
269
321
  hash.map { |attr, value| %(#{attr}="#{value}") }.join(" ")
270
322
  end
323
+
324
+ # Return sorted codepoint values by descending length.
325
+ #
326
+ # @return [Array] An array of emoji codepoint values sorted by descending length
327
+ def self.sorted_codepoint_values
328
+ # has to be sorted to match the combined codepoint (2-3 char emojis) before single char emojis
329
+ @sorted_codepoint_values ||= invert_codes.keys.sort_by {|key| key.length }.reverse
330
+ end
271
331
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Twemoji
4
- VERSION = "3.0.2"
4
+ VERSION = "3.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twemoji
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanito
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-15 00:00:00.000000000 Z
12
+ date: 2016-12-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  requirements: []
70
70
  rubyforge_project:
71
- rubygems_version: 2.6.7
71
+ rubygems_version: 2.5.2
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: 'A RubyGem to convert :heart: to Twitter cdn url'