Update usage examples in the README with the new API
This commit is contained in:
12
README.md
12
README.md
@@ -39,7 +39,7 @@ Then have them compiled to public on deploy.
|
||||
|
||||
``` ruby
|
||||
# config/application.rb
|
||||
config.assets.precompile << "emoji/*.png"
|
||||
config.assets.precompile << "emoji/**/*.png"
|
||||
```
|
||||
|
||||
**WARNING** Since there are a ton of images, just adding the path may slow down other lookups if you aren't using it. Compiling all the emojis on deploy will add overhead to your deploy if even the images haven't changed. Theres just so many more superfluous files to iterate over. Also, the urls will be fingerprinted which may not be ideal for referencing from cached content.
|
||||
@@ -55,9 +55,9 @@ See the [Emoji cheat sheet](http://www.emoji-cheat-sheet.com) for more examples.
|
||||
```ruby
|
||||
module EmojiHelper
|
||||
def emojify(content)
|
||||
h(content).to_str.gsub(/:([a-z0-9\+\-_]+):/) do |match|
|
||||
if Emoji.names.include?($1)
|
||||
'<img alt="' + $1 + '" height="20" src="' + asset_path("emoji/#{$1}.png") + '" style="vertical-align:middle" width="20" />'
|
||||
h(content).to_str.gsub(/:([\w+-]+):/) do |match|
|
||||
if emoji = Emoji.find_by_alias($1) { nil }
|
||||
%(<img alt="#$1" src="#{asset_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
|
||||
else
|
||||
match
|
||||
end
|
||||
@@ -72,9 +72,9 @@ Unicode mapping
|
||||
Translate emoji names to unicode and vice versa.
|
||||
|
||||
```ruby
|
||||
>> Emoji.unicode_for("cat")
|
||||
>> Emoji.find_by_alias("cat").raw
|
||||
=> "🐱" # Don't see a cat? That's U+1F431.
|
||||
|
||||
>> Emoji.name_for("\u{1f431}")
|
||||
>> Emoji.find_by_unicode("\u{1f431}").name
|
||||
=> "cat"
|
||||
```
|
||||
|
||||
46
test/documentation_test.rb
Normal file
46
test/documentation_test.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
require 'test_helper'
|
||||
|
||||
# Pull the EmojiHelper example from the docs
|
||||
readme = File.expand_path('../../README.md', __FILE__)
|
||||
docs = File.open(readme, 'r:UTF-8') { |f| f.read }
|
||||
eval docs.match(/^module.+?^end/m)[0]
|
||||
|
||||
String.class_eval do
|
||||
def html_safe() self end
|
||||
def present?() !empty? end
|
||||
end
|
||||
|
||||
class DocumentationTest < TestCase
|
||||
module Helper
|
||||
extend EmojiHelper
|
||||
|
||||
def self.h(str)
|
||||
str.gsub('<', '<').gsub('>', '>')
|
||||
end
|
||||
|
||||
def self.asset_path(img)
|
||||
"/images/#{img}?123"
|
||||
end
|
||||
end
|
||||
|
||||
test "replaces emoji syntax with images" do
|
||||
assert_equal "It's raining " \
|
||||
'<img alt="cat" src="/images/emoji/unicode/1f431.png?123" style="vertical-align:middle" width="20" height="20" />s and ' \
|
||||
'<img alt="dog" src="/images/emoji/unicode/1f436.png?123" style="vertical-align:middle" width="20" height="20" />s!',
|
||||
Helper.emojify("It's raining :cat:s and :dog:s!")
|
||||
end
|
||||
|
||||
test "doesn't replace unknown emoji" do
|
||||
content = ":jupiter: is in :space:"
|
||||
assert_equal content, Helper.emojify(content)
|
||||
end
|
||||
|
||||
test "escapes other HTML" do
|
||||
assert_equal "You have been <script>alert('pwned!')</script>",
|
||||
Helper.emojify("You have been <script>alert('pwned!')</script>")
|
||||
end
|
||||
|
||||
test "returns nil for blank content" do
|
||||
assert_nil Helper.emojify('')
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user