BlueCloth isn't working with Rails 3

2020-05-28 11:13发布

问题:

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.

回答1:

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.



回答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...



回答3:

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.



回答4:

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