BlueCloth isn't working with Rails 3
Asked Answered
N

4

5

Is BlueCloth compatible with Rails 3? I can't make it work, maybe someone uses it?

There is supposed to be a helper called 'markdown' available in the views after requiring 'bluecloth', but this doesn't seem to be available.

Newsy answered 30/8, 2010 at 15:56 Comment(1)
having this problem, tooJugurtha
M
10

I'm upgrading an app to rails3 right now and it worked fine for me. I use a helper function called "format" in templates though the code below also provides a markdown function (in rails3 you'll have to use that with raw()). Here's the contents of my [project]/app/helpers/application_helper.rb

module ApplicationHelper
  # Format text for display.                                                                    
  def format(text)
    sanitize(markdown(text))
  end

  # Process text with Markdown.                                                                 
  def markdown(text)
    BlueCloth::new(text).to_html
  end
end

Like a previous poster said, you'll also need

gem 'bluecloth'

in your [project]/Gemfile. My template looks like:

<p><%= format @post.body %></p>

With the markdown function it would be:

<p><%= raw(markdown(@post.body)) %></p>

So I use the format function. Rename the functions however you want.

Mcginley answered 21/10, 2010 at 2:54 Comment(1)
I would add .html_safe to the string your helper returns. Then it's simply "<%= markdown @post.body %>", no need for "raw".Drover
S
2

I've created a fresh Rails 3 app and in the Gemfile I added:

gem 'bluecloth', '>= 2.0.0'

Then opened the console:

ruby-1.8.7-p302 > BlueCloth.new('**hello**').to_html
=> "<p><strong>hello</strong></p>"

So it appears to be working, at least for me.

You could also try Rdiscount which I am not shure but I think is based on the same C library, or at least has similar benchmarks.

You should be more specific in how is it not working: Does it raises an error? Doesn't it renders html? etc...

Spiker answered 27/9, 2010 at 9:40 Comment(2)
Yes, the BlueCloth library works, but there is no 'markdown' helper available.Jugurtha
I've allways defined my helper in ApplicationHelper, I imagine BlueGem is not rails specific, does BlueGem includes a Rails helper?Spiker
W
0

What you could do, not saying it is pretty, is creating an initializer in your rails project and put the following in it:

require 'bluecloth'

class String
 def markdown
   BlueCloth.new(self).to_html
 end
end

This should enable the markdown method on every string object.

Windowshop answered 3/10, 2010 at 13:56 Comment(0)
C
0

I'd suggest switching to RDiscount over BlueCloth. It's a drop in replacement and is better on all counts.

http://github.com/rtomayko/rdiscount

Chastitychasuble answered 26/10, 2010 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.