jbowens / jBBCode

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

Support for list tags #9

Open aops opened 11 years ago

aops commented 11 years ago

Enhancement suggestion: Support for list tags [li] and its various attributes as well a [*] which indicated each new list item.

Or is this currently possible some how?

jbowens commented 11 years ago

This is possible with the current version but with limitations. You could define a [list] bbcode and a list item bbcode. This type of syntax is possible:

[list]
  [li]first item[/li]
  [li]second item[/li]
  [li]third item[/li]
[/list]

However, there's currently no way to prohibit li bbcodes from existing outside any list bbcode block. Additionally, the ideal syntax would likely be:

[list]
  [*] first item
  [*] second item
  [*] third item
[/list]

This is not possible with jBBCode because there's no support for unary tags. Limiting bbcodes to certain contexts and unary tags would both require modifications to the parser, but it's definitely something to consider. In the meantime, if you want to create bbcodes for the example above, you could do something like:

$builder = new JBBCode\CodeDefinitionBuilder('list', '<ul>{param}</ul>');
$parser->addCodeDefinition($builder->build());
$builder = new JBBCode\CodeDefinitionBuilder('li', '<li>{param}</li>');
$parser->addCodeDefinition($builder->build());
jbowens commented 11 years ago

I take that back. This is possible right now by defining a custom CodeDefinition. I made a gist with an example:

https://gist.github.com/jbowens/5646994

Since I don't think there are very many other use cases for unary tags, this might be preferable to baking support into the parser itself. This does have limitations though. Since you're hacking with already produced HTML of the children of the list element, this will do nasty things if there are unclosed tags within lists.

t1gor commented 9 years ago

It would be also great if there was a functionality that could read the bbCode template from phpBB (just as an example) and use the read template as a CodeDefinitionSet

axelson commented 9 years ago

Any chance that gist can be merged into the main code base? I'd argue that lists are a common feature.

jbowens commented 9 years ago

@axelson Not as is. That gist is pretty hacky. While I agree it'd be nice to add list support by default, I'd want to find a better way to do it. In the meantime, the API is designed for adding arbitrary code definitions, so there's not much of a burden for developers who do want to use that code definition.

DaSourcerer commented 9 years ago

Somewhat unrelated but then again not: I'm not really satisfied with how the SmileyVisitor is working. Are there any plans to make (text)nodes splitable? I'd like to keep smileys and freestanding URLs separate so they won't conflict with each other.