Skip to content

Commit

Permalink
Merge pull request #122 from bradly/emoji-pipeline-asset-path
Browse files Browse the repository at this point in the history
Adding support to supply the path to the emoji sprite in the emoji pipeline
  • Loading branch information
Simeon Willbanks committed Apr 4, 2014
2 parents f5d51b4 + 0227203 commit 16ef9e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Pipeline
#
# Context:
# :asset_root (required) - base url to link to emoji sprite
# :asset_path (optional) - url path to link to emoji sprite. :file_name can be used as a placeholder for the sprite file name. If no asset_path is set "emoji/:file_name" is used.
class EmojiFilter < Filter
# Build a regexp that matches all valid :emoji: names.
EmojiPattern = /:(#{Emoji.names.map { |name| Regexp.escape(name) }.join('|')}):/
Expand Down Expand Up @@ -56,10 +57,22 @@ def asset_root
context[:asset_root]
end

# The url path to link emoji sprites
#
# :file_name can be used in the asset_path as a placeholder for the sprite file name. If no asset_path is set in the context "emoji/:file_name" is used.
# Returns the context's asset_path or the default path if no context asset_path is given.
def asset_path(name)
if context[:asset_path]
context[:asset_path].gsub(":file_name", "#{::CGI.escape(name)}.png")
else
File.join("emoji", "#{::CGI.escape(name)}.png")
end
end

private

def emoji_url(name)
File.join(asset_root, "emoji", "#{::CGI.escape(name)}.png")
File.join(asset_root, asset_path(name))
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/html/pipeline/emoji_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ def test_required_context_validation
}
assert_match /:asset_root/, exception.message
end

def test_custom_asset_path
filter = EmojiFilter.new("<p>:+1:</p>", {:asset_path => ':file_name', :asset_root => 'https://foo.com'})
doc = filter.call
assert_match "https://foo.com/%2B1.png", doc.search('img').attr('src').value
end
end

0 comments on commit 16ef9e0

Please sign in to comment.