veger / ruby-bbcode

Convert BBCode to HTML and check whether the BBCode is valid
http://rubygems.org/gems/ruby-bbcode
MIT License
28 stars 29 forks source link

Use <p></p> instead of <br/> #19

Open veger opened 9 years ago

veger commented 9 years ago

It is nicer (HTML) to use paragraphs (<p>...</p>) instead of <br /> for newlines/paragraphs.

Furthermore, the <p> HTML elements can be styled (e.g. using CSS) making the output more flexible

TheNotary commented 9 years ago

+1

veger commented 9 years ago

Simple adding paragraphs using something like:

node[:text].gsub("\r\n", "\n").map {|s| "<p>#{s}</p>"}.join("")

results in (lots of) paragraphs around each text section, for example

"This is [b]bold[/b] and this [i]italic[/i]."

becomes

 "<p>This is</p><b><p>bold</p></b><p> and this </p><i><p>italic</p></i><p>.</p>"

Lots of paragraphs, even without a single newline!

Using

node[:text].gsub("\r\n", "\n").gsub("\n", "</p>\n<p>")

and putting the opening and ending tag before and after the test, solves this But it easily results in invalid HTML. The following is valid bbcode:

"This is [b] line 1
and this[/b] line 2."

but would become

"<p>This is <b> line 1</p>
<p>and this </b> line 2.</p>"

So solving this issue is not as trivial as I initially assumed and some more thoughts on the subject are required...

veger commented 9 years ago

Additionally, using the <p>-tags, limits the allowed contents (no tables, frames, blocks, etc.). So it might be better to use <div>-tags, as these support any/most content.

almaron commented 5 years ago

Just a reminder. According to the official reference, newlines should not be treated as paragraph borders (see tag [*]). Double newlines however can be.