Commit 2dc2c4b2 by Jason Axelson

Implement the ability to pass a block to encode_io and encode

This allows a library user to easily customize the representation of an
emoji (mapping it to a custom image for example)
parent 3abd7063
...@@ -8,7 +8,15 @@ module Rumoji ...@@ -8,7 +8,15 @@ module Rumoji
# Transform emoji into its cheat-sheet code # Transform emoji into its cheat-sheet code
def encode(str) def encode(str)
str.gsub(Emoji::ALL_REGEXP) { |match| (emoji = Emoji.find_by_string(match)) && emoji.code || match } str.gsub(Emoji::ALL_REGEXP) do |match|
if emoji = Emoji.find_by_string(match)
if block_given?
yield emoji
else
emoji.code || match
end
end
end
end end
# Transform a cheat-sheet code into an Emoji # Transform a cheat-sheet code into an Emoji
...@@ -16,9 +24,9 @@ module Rumoji ...@@ -16,9 +24,9 @@ module Rumoji
str.gsub(/:([^\s:]?[\w-]+):/) { |match| (Emoji.find($1) || match).to_s } str.gsub(/:([^\s:]?[\w-]+):/) { |match| (Emoji.find($1) || match).to_s }
end end
def encode_io(readable, writeable=StringIO.new("")) def encode_io(readable, writeable=StringIO.new(""), &block)
readable.each_line do |line| readable.each_line do |line|
writeable.write encode(line) writeable.write encode(line, &block)
end end
writeable writeable
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment