Alexander
Alexander

Reputation: 4219

Best way to only keep blank newlines in ruby

I'm making a simple way to parse some markdown-like text. Let's pretend my string looks like this (with the \n characters shown)

hello this\n
is part of the same paragraph\n
\n
this is a separate paragraph\n
\n
\n
\n
this is another one!\n

Right now, I'm adding a new <p> tag per-line, which ends up looking like this -

<p>hello this</p>
<p>is part of the same paragraph</p>
<p></p>
<p>this is a separate paragraph</p>
<p></p>
<p></p>
<p></p>
<p>this is another one!</p>

I reduced this a little bit by using the .squeeze("\n") method in ruby. Then my HTML would look like this-

<p>hello this</p>
<p>is part of the same paragraph</p>
<p>this is a separate paragraph</p>
<p>this is another one!</p>

As you can see, this gets rid of the extra pelements - however the first two lines are still split into paragraphs.

How can I achieve an effect similar to markdown where two returns are required for a new paragraph to occur? e.g.

this is 
part of the same paragraph

new para!

becomes

this is part of the same paragraph
\n
new para!

which becomes...

<p>this is part of the same paragraph</p>
<p>new para!</p>

Is there a regex solution I'm forgetting, maybe?

Upvotes: 3

Views: 77

Answers (2)

seph
seph

Reputation: 6076

You can use the block version of gsub:

str = "hello this
is part of the same paragraph

this is a separate paragraph



this is another one!
"

str.gsub!(/\n+/) { |newlines| newlines[/\n{2,}/] ? "\n" : " " }
str.each_line { |line| puts "<p>#{line.strip}</p>" }

# output
<p>hello this is part of the same paragraph</p>
<p>this is a separate paragraph</p>
<p>this is another one!</p>

Upvotes: 0

Psylone
Psylone

Reputation: 2808

Here's a quick idea:

str = <<-STR
hello this
is part of the same paragraph

this is a separate paragraph



this is another one!
STR

result = ''

result << '<p>'
result << str.gsub!(/\n{2,}/, "</p>\n<p>")
result << '</p>'

puts result


# Output
<p>hello this
is part of the same paragraph</p>
<p>this is a separate paragraph</p>
<p>this is another one!
</p>

Upvotes: 1

Related Questions