From 2530b241213b19c0f58dc8d69b53bbe8287c2b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 3 Jun 2014 03:42:26 +0700 Subject: [PATCH] Update usage examples in the README with the new API --- README.md | 12 +++++----- test/documentation_test.rb | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 test/documentation_test.rb diff --git a/README.md b/README.md index 35036e6..a4bf442 100644 --- a/README.md +++ b/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) - '' + $1 + '' + h(content).to_str.gsub(/:([\w+-]+):/) do |match| + if emoji = Emoji.find_by_alias($1) { nil } + %(#$1) 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" ``` diff --git a/test/documentation_test.rb b/test/documentation_test.rb new file mode 100644 index 0000000..cd8fcd3 --- /dev/null +++ b/test/documentation_test.rb @@ -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 " \ + 'cats and ' \ + 'dogs!', + 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 ") + end + + test "returns nil for blank content" do + assert_nil Helper.emojify('') + end +end