Skip to content

Commit

Permalink
Update usage examples in the README with the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jun 27, 2014
1 parent 5d24012 commit 2530b24
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Then have them compiled to public on deploy.


``` ruby ``` ruby
# config/application.rb # 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. **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.
Expand All @@ -55,9 +55,9 @@ See the [Emoji cheat sheet](http://www.emoji-cheat-sheet.com) for more examples.
```ruby ```ruby
module EmojiHelper module EmojiHelper
def emojify(content) def emojify(content)
h(content).to_str.gsub(/:([a-z0-9\+\-_]+):/) do |match| h(content).to_str.gsub(/:([\w+-]+):/) do |match|
if Emoji.names.include?($1) if emoji = Emoji.find_by_alias($1) { nil }
'<img alt="' + $1 + '" height="20" src="' + asset_path("emoji/#{$1}.png") + '" style="vertical-align:middle" width="20" />' %(<img alt="#$1" src="#{asset_path("emoji/#{emoji.image_filename}")}" style="vertical-align:middle" width="20" height="20" />)
else else
match match
end end
Expand All @@ -72,9 +72,9 @@ Unicode mapping
Translate emoji names to unicode and vice versa. Translate emoji names to unicode and vice versa.


```ruby ```ruby
>> Emoji.unicode_for("cat") >> Emoji.find_by_alias("cat").raw
=> "🐱" # Don't see a cat? That's U+1F431. => "🐱" # Don't see a cat? That's U+1F431.


>> Emoji.name_for("\u{1f431}") >> Emoji.find_by_unicode("\u{1f431}").name
=> "cat" => "cat"
``` ```
46 changes: 46 additions & 0 deletions test/documentation_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -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('<', '&lt;').gsub('>', '&gt;')
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 &lt;script&gt;alert('pwned!')&lt;/script&gt;",
Helper.emojify("You have been <script>alert('pwned!')</script>")
end

test "returns nil for blank content" do
assert_nil Helper.emojify('')
end
end

0 comments on commit 2530b24

Please sign in to comment.