twemojify 0.4.1

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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ .idea
3
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rake'
7
+ gem 'minitest'
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Thomas Tuttle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,147 @@
1
+ Twemojify
2
+ =========
3
+
4
+ Twemojify is a bare-bones port of the official Twitter [twemoji](https://github.com/twitter/twemoji) javascript library.
5
+
6
+
7
+ Installation
8
+ ------------
9
+
10
+ Include the gem in you Gemfile:
11
+
12
+ ```ruby
13
+ gem "twemojify"
14
+ ```
15
+
16
+ Or install directly:
17
+
18
+ ```bash
19
+ gem install twemojify
20
+ ```
21
+
22
+
23
+ Usage
24
+ -----
25
+
26
+ ### parse
27
+
28
+ The core functionality of the Twemojify library:
29
+
30
+ ```ruby
31
+ Twemojify.parse("I \u2764 emoji!")
32
+ ```
33
+
34
+ This method takes the string and returns an html string parsed with the default configuration.
35
+
36
+ ```
37
+ I <img class="emoji" draggable="false" alt="❤" src="//twemoji.maxcdn.com/36x36/2764.png"> emoji!
38
+ ```
39
+
40
+ You can also pass in options, like so:
41
+
42
+ ```ruby
43
+ Twemojify.parse("I \u2764 emoji!", :size => '64x64',:ext => '.jpg')
44
+ ```
45
+
46
+ Which returns:
47
+
48
+ ```
49
+ I <img class="emoji" draggable="false" alt="❤" src="//twemoji.maxcdn.com/64x64/2764.jpg"> emoji!
50
+ ```
51
+
52
+ ### test
53
+
54
+ The test method attempts to find the first emoji in a string:
55
+
56
+ ```ruby
57
+ # returns 2
58
+ Twemojify.test("I \u2764 emoji!")
59
+
60
+ # returns nil
61
+ Twemojify.test("I love emoji!")
62
+ ```
63
+
64
+ ### Rails Helper
65
+
66
+ Twemojify includes a basic rails helper to access the parse method.
67
+
68
+ ```
69
+ <p><%= twemojify(@comment.body) %><p>
70
+ ```
71
+
72
+
73
+ Configuration
74
+ -------------
75
+
76
+ Default values from the original Twitter library are used, but can be customized through an initializer.
77
+
78
+ ```ruby
79
+ # config/initializers/twemojify.rb
80
+ Twemojify.configure do |config|
81
+ config.base = '//twemoji.maxcdn.com/'
82
+ config.ext = '.png'
83
+ config.size = '36x36'
84
+ config.class_name = 'emoji'
85
+ config.folder = nil
86
+ end
87
+ ```
88
+
89
+
90
+ Options
91
+ -------
92
+
93
+ #### base
94
+
95
+ Indicates the base url to prepend to the emoji img src. You can change the base to point to local assets on your own servers.
96
+
97
+ #### ext
98
+
99
+ The file extension used for the emoji.
100
+
101
+ #### size
102
+
103
+ Size of the emoji, default sizes follow a ```[width]x[height]``` format, but you can include any kind of custom sizes
104
+
105
+ #### class_name
106
+
107
+ The class assigned the img tags created by the parse method.
108
+
109
+ #### folder
110
+
111
+ Used to point to svg and other custom resources that don't require sizes. Overrides the size option.
112
+
113
+
114
+ Notes
115
+ -----
116
+
117
+ #### Parsing HTML
118
+
119
+ Be careful when using the parse method with html string, as it does not sanitize the string nor
120
+ check if the string has already been parsed.
121
+
122
+ #### Features NOT included from the original Twitter library
123
+
124
+ - DOM parsing (currently uses a simple regex match)
125
+ - Recognize control characters
126
+
127
+
128
+ Twitter Attribution Requirements
129
+ --------------------------------
130
+
131
+ From the official twitter library [readme](https://github.com/twitter/twemoji/blob/gh-pages/README.md):
132
+
133
+ > As an open source project, attribution is critical from a legal, practical and motivational perspective in our opinion. The graphics are licensed under the CC-BY 4.0 which has a pretty good guide on [best practices for attribution](https://wiki.creativecommons.org/Best_practices_for_attribution).
134
+
135
+ > However, we consider the guide a bit onerous and as a project, will accept a mention in a project README or an 'About' section or footer on a website. In mobile applications, a common place would be in the Settings/About section (for example, see the mobile Twitter application Settings->About->Legal section). We would consider a mention in the HTML/JS source sufficient also.
136
+
137
+
138
+ License
139
+ -------
140
+
141
+ Copyright 2015 Thomas Tuttle
142
+
143
+ MIT License: [http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT)
144
+
145
+
146
+
147
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
9
+
10
+ desc "Open an irb session preloaded with this library"
11
+ task :console do
12
+ sh "irb -rubygems -I lib -r twemojify.rb"
13
+ end
data/lib/twemojify.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'twemojify/configuration'
2
+ require 'twemojify/version'
3
+ require 'twemojify/railtie' if defined?(Rails)
4
+
5
+ module Twemojify
6
+ extend Configuration
7
+
8
+ MATCH_REG_EX = /\u{a9}|\u{ae}|\u{203c}|\u{2049}|\u{2122}|\u{2139}|[\u{2194}-\u{2199}]|[\u{21a9}-\u{21aa}]|[\u{231a}-\u{231b}]|[\u{23e9}-\u{23ec}]|\u{23f0}|\u{23f3}|\u{24c2}|[\u{25aa}-\u{25ab}]|\u{25b6}|\u{25c0}|[\u{25fb}-\u{25fe}]|[\u{2600}-\u{2601}]|\u{260e}|\u{2611}|[\u{2614}-\u{2615}]|\u{261d}|\u{263a}|[\u{2648}-\u{2653}]|\u{2660}|\u{2663}|[\u{2665}-\u{2666}]|\u{2668}|\u{267b}|\u{267f}|\u{2693}|[\u{26a0}-\u{26a1}]|[\u{26aa}-\u{26ab}]|[\u{26bd}-\u{26be}]|[\u{26c4}-\u{26c5}]|\u{26ce}|\u{26d4}|\u{26ea}|[\u{26f2}-\u{26f3}]|\u{26f5}|\u{26fa}|\u{26fd}|\u{2702}|\u{2705}|[\u{2708}-\u{270c}]|\u{270f}|\u{2712}|\u{2714}|\u{2716}|\u{2728}|[\u{2733}-\u{2734}]|\u{2744}|\u{2747}|\u{274c}|\u{274e}|[\u{2753}-\u{2755}]|\u{2757}|\u{2764}|[\u{2795}-\u{2797}]|\u{27a1}|\u{27b0}|\u{27bf}|[\u{2934}-\u{2935}]|[\u{2b05}-\u{2b07}]|[\u{2b1b}-\u{2b1c}]|\u{2b50}|\u{2b55}|\u{3030}|\u{303d}|\u{3297}|\u{3299}|\u{e50a}|\u{1f004}|\u{1f0cf}|[\u{1f170}-\u{1f171}]|[\u{1f17e}-\u{1f17f}]|\u{1f18e}|[\u{1f191}-\u{1f19a}]|[\u{1f1e6}-\u{1f1ff}]|[\u{1f201}-\u{1f202}]|\u{1f21a}|\u{1f22f}|[\u{1f232}-\u{1f23a}]|[\u{1f250}-\u{1f251}]|[\u{1f300}-\u{1f320}]|[\u{1f330}-\u{1f335}]|[\u{1f337}-\u{1f37c}]|[\u{1f380}-\u{1f393}]|[\u{1f3a0}-\u{1f3c4}]|[\u{1f3c6}-\u{1f3ca}]|[\u{1f3e0}-\u{1f3f0}]|[\u{1f400}-\u{1f43e}]|\u{1f440}|[\u{1f442}-\u{1f4f7}]|[\u{1f4f9}-\u{1f4fc}]|[\u{1f500}-\u{1f53d}]|[\u{1f550}-\u{1f567}]|[\u{1f5fb}-\u{1f640}]|[\u{1f645}-\u{1f64f}]|\u{1f1e8}\u{1f1f3}|\u{1f1e9}\u{1f1ea}|\u{1f1ea}\u{1f1f8}|\u{1f1eb}\u{1f1f7}|\u{1f1ec}\u{1f1e7}|\u{1f1ee}\u{1f1f9}|\u{1f1ef}\u{1f1f5}|\u{1f1f0}\u{1f1f7}|\u{1f1f7}\u{1f1fa}|\u{1f1fa}\u{1f1f8}|\u{23}\u{20e3}|\u{30}\u{20e3}|\u{31}\u{20e3}|\u{32}\u{20e3}|\u{33}\u{20e3}|\u{34}\u{20e3}|\u{35}\u{20e3}|\u{36}\u{20e3}|\u{37}\u{20e3}|\u{38}\u{20e3}|\u{39}\u{20e3}/.freeze
9
+
10
+ def self.parse(str, options = {})
11
+ options = Twemojify.options.merge(options)
12
+
13
+ if !str.nil?
14
+ str.gsub(MATCH_REG_EX) do |match|
15
+ %Q|<img class="#{options[:class_name]}" draggable="false" alt="#{match}" src="#{options[:base]}#{options[:folder]||options[:size]}/#{get_codepoint(match)}#{options[:ext]}">|
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.test(str)
21
+ str =~ MATCH_REG_EX
22
+ end
23
+
24
+ private
25
+
26
+ def self.get_codepoint(match)
27
+ match.codepoints.map { |a| a.to_s(16) }.join('-')
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path('../utils', __FILE__)
2
+
3
+ module Twemojify
4
+ module Configuration
5
+ VALID_OPTIONS_KEYS = [
6
+ :base,
7
+ :ext,
8
+ :size,
9
+ :class_name,
10
+ :folder
11
+ ]
12
+
13
+ DEFAULT_BASE = '//twemoji.maxcdn.com/'.freeze
14
+ DEFAULT_EXT = '.png'.freeze
15
+ DEFAULT_SIZE = '36x36'.freeze
16
+ DEFAULT_CLASS_NAME = 'emoji'.freeze
17
+ DEFAULT_FOLDER = nil
18
+
19
+ attr_accessor *VALID_OPTIONS_KEYS
20
+
21
+ def size=(value)
22
+ @size = Utils.to_size_squared_asset(value)
23
+ end
24
+
25
+ def self.extended(base)
26
+ base.reset
27
+ end
28
+
29
+ def configure
30
+ yield self
31
+ end
32
+
33
+ def options
34
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
35
+ option.merge!(key => send(key))
36
+ end
37
+ end
38
+
39
+ def reset
40
+ self.base = DEFAULT_BASE
41
+ self.ext = DEFAULT_EXT
42
+ self.size = DEFAULT_SIZE
43
+ self.class_name = DEFAULT_CLASS_NAME
44
+ self.folder = DEFAULT_FOLDER
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,9 @@
1
+ require 'twemojify/view_helpers'
2
+
3
+ module Twemojify
4
+ class Railtie < Rails::Railtie
5
+ initializer 'twemojify.view_helpers' do
6
+ ActiveSupport.on_load( :action_view ){ include Twemojify::ViewHelpers }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module Twemojify
2
+ module Utils
3
+ def self.to_size_squared_asset(value)
4
+ value.is_a?(Integer) ? "#{value}x#{value}" : value
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Twemojify
2
+ VERSION = '0.4.1'
3
+ end
@@ -0,0 +1,7 @@
1
+ module Twemojify
2
+ module ViewHelpers
3
+ def twemojify(text, options = {})
4
+ Twemojify.parse(html_escape(text), options).html_safe
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest'
2
+ require 'minitest/autorun'
3
+ require 'twemojify'
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class TwemojifyTest < Minitest::Test
4
+ def setup
5
+ Twemojify.reset
6
+ end
7
+
8
+ def test_default_parsing
9
+ assert_equal 'I <img class="emoji" draggable="false" alt="' + "\u2764" + '" src="' + Twemojify.base + '36x36/2764.png"> emoji!',
10
+ Twemojify.parse("I \u2764 emoji!")
11
+ end
12
+
13
+ def test_configure_block
14
+ Twemojify.configure do |config|
15
+ config.size = '48x48'
16
+ config.base = '/base/'
17
+ end
18
+
19
+ assert_equal 'I <img class="emoji" draggable="false" alt="' + "\u2764" + '" src="/base/48x48/2764.png"> emoji!',
20
+ Twemojify.parse("I \u2764 emoji!")
21
+ end
22
+
23
+ def test_parse_method_options
24
+ assert_equal 'I <img class="emoji" draggable="false" alt="' + "\u2764" + '" src="' + Twemojify.base + '64x64/2764.jpg"> emoji!',
25
+ Twemojify.parse("I \u2764 emoji!", :size => '64x64',:ext => '.jpg')
26
+ end
27
+
28
+ def test_folder_option
29
+ Twemojify.folder = 'custom'
30
+ assert_equal 'I <img class="emoji" draggable="false" alt="' + "\u2764" + '" src="' + Twemojify.base + 'custom/2764.png"> emoji!',
31
+ Twemojify.parse("I \u2764 emoji!")
32
+ end
33
+
34
+ def test_test_method
35
+ assert_equal(2, Twemojify.test("I \u2764 emoji!"))
36
+ assert_equal(nil, Twemojify.test("I emoji!"))
37
+ end
38
+ end
data/twemojify.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+ require 'twemojify/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'twemojify'
6
+ s.version = Twemojify::VERSION
7
+ s.summary = "Ruby port of the official Twitter twemoji library."
8
+ s.description = "Ruby port of the official Twitter twemoji library."
9
+ s.licenses = ['MIT']
10
+ s.homepage = 'http://github.com/postliminary/twemojify'
11
+
12
+ s.authors = ["Thomas Tuttle"]
13
+ s.email = 'thomas@postlimiary.com'
14
+
15
+ s.require_paths = ["lib"]
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twemojify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Tuttle
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-04-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby port of the official Twitter twemoji library.
15
+ email: thomas@postlimiary.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/twemojify.rb
26
+ - lib/twemojify/configuration.rb
27
+ - lib/twemojify/railtie.rb
28
+ - lib/twemojify/utils.rb
29
+ - lib/twemojify/version.rb
30
+ - lib/twemojify/view_helpers.rb
31
+ - test/test_helper.rb
32
+ - test/test_twemojify.rb
33
+ - twemojify.gemspec
34
+ homepage: http://github.com/postliminary/twemojify
35
+ licenses:
36
+ - MIT
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.23.2
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Ruby port of the official Twitter twemoji library.
59
+ test_files:
60
+ - test/test_helper.rb
61
+ - test/test_twemojify.rb