Class: MadChatter::MessageListeners::Markdown
- Inherits:
-
Object
- Object
- MadChatter::MessageListeners::Markdown
- Defined in:
- lib/mad_chatter/message_listeners/markdown.rb
Instance Method Summary collapse
- #apply_markdown(text) ⇒ Object
-
#handle(message) ⇒ Object
Apply markdown to all messages that pass through.
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 42 43 44 45 46 47 |
# 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 # inline code (backticks) text.gsub!(%r{(^|\s)(\`)(.+?)\2(\s|$)}, %{\\1<code>\\3</code>\\4}) # 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 # reference style links: [link text](http://link.url) text.gsub!(%r{\[([^\]]+)\]\((\S+(?=\)))\)}, %{<a target="_blank" href="\\2">\\1</a>}) # bold (must come before italic) text.gsub!(%r{(^|\s)(\*\*|__)(.+?)\2(\s|$)}, %{\\1<strong>\\3</strong>\\4}) # italic text.gsub!(%r{(^|\s)([*_])(.+?)\2(\s|$)}, %{\\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() .html = apply_markdown(.filtered_text) return end |