Module: Prarupa::TextHelpers

Included in:
ActionView::Base
Defined in:
lib/prarupa/text_helpers.rb

Instance Method Summary collapse

Instance Method Details

#markdown(text, *options) ⇒ Object

Returns the text with all the Markdown codes turned into HTML tags. This method requires RDiscount[http://github.com/rtomayko/rdiscount/] to be available.

Examples

markdown("We are using __Markdown__ now!")
# => "<p>We are using <strong>Markdown</strong> now!</p>"

markdown("We like to _write_ `code`, not just _read_ it!")
# => "<p>We like to <em>write</em> <code>code</code>, not just <em>read</em> it!</p>"

markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.")
# => "<p>The <a href="http://daringfireball.net/projects/markdown/">Markdown website</a>
#     has more information.</p>"

markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")')
# => '<p><img src="http://rubyonrails.com/images/rails.png" alt="The ROR logo" title="Ruby on Rails" /></p>'


81
82
83
84
85
86
87
88
89
90
# File 'lib/prarupa/text_helpers.rb', line 81

def markdown(text, *options)
  text = h(text) unless text.html_safe? || options.delete(:safe)
  
  if text.blank?
    ""
  else
    markdowned = RDiscount.new(text, *options)
    markdowned.to_html.chomp #used chomp to remove the \n appended by RDiscount
  end.html_safe
end

#textilize(text, *options) ⇒ Object

Returns the text with all the Textile codes turned into HTML tags.

You can learn more about Textile’s syntax at its website. This method requires RedCloth[http://redcloth.org/] to be installed.

Examples

textilize("*This is Textile!*  Rejoice!")
# => "<p><strong>This is Textile!</strong>  Rejoice!</p>"

textilize("I _love_ ROR(Ruby on Rails)!")
# => "<p>I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!</p>"

textilize("h2. Textile makes markup -easy- simple!")
# => "<h2>Textile makes markup <del>easy</del> simple!</h2>"

textilize("Visit the Rails website "here":http://www.rubyonrails.org/.)
# => "<p>Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>.</p>"

textilize("This is worded <strong>strongly</strong>")
# => "<p>This is worded <strong>strongly</strong></p>"

textilize("This is worded <strong>strongly</strong>", :filter_html)
# => "<p>This is worded &lt;strong&gt;strongly&lt;/strong&gt;</p>"


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prarupa/text_helpers.rb', line 27

def textilize(text, *options)
  options ||= [:hard_breaks]
  text = h(text) unless text.html_safe? || options.delete(:safe)

  if text.blank?
    ""
  else
    textilized = RedCloth.new(text, options)
    textilized.to_html
  end.html_safe
end

#textilize_without_paragraph(text, *options) ⇒ Object

Returns the text with all the Textile codes turned into HTML tags, but without the bounding <p> tag that RedCloth adds.

You can learn more about Textile’s syntax at its website. This method is only available if RedCloth[http://redcloth.org/] is available.

Examples

textilize_without_paragraph("*This is Textile!*  Rejoice!")
# => "<strong>This is Textile!</strong>  Rejoice!"

textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!")
# => "I <em>love</em> <acronym title="Ruby on Rails">ROR</acronym>!"

textilize_without_paragraph("h2. Textile makes markup -easy- simple!")
# => "<h2>Textile makes markup <del>easy</del> simple!</h2>"

textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.)
# => "Visit the Rails website <a href="http://www.rubyonrails.org/">here</a>."


57
58
59
60
61
62
# File 'lib/prarupa/text_helpers.rb', line 57

def textilize_without_paragraph(text, *options)
  textiled = textilize(text, *options)
  if textiled[0..2] == "<p>" then textiled = textiled[3..-1] end
  if textiled[-4..-1] == "</p>" then textiled = textiled[0..-5] end
  return textiled
end