Forum: Ruby on Rails Convert line breaks to paragraph tags

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-03-13 02:49
(Received via mailing list)
Hi All!

I have a small problem with a data conversion that I am doing from
WordPress
to a custom app that I am building in rails. In WordPress, line breaks
were
handled in the PHP code and converted to <p></p>. What I am wondering
is,
how can I add in the paragraph tags around lines of text (e.g. <p>some
text
that is line 1</p><p>some text that is line 2</p> etc...)? I've seen
gems
that might be able to do this, but I'd prefer to stay away from that
route.
Also, I'd prefer to stick away from markdown formatting since we already
have many entries that utilize raw HTML.

Thanks in advance!
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2006-03-13 04:30
The first regex will do pretty much what you said: Wrap newline
separated strings in paragraph tags. The second will wrap groups of
lines in a paragraph tag set. You can get way more elegant with the
regexes.

=> "this is a line of text\nand here is another"
irb(main):009:0> a.gsub(/^(.*)$/, '<p>\1</p>')
=> "<p>this is a line of text</p>\n<p>and here is another</p>"
irb(main):010:0> a.gsub(/^(.*)$/m, '<p>\1</p>')
=> "<p>this is a line of text\nand here is another</p>"

Ryan Prins wrote:
> Hi All!
>
> I have a small problem with a data conversion that I am doing from
> WordPress
> to a custom app that I am building in rails. In WordPress, line breaks
> were
> handled in the PHP code and converted to <p></p>. What I am wondering
> is,
> how can I add in the paragraph tags around lines of text (e.g. <p>some
> text
> that is line 1</p><p>some text that is line 2</p> etc...)? I've seen
> gems
> that might be able to do this, but I'd prefer to stay away from that
> route.
> Also, I'd prefer to stick away from markdown formatting since we already
> have many entries that utilize raw HTML.
>
> Thanks in advance!
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-03-13 06:18
(Received via mailing list)
Thanks Steve! That's excellent. Now, I wish I only knew regular
expressions
better, because I have one last question. How and I get it do put a <br
/>
after lines that don't have a space in between them. So, for example, if
I
have the following text:

This is a first line with an empty line after it.

This is a 2nd line with one following it.
This is the 3rd line.

I would like the HTML to be like this:

<p>This is a first line with an empty line after it.</p>
<p>This is a 2nd line with one following it.<br />This is the 3rd
line<br
/></p>

Thanks for any help you can offer!

Best,
Ryan
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2006-03-13 07:48
I typed this ugly stuff into irb and it appears to capture the case you
describe. Geez, this is ugly. Do any of you regex gurus know a better
way to do this?

irb(main):006:0> a.gsub(/\n\n/, '%%%').gsub(/\n/, '<br />').gsub(/%%%/,
'</p><p>').gsub(/^(.*)$/, '<p>\1</p>')
=> "<p>this is the first line with an empty line after it</p><p>this is
a 2nd line with one following it<br />this is the 3rd line</p>"

Alternatively, you could do something with splitting the lines:

e.g.:

a = line.split("\n")

then based on the array, piece it back together how you want it.


Ryan Prins wrote:
> Thanks Steve! That's excellent. Now, I wish I only knew regular
> expressions
> better, because I have one last question. How and I get it do put a <br
> />
> after lines that don't have a space in between them. So, for example, if
> I
> have the following text:
>
> This is a first line with an empty line after it.
>
> This is a 2nd line with one following it.
> This is the 3rd line.
>
> I would like the HTML to be like this:
>
> <p>This is a first line with an empty line after it.</p>
> <p>This is a 2nd line with one following it.<br />This is the 3rd
> line<br
> /></p>
>
> Thanks for any help you can offer!
>
> Best,
> Ryan
This topic is locked and can not be replied to.