gemoji-parser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f5c233d77c332dabf52ca117a87ac008e0df2c2
4
+ data.tar.gz: 79876f028d01361d173c93de9ca527ee6ec71c7b
5
+ SHA512:
6
+ metadata.gz: 20e96eb56067495de4b03295e5484853bf86162d0773203f8b67c7eff47bf1ffda05d8753c67c575fe4dd502e68ffbee231a40f75f2db94eb2908b95db1d52e6
7
+ data.tar.gz: d24710b9c58651f74749ff7fcf2efb00440b04102ffe511e8eaa1b54b066ed5169c9074d8b37eacfb3a6a33fce06117919fc50f77798a08203b2144272eab365
@@ -0,0 +1,15 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Greg MacWilliam
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ # gemoji-parser
2
+
3
+ The missing helper methods for [GitHub's Gemoji](https://github.com/github/gemoji) gem. This utility provides a parsing API for the `Emoji` corelib (provided by Gemoji). The parser includes quick tokenizers for transforming unicode symbols (🐠) into token symbols (`:tropical_fish:`), and arbitrary block replacement methods for custom formatting of symbols.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gemoji-parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gemoji-parser
20
+
21
+ To run tests:
22
+
23
+ $ be rake spec
24
+
25
+ ## Usage
26
+
27
+
28
+ ### Tokenizing
29
+
30
+ These methods perform basic conversions of unicode symbols to token symbols, and vice versa.
31
+
32
+ ```ruby
33
+ EmojiParser.tokenize("Test 🙈 🙊 🙉")
34
+ # "Test :see_no_evil: :speak_no_evil: :hear_no_evil:"
35
+
36
+ EmojiParser.detokenize("Test :see_no_evil: :speak_no_evil: :hear_no_evil:")
37
+ # "Test 🙈 🙊 🙉"
38
+ ```
39
+
40
+ ### Block Parsing
41
+
42
+ For custom symbol transformations, use the block parser methods. All parsers yeild Gemoji `Emoji::Character` instances into the parsing block for custom formatting.
43
+
44
+ **Unicode symbols**
45
+
46
+ ```ruby
47
+ EmojiParser.parse_unicode('Test 🐠') do |emoji|
48
+ %Q(<img src="#{emoji.image_filename}" alt=":#{emoji.name}:">).html_safe
49
+ end
50
+
51
+ # 'Test <img src="unicode/1F420.png" alt=":tropical_fish:">'
52
+ ```
53
+
54
+ **Token symbols**
55
+
56
+ ```ruby
57
+ EmojiParser.parse_tokens('Test :tropical_fish:') do |emoji|
58
+ %Q(<img src="#{emoji.image_filename}" alt=":#{emoji.name}:">).html_safe
59
+ end
60
+
61
+ # 'Test <img src="unicode/1F420.png" alt=":tropical_fish:">'
62
+ ```
63
+
64
+ **All symbols**
65
+
66
+ ```ruby
67
+ EmojiParser.parse_all('Test 🐠 :tropical_fish:') { |emoji| emoji.hex_inspect }
68
+
69
+ # 'Test 1f420 1f420'
70
+ ```
71
+
72
+ ### File Paths
73
+
74
+ A helper is provided for formatting custom filepaths beyond the Gemoji default. This may be useful if you'd like to upload your images to a CDN, and simply reference them from there:
75
+
76
+ ```ruby
77
+ fish = Emoji.find_by_alias('tropical_fish')
78
+ EmojiParser.filepath(fish, '//cdn.fu/emoji/')
79
+ # "//cdn.fu/emoji/1F420.png"
80
+ ```
81
+
82
+ ## Shoutout
83
+
84
+ Thanks to the GitHub team for the [Gemoji](https://github.com/github/gemoji) gem. They're handling all the heavy lifting.
85
+
86
+ ## Contributing
87
+
88
+ 1. Fork it ( https://github.com/gmac/gemoji-parser/fork )
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create a new Pull Request
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ # RSpec
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gemoji-parser/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gemoji-parser"
8
+ s.version = EmojiParser::VERSION
9
+ s.authors = ["Greg MacWilliam"]
10
+ s.email = ["greg.macwilliam@voxmedia.com"]
11
+ s.summary = %q{The missing helper methods for GitHub's Gemoji gem.}
12
+ s.description = %q{Parses emoji unicode symbols and string tokens, allowing for customizable transformations.}
13
+ s.homepage = "https://github.com/gmac/gemoji-parser"
14
+ s.license = "MIT"
15
+
16
+ s.files = `git ls-files -z`.split("\x0")
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ["lib"]
20
+
21
+ s.required_ruby_version = '> 1.9'
22
+
23
+ s.add_dependency "gemoji", ">= 2.1.0"
24
+ s.add_development_dependency "bundler", "~> 1.6"
25
+ s.add_development_dependency "rake", "~> 10.0"
26
+ s.add_development_dependency 'rspec'
27
+ end
@@ -0,0 +1,63 @@
1
+ require 'gemoji-parser/version'
2
+ require 'gemoji'
3
+
4
+ module EmojiParser
5
+ extend self
6
+
7
+ # Generates a regular expression for matching emoji unicodes.
8
+ # Call with "rehash: true" to regenerate the cached regex.
9
+ def emoji_regexp(opts = {})
10
+ return @emoji_regexp if defined?(@emoji_regexp) && !opts[:rehash]
11
+ patterns = []
12
+
13
+ Emoji.all.each do |emoji|
14
+ u = emoji.unicode_aliases.map do |str|
15
+ str.codepoints.map { |c| '\u{%s}' % c.to_s(16).rjust(4, '0') }.join('')
16
+ end
17
+ # Append unicode patterns longest first for broader match:
18
+ patterns.concat u.sort! { |a, b| b.length - a.length }
19
+ end
20
+
21
+ @emoji_regexp = Regexp.new("(#{patterns.join('|')})")
22
+ end
23
+
24
+ # Parses all unicode emoji characters within a string.
25
+ # Provide a block that performs the character transformation.
26
+ def parse_unicode(text)
27
+ text.gsub(emoji_regexp) do |match|
28
+ emoji = Emoji.find_by_unicode($1)
29
+ block_given? && emoji ? yield(emoji) : match
30
+ end
31
+ end
32
+
33
+ # Parses all emoji tokens within a string.
34
+ # Provide a block that performs the token transformation.
35
+ def parse_tokens(text)
36
+ text.gsub(/:([\w+-]+):/) do |match|
37
+ emoji = Emoji.find_by_alias($1.to_s)
38
+ block_given? && emoji ? yield(emoji) : match
39
+ end
40
+ end
41
+
42
+ # Parses all emoji unicodes and tokens within a string.
43
+ # Provide a block that performs all transformations.
44
+ def parse_all(text)
45
+ text = parse_unicode(text) { |emoji| yield(emoji) }
46
+ parse_tokens(text) { |emoji| yield(emoji) }
47
+ end
48
+
49
+ # Transforms all unicode emoji into token strings.
50
+ def tokenize(text)
51
+ parse_unicode(text) { |emoji| ":#{emoji.name}:" }
52
+ end
53
+
54
+ # Transforms all token strings into unicode emoji.
55
+ def detokenize(text)
56
+ parse_tokens(text) { |emoji| emoji.raw }
57
+ end
58
+
59
+ # Generates a custom emoji file path.
60
+ def filepath(emoji, path='/')
61
+ [path.sub(/\/$/, ''), emoji.image_filename.split('/').pop].join('/')
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module EmojiParser
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,88 @@
1
+ # coding: utf-8
2
+ require 'gemoji-parser'
3
+
4
+ describe EmojiParser do
5
+ let(:test_unicode) { 'Test 🙈 🙊 🙉 😰 :invalid: 🐠.' }
6
+ let(:test_mixed) { 'Test 🙈 🙊 🙉 :cold_sweat: :invalid: :tropical_fish:.' }
7
+ let(:test_tokens) { 'Test :see_no_evil: :speak_no_evil: :hear_no_evil: :cold_sweat: :invalid: :tropical_fish:.' }
8
+
9
+ describe '#emoji_regexp' do
10
+ it 'generates once and remains cached.' do
11
+ first = EmojiParser.emoji_regexp
12
+ second = EmojiParser.emoji_regexp
13
+ expect(first).to be second
14
+ end
15
+
16
+ it 'regenerates when called with a :rehash option.' do
17
+ first = EmojiParser.emoji_regexp
18
+ second = EmojiParser.emoji_regexp(rehash: true)
19
+ expect(first).not_to be second
20
+ end
21
+ end
22
+
23
+ describe '#parse_unicode' do
24
+ it 'replaces all valid emoji unicode via block transformation.' do
25
+ parsed = EmojiParser.parse_unicode(test_mixed) { |emoji| 'X' }
26
+ expect(parsed).to eq "Test X X X :cold_sweat: :invalid: :tropical_fish:."
27
+ end
28
+ end
29
+
30
+ describe '#parse_tokens' do
31
+ it 'replaces all valid emoji tokens via block transformation.' do
32
+ parsed = EmojiParser.parse_tokens(test_tokens) { |emoji| 'X' }
33
+ expect(parsed).to eq "Test X X X X :invalid: X."
34
+ end
35
+ end
36
+
37
+ describe '#parse_all' do
38
+ it 'replaces all valid emoji unicode and tokens via block transformation.' do
39
+ parsed = EmojiParser.parse_all(test_mixed) { |emoji| 'X' }
40
+ expect(parsed).to eq "Test X X X X :invalid: X."
41
+ end
42
+ end
43
+
44
+ describe '#tokenize' do
45
+ it 'successfully tokenizes all Gemoji unicode aliases.' do
46
+ Emoji.all.each do |emoji|
47
+ emoji.unicode_aliases.each do |u|
48
+ tokenized = EmojiParser.tokenize("Test #{u}")
49
+ expect(tokenized).to eq "Test :#{emoji.name}:"
50
+ end
51
+ end
52
+ end
53
+
54
+ it 'replaces all valid emoji unicodes with their token equivalent.' do
55
+ tokenized = EmojiParser.tokenize(test_mixed)
56
+ expect(tokenized).to eq test_tokens
57
+ end
58
+ end
59
+
60
+ describe '#detokenize' do
61
+ it 'replaces all valid emoji tokens with their raw unicode equivalent.' do
62
+ tokenized = EmojiParser.detokenize(test_mixed)
63
+ expect(tokenized).to eq test_unicode
64
+ end
65
+ end
66
+
67
+ describe '#filepath' do
68
+ let (:test_emoji) { Emoji.find_by_alias('de') }
69
+ let (:test_file) { '1f1e9-1f1ea.png' }
70
+
71
+ it 'formats a Gemoji image path as a root location by default.' do
72
+ path = EmojiParser.filepath(test_emoji)
73
+ expect(path).to eq "/#{test_file}"
74
+ end
75
+
76
+ it 'formats a Gemoji image path as a custom location (with trailing slash).' do
77
+ images_path = '//fonts.test.com/emoji/'
78
+ path = EmojiParser.filepath(test_emoji, images_path)
79
+ expect(path).to eq "#{images_path}#{test_file}"
80
+ end
81
+
82
+ it 'formats a Gemoji image path to a custom location (no trailing slash).' do
83
+ images_path = '//fonts.test.com/emoji'
84
+ path = EmojiParser.filepath(test_emoji, images_path)
85
+ expect(path).to eq "#{images_path}/#{test_file}"
86
+ end
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gemoji-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg MacWilliam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gemoji
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Parses emoji unicode symbols and string tokens, allowing for customizable
70
+ transformations.
71
+ email:
72
+ - greg.macwilliam@voxmedia.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - gemoji-parser.gemspec
83
+ - lib/gemoji-parser.rb
84
+ - lib/gemoji-parser/version.rb
85
+ - spec/emoji_helper_spec.rb
86
+ homepage: https://github.com/gmac/gemoji-parser
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">"
97
+ - !ruby/object:Gem::Version
98
+ version: '1.9'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.2
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: The missing helper methods for GitHub's Gemoji gem.
110
+ test_files:
111
+ - spec/emoji_helper_spec.rb