Convert line breaks to paragraph tags

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

. What I am wondering
is,
how can I add in the paragraph tags around lines of text (e.g.

some
text
that is line 1

some text that is line 2

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!

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(/^(.)$/, ‘

\1

’)
=> “

this is a line of text

\n

and here is another


irb(main):010:0> a.gsub(/^(.
)$/m, ‘

\1

’)
=> “

this is a line of text\nand here is another

Ryan P. 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

. What I am wondering
is,
how can I add in the paragraph tags around lines of text (e.g.

some
text
that is line 1

some text that is line 2

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!

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

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:

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

Thanks for any help you can offer!

Best,
Ryan

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/, ‘
’).gsub(/%%%/,

’).gsub(/^(.*)$/, ‘

\1

’)
=> “

this is the first line with an empty line after it

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

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 P. 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

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:

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

Thanks for any help you can offer!

Best,
Ryan