Replace string with substring that is html safe [duplicate] - ruby-on-rails

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

Related

Does Ruby/Slim have support for markdown?

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

Rails automatically escpaping HTML - how to stop it?

I'm working on upgrading an old Rails app (1.1.6) to Rails 3. Obviously, a lot has changed. One thing appears to be that Rails automatically escapes content dropped into the view. However, I have a situation where I have a helper generating IMG tags for me, and Rails is automatically escaping the resulting content.
<%= random_image('public/images/headers') %>
This results in escaped content, much like one would expect had I done this (in 1.1.6)
<%= h random_image('public/images/headers') %>
Is there a way to tell it to not escape?
<%= raw random_image('public/images/headers') %>
.html_safe
It may need to be inside the helper
There are there ways in which this can be achieved in rails 3 application
html_safe
raw
h
raw and h can only be used in controller and views these methods are defined in helpers.
html_safe can be used anywhere in a rails application i.e., can be used in models, helpers, controller etc.
For more information please read http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/

BlueCloth isn't working with Rails 3

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

Possible to embed markdown within erb?

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.

BBCode for Ruby on Rails

So I'm putting together a simple forum. I'd like to allow my users limited formatting options and BBCode would be plenty for my users. Knowing that I'm assuredly not the first one to want to use BBCode with RoR I googled but couldn't find a straight forward tutorial on how to create a editor which accepts BBCode nor a way to parse and display BBCode formatted input.
Any help or guides would be appreciated!
You should give bb-ruby a try. Its documentation on the web page seems to be very clear and straightforward.
Here is another gem you may find useful
http://github.com/jarrett/rbbcode
Gemfile
gem 'bb-ruby'
# run `bundle`
In the place (haml):
%h1= put_header_string.bbcode_to_html.html_safe
%p= "[b]bold text[/b]".bbcode_to_html.html_safe
Besides a builtins you could also extend your own bbcode as you need. For example:
module BBRuby
##tags = ##tags.merge({
'Email' => [
/\[email(:.*)?\](.*?)\[\/file\1?\]/mi,
lambda{ |e| "<span class='email'>#{e[2].gsub('#','<i>(at)</i>')}</span>"},
'protect email from spam',
'[email]electronic#test.ru[/email]',
:email
],
})
end
In place
[b]Contact me:[/b][email]email#test.ru[/email]
Contact me: email(at)test.ru
bb-ruby on github | bb-ruby on rubygems | bb-ruby home | tags processed list

Resources