简体   繁体   中英

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 p elements - 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? eg

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?

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM