How to convert \ r \ n to <p> using a simple format in Ruby on Rails?

Now he just adds

\r\n\r\n

I'm trying to figure out how to convert

\r \n to <p> tags 

HTML is sent to DB via XHR, and a post in firebug shows this as follows:

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;xxxx@xxxxx.com&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 doesn't simple_format get rid of \ r and \ n and use paragraphs?

Thank you for your time.

+3
source share
1 answer

You might be better off replacing the line first:

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

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

From the API :

, HTML, . (\n\n)

. (\n)
. .

+6

Source: https://habr.com/ru/post/1796434/


All Articles