Module: ActionView::Helpers::Markdown

Includes:
CaptureHelper, TagHelper, TextHelper, UrlHelper
Defined in:
lib/flavoured_markdown.rb

Instance Method Summary collapse

Instance Method Details

#flavoured_markdown(str) ⇒ Object Also known as: flavored_markdown



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/flavoured_markdown.rb', line 13

def flavoured_markdown(str)
  str = RDiscount.new(str, :smart, :filter_styles, :filter_html).to_html

  # Extract pre blocks
  str = extract_markup(str, :pre)

  # Undo markdown's html encoding of breaks
  str.gsub!(%r{&lt;br\s*\/?>}m, "<br/>")

  # Undo markdowns' html encoding of hyperlinks, except for those that
  # contain an on* attribute which may contain javascript code.
  str.gsub!(%r{&lt;a\s(.*?)&lt;/a>}m) do
    match = $1
    match =~ %r{\son\w}m ? "&lt;a #{match}&lt;/a>" : "<a #{match}</a>"
  end

  # Extract hyperlink html code
  str = extract_markup(str, :a)

  # Auto hyperlink urls and email addresses
  str = auto_link(str, :all) do |txt|
    txt.size < 55 ? txt : truncate(txt, :length => 50, :omission => "...")
  end

  # Extract the new hyperlinks
  str = extract_markup(str, :a)

  # preserve whitespace for haml
  #str = str.chomp("\n").gsub(/\n/, '&#x000A;').gsub(/\r/, '')

  # Prevent an orphan for P and LI tags
  str.gsub!(%r{<(p|li)\b[^>]*>(.*?)</\1>}m) do |match|
    last_space_index = match.rindex(' ')
    if last_space_index
      match = match.slice(0, last_space_index) + "&#160;" + match.slice(last_space_index + 1, match.length - last_space_index - 1)
    end
    match
  end

  # Add css class "last" to last P tag if it's the last element
  str.sub!(%r{<p>(.*?)</p>\Z}, "<p class=\"last\">\\1</p>")

  # Turn 3 consecutive dot characters into an ellipsis character
  str = hellip(str)

  # Put back in the previously extracted markup
  str = add_extracted_markup(str)
  
  str
end