jbowens / jBBCode

A lightweight but extensible BBCode parser
http://jbbcode.com
MIT License
164 stars 32 forks source link

Parse returns #63

Open Studio384 opened 9 years ago

Studio384 commented 9 years ago

Hello,

I'm trying to put jBBCode into a project of mine. For that, the code I've written fetches data from a database and puts that code through jBBCode, however, jBBCode seems to ignore returns completely. So text written in separate paragraphs make just a single paragraph. Is there any way to put every block of text between the p-tag in HTML without users having to put additional BBCode in their comments?

Thanks in advance.

DaSourcerer commented 8 years ago

Auto-paragraphs are difficult to achieve. You can try to split the output by double linebreaks and insert tags there. But the error rate would probably be high. IMHO it would be much easier to implement a NodeVisitor that is applying nl2br() on all TextNodes.

Art4 commented 8 years ago

I have implemented this feature in a parser for a project: https://github.com/youthweb/bbcode-parser But it isn't simple.

I implemented it this (theoretical easy) way: After $parser->getAsHtml() replace a single line break with <br /> and two line breaks with </p>\n<p>. Then prepand a <p> and append a </p> to the text. See https://github.com/youthweb/bbcode-parser/blob/master/src/Manager.php#L132

The problem is that not every HTML tag should be inside the p-Tag (like <list>, <code> or <h1>). So I prepand and append a marker where not to add a <p>. See https://github.com/youthweb/bbcode-parser/blob/master/src/DefinitionSet/HeadlineSet.php#L35

You can also look at the testsuite to see, how the parser will work: https://github.com/youthweb/bbcode-parser/blob/master/tests/integration/ParsingTest.php#L142

I hope this will help you.