Class: MadChatter::MessageListeners::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/mad_chatter/message_listeners/markdown.rb

Instance Method Summary collapse

Instance Method Details

#apply_markdown(text) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mad_chatter/message_listeners/markdown.rb', line 11

def apply_markdown(text)

  # in very clear cases, let newlines become <br /> tags
  text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
    x =~ /\n{2}/ ? x : (x.strip!; x << "  \n")
  end

  # autolink email addresses
  text.gsub!(/([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})/i) do |x|
    x = %Q{<a href="mailto:#{$&}">#{$&}</a>}
  end

  # autolink urls
  text.gsub!(/(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/(\S)+)?/i) do |x|
    href = $&
     # check if we're in a markdown link
     if $` =~ /[(]/
      x = href
     else
      x = %Q{<a target="_blank" href="#{href}">#{href}</a>}
     end
  end
  
  # bold (must come before italic)
  text.gsub!(%r{(^|\s)(\*\*|__)(.+?)\2(\s|$)}x, %{\\1<strong>\\3</strong>\\4})
  
  # italic
  text.gsub!(%r{(^|\s)([*_])(.+?)\2(\s|$)}x, %{\\1<em>\\3</em>\\4})
  
  return text
end

#handle(message) ⇒ Object

Apply markdown to all messages that pass through



6
7
8
9
# File 'lib/mad_chatter/message_listeners/markdown.rb', line 6

def handle(message)
  message.html = apply_markdown(message.filtered_text)
  return message
end