Sha256: b616075bc6c49c43ee85571166e576d1636ff0db73ef515eb97e02feac3196aa

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

require 'mumuki/emojis'

module Mumukit::ContentType::Emoji
  class Render < Redcarpet::Render::HTML
    def initialize(options = {})
      @options = options.merge(:no_intra_emphasis => true)
      super @options
    end

    def paragraph(text)
      text.gsub!("\n", "<br>\n") if @options[:hard_wrap]

      "<p>#{replace_emoji(text)}</p>\n"
    end

    def list_item(text, list_type)
      "<li>#{replace_emoji(text)}</li>"
    end

    # Replaces valid emoji characters, ie :smile:, with img tags
    #
    # Valid emoji charaters are listed in +Mumukit::ContentType::Emoji::EMOJI+
    def replace_emoji(text)
      emojis = Mumuki::Emojis::EMOJIS
      text.gsub(/:([^\s:])+:/) do |short_name|
        short_code = short_name.gsub(':', '')
        emoji = emojis[short_code]
        if emoji
          %{<i class="mu-emoji #{emoji['ca']} _#{emoji['co']}" title="#{short_name}"></i>}
        else
          short_name
        end
      end
    end

    private

    # Returns +true+ if emoji are present in +text+, otherwise returns +false+
    def include_emoji?(text)
      text && text[/:\S+:/]
    end
  end
end

module Mumukit::ContentType::Emoji
  class Engine < ::Rails::Engine
  end if defined? ::Rails::Engine
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mumukit-content-type-1.12.1 lib/mumukit/content_type/emoji.rb
mumukit-content-type-1.12.0 lib/mumukit/content_type/emoji.rb
mumukit-content-type-1.11.1 lib/mumukit/content_type/emoji.rb
mumukit-content-type-1.11.0 lib/mumukit/content_type/emoji.rb
mumukit-content-type-1.10.0 lib/mumukit/content_type/emoji.rb
mumukit-content-type-1.9.0 lib/mumukit/content_type/emoji.rb
mumukit-content-type-1.8.1 lib/mumukit/content_type/emoji.rb