Sha256: bdcab94c90cc7a2b4cad96f26b89120e44fe3fd90b38b58bc68d1a2f492590c8

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module RailsEmoji
  class << self

    def render(*args)
      options = args.extract_options!.stringify_keys
      host = options['host'] || RailsEmoji.host
      return if args[0].blank?

      if options.key? 'size'
        if options['size'] =~ %r{\A\d+x\d+\z}
          options['width'], options['height'] = options['size'].split('x')
        else
          options['width'] = options['height'] = options['size']
        end
      end

      options.slice! 'class', 'width', 'height', 'title', 'alt'
      options.reverse_merge! 'class' => 'emoji', 'width' => 20, 'height' => 20

      # replace emoji code to image tag
      args[0].gsub(/:([a-z0-9_+-]+):/) do |emoji|
        return emoji unless RailsEmoji::EMOJI.include? emoji[1..-2]

        attrs = options.reverse_merge(
          'src' => "#{host}/assets/emojis/#{emoji[1..-2]}.png",
          'alt' => emoji,
          'title' => emoji
        ).select{ |k, v| !v.blank? }.collect{ |k, v| %(#{k}="#{v}") }

        %(<img #{attrs * ' '} />)
      end
    end

  end
end

# extend to String class
class String

  def emojify
    RailsEmoji.render(self)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_emoji-1.7.1 lib/rails_emoji/render.rb