cola
cola

Reputation: 12466

How can i convert/replace every newline to '<br/>'?

set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on

I tried this, content.gsub("\r\n","<br/>") but when I click the view/show button to see the contents of these line, I get the output/result=>

set tabstop=4<br/> set shiftwidth=4<br/> set nu<br/> set ai<br/> syntax on<br/> filetype plugin indent on

But I tried to get those lines as a seperate lines. But all become as a single line. Why?

How can I make all those lines with a html break (<br/>) ?

I tried this, that didn't work.

@addpost = Post.new params[:data]
@temptest = @addpost.content.html_safe
@addpost.content = @temptest
#logger.debug(@addpost)
@addpost.save

Also tried without saving into database. Tried only in view layer,<%= t.content.html_safe %> That didn't work too.

Got this from page source

        <a href="/posts/36">vimrc file</a> <br/>
        2011-12-06<br/><br/>

        set tabstop=4&lt;br/&gt;&lt;br/&gt;set shiftwidth=4&lt;br/&gt;&lt;br/&gt;set nu&lt;br/&gt;&lt;br/&gt;set ai&lt;br/&gt;&lt;br/&gt;syntax on&lt;br/&gt;&lt;br/&gt;filetype plugin indent on<br/>

            <a href="/posts/36/edit">Edit</a>
            <a href="/posts/36" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
        <br/><br/>

Upvotes: 6

Views: 14764

Answers (5)

Dominic Goulet
Dominic Goulet

Reputation: 8113

With Ruby On Rails 4.0.1 comes the simple_format from TextHelper. It will handle more tags than the OP requested, but will filter malicious tags from the content (sanitize).

simple_format(t.content)

Reference : http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html

Upvotes: 6

Dave Newton
Dave Newton

Reputation: 160191

You need to use html_safe if you want to render embedded HTML:

<%= @the_string.html_safe %>

If it might be nil, raw(@the_string) won't throw an exception. I'm a bit ambivalent about raw; I almost never try to display a string that might be nil.

Upvotes: 6

Dominic Goulet
Dominic Goulet

Reputation: 8113

An alternative to convert every new lines to html tags <br> would be to use css to display the content as it was given :

.wrapped-text {
    white-space: pre-wrap;
}

This will wrap the content on a new line, without altering its current form.

Upvotes: 27

jackdoe
jackdoe

Reputation: 1866

http://www.ruby-doc.org/core-1.9.3/String.html as it says there gsub expects regex and replacement since "\n\r" is a string you can see in the docs:

if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ‘d’, instead of a digit.

so you are trying to match "\n\r", you probably want a character class containing \n or \r -[\n\r]

a = <<-EOL
set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on
EOL
print a.gsub(/[\n\r]/,"<br/>\n");

Upvotes: 3

Matt
Matt

Reputation: 10564

I'm not sure I exactly follow the question - are you seeing the output as e.g. preformatted text, or does the source HTML have those tags? If the source HTML has those tags, they should appear on new lines, even if they aren't on line breaks in the source, right?

Anyway, I'm guessing you're dealing with automatic string escaping. Check out this other Stack Overflow question Also, this: Katz talking about this feature

Upvotes: 1

Related Questions