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.
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.
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...
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.
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
Related
I have a text area into which users enter a lot of text that potentially includes hyperlinks. How can I display the text and have the URLs automatically appear as hyperlinks? Is there a gem, plugin, or existing method that does this? I'm looking to be able to do something like:
<%=h #my_object.description.with_links %> in my view.
Rails 3
Use the provided helper method called auto_link, which is part of ActionPack.
<%=h auto_link(#my_object.description) %>
Rails 4
Auto-linking has been removed from Rails and has been made into the rails_autolink gem.
require 'rails_autolink'
auto_link("Go to http://www.rubyonrails.org and say hello to david#loudthinking.com")
# => "Go to http://www.rubyonrails.org and
# say hello to david#loudthinking.com"
See rinku gem, which is apparently faster and more accurate than autolink gem
I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end
I cant get markdown to work with Slim (Rails 3.1 app), I get this error:
Unknown line indicator
:markdown
I have the following gems in my Gemfile:
gem 'bluecloth'
gem 'rdiscount'
gem 'slim'
This is how my template looks like:
:markdown
#hello
Yes. Slim supports Markdown. You need to put markdown: before you use markdown code in your templates.
Example:
markdown:
#Header
Hello from #{"Markdown!"}
Second Line!
You might be able to use Markdown syntax with a single gem: redcarpet. Nothing against the other gems, but after a couple of years dealing with Rails I really believe less is more, especially when it comes to adding gems to your projects, it can get out of hand very quickly.
This page lists the requirements for Markdown support in Slim:
slim-template
My rails app is using RDiscount to generate HTML from user-supplied markdown text, and I noticed that the anchor tags don't have rel="nofollow". This is a big issue for me as my app is open to the public. Is there a way to enable nofollow links, or are there better solutions?
Thanks!
I think this is possible only with Kramdown, which is a ruby Markdown parser with extended syntax. You would do that then as shown in the link:
[link](test.html){:rel='nofollow'}
In the mean time, I am using this hack, by re-parsing the RDiscount output and add a rel="nofollow" to each anchor:
def markdown(input)
html = RDiscount.new(input).to_html
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("a").each do |link|
link['rel'] = 'nofollow'
end
doc.to_html
end
Though I think this should really be handled by the markdown parser.
I needed to do something similar, add target="_new" to all links. Solved it using Kramdown and a custom Kramdown::Converter::Html class.
Define a Kramdown::Converter::Html subclass (kramdown/converter/my_html.rb in some autoload path)
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html
def convert_a(el, indent)
el.attr['target'] = '_new'
super
end
end
I also have a view helper in app/helpers/application_helper.rb
def markdown(str)
Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe
end
Ideally it should be possible to just use Kramdown::Document.new(str).to_my_html.html_safe but I can't get it to work in rails development mode as Kramdown uses const_defined? to see if a converter is available and that does not trigger the autoloader. Please comment if you know how to fix this.
There is an open feature request on RDiscount to support modifying links in this fashion.
It is scheduled for one of the upcoming RDiscount 2.1.5.x releases.
If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter.
Is is possible to do the same using erb?
It's pretty easy to write a method that does it, assuming you're using something like Rails which has #capture, #concat, and #markdown helpers. Here's an example, using Maruku:
def markdown_filter(&block)
concat(markdown(capture(&block)))
end
Then you can use this as so:
<% markdown_filter do %>
# Title
This is a *paragraph*.
This is **another paragraph**.
<% end %>
There are a few things to note here. First, it's important that all the text in the block have no indentation; you could get around this by figuring out the common indentation of the lines and removing it, but I didn't do that in the example helper above. Second, it uses Rails' #markdown helper, which could easily be implemented in other frameworks as so (replacing Maruku with your Markdown processor of choice):
def markdown(text)
Maruku.new(text).to_html
end
Rails 3 has removed the #markdown helper, so just add the above code in the appropriate helper, substituting the Markdown processor of your choice.
ERB does not have filtering like this built-in. You'll need to directly use a gem that handles it, like RDiscount or the venerable BlueCloth.