Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Convert \r \n to <p> with simple format in Ruby on Rails?

Right now it just adds

\r\n\r\n

I'm trying to figure out how to convert

\r \n to <p> tags 

The HTML gets posted to the DB via XHR, and post in firebug shows it like this:

html:

<html><head>\r\n<meta http-equiv=Content-Type content=text/html; charset=utf-8>\r\n</head>\r\n<body>\r\n<pre style=word-wrap:break-word; font-size:10.0pt; font-family:Tahoma; color:black>YADA YADA YADAYADA YADA YADAYADA YADA YADAYADA YADA YADA!\n\n YADA YADA YADAYADA YADA YADAYADA YADA YADAYADA YADA YADA.\n\nYADA YADA YADA\n\n&lt; via mobile device &gt;\n\nYADA YADA YADA &lt;[email protected]&gt; wrote:\n\n</pre>\r\n<div>\r\n<table cellpadding=0 cellspacing=0 border=0 width=700 style=padding:0;>\r\n<tbody>\r\n<tr>\r\n<td colspan=2 style=color:#000000;font-size:11px;font-family:'lucida grande', tahoma, verdana, arial, sans-serif valign=top>\r\n<div style=width:100%;font-size:12px;font-family:Helvetica;line-height:18px;>..... and it keeps going.

But when I use simple_format:

body = simple_format(@html)

I get this:

 <p>\r\n\r\n\r\n\r\n<pre>YADA YADA YADAYAD........ 

Why isn't simple_format getting rid of the \r and \n and using paragraphs?

Thank you for your time.

like image 258
AnApprentice Avatar asked Feb 25 '23 21:02

AnApprentice


1 Answers

You might be better off doing a string replace first:

@html.gsub!(/\r\n?/, "\n");
body = simple_format(@html)

This because simple_format only works on \n and \n\n, not \r\n:

From the API:

Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in

tags. One newline (\n) is considered as a linebreak and a
tag is appended. This method does not remove the newlines from the text.

like image 175
Guus Bosman Avatar answered Apr 28 '23 05:04

Guus Bosman