-
-
Save laslingerland/87a33ac1750bbbc3c3a68ac18c9c37a6 to your computer and use it in GitHub Desktop.
A [list] BBCode definition for jBBCode using [*] for list items.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "jBBCode" . DIRECTORY_SEPARATOR . "Parser.php"; | |
/** | |
* Implements a [list] code definition that provides the following syntax: | |
* | |
* [list] | |
* [*] first item | |
* [*] second item | |
* [*] third item | |
* [/list] | |
* | |
*/ | |
class ListCodeDefinition extends \JBBCode\CodeDefinition | |
{ | |
public function __construct() | |
{ | |
$this->parseContent = true; | |
$this->useOption = false; | |
$this->setTagName('list'); | |
$this->nestLimit = -1; | |
} | |
public function asHtml(\JBBCode\ElementNode $el) | |
{ | |
$bodyHtml = ''; | |
foreach ($el->getChildren() as $child) { | |
$bodyHtml .= $child->getAsHTML(); | |
} | |
$listPieces = explode('[*]', $bodyHtml); | |
unset($listPieces[0]); | |
$listPieces = array_map(function($li) { return '<li>'.$li.'</li>' . "\n"; }, $listPieces); | |
return '<ul>'.implode('', $listPieces).'</ul>'; | |
} | |
public function asText(\JBBCode\ElementNode $el) | |
{ | |
$bodyText = ''; | |
foreach ($el->getChildren() as $child) { | |
$bodyText .= $child->getAsText(); | |
} | |
$listPieces = explode('[*]', $bodyText); | |
// how to separate list values; by using a variable, we can eliminate the separator before the first list item. | |
$separator = ', '; | |
return substr(implode($separator, $listPieces), strlen($separator)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment