Reputation: 563
I use jBBCode for bb codes parsing.
I want to extend CodeDefinition class to write my own asHtml function as in example in documentation.
Mytag (see the code fragment) may have an option and may have not. And if there's no one, I want it to become the default value.
But I have to do setUseOption() to tell jBBCode whether my tag requires option or not. It can only be true or false.
If I set true, then [mytag]without option[/mytag]
fails to proceed.
If I set false, [mytag=value]with option[/mytag]
fails.
Is there a way to allow proceeding tags both with and without option? I.e. making option be optional. And set the default value for it?
I haven't found this in the documentation.
Code fragment:
<?php
class MyTag extends JBBCode\CodeDefinition {
public function __construct()
{
parent::__construct();
$this->setTagName("mytag");
$this->setUseOption(true);
}
...
Upvotes: 0
Views: 508
Reputation: 606
Take a look at the definitions for the default url and img bbcodes in DefaultCodeDefinitionSet.php. These provide optional arguments by defining the bbcode twice:
/* [url] link tag */
$builder = new CodeDefinitionBuilder('url', '<a href="{param}">{param}</a>');
$builder->setParseContent(false)->setBodyValidator($urlValidator);
array_push($this->definitions, $builder->build());
/* [url=http://example.com] link tag */
$builder = new CodeDefinitionBuilder('url', '<a href="{option}">{param}</a>');
$builder->setUseOption(true)->setParseContent(true)->setOptionValidator($urlValidator);
array_push($this->definitions, $builder->build());
The parser then takes care of matching instances of the bbcode with the appropriate definition.
Upvotes: 1