Skip to content

Instantly share code, notes, and snippets.

@jbowens
Last active April 9, 2021 09:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jbowens/5646994 to your computer and use it in GitHub Desktop.
Save jbowens/5646994 to your computer and use it in GitHub Desktop.
A [list] BBCode definition for jBBCode using [*] for list items.
<?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>';
}
}
@dhardtke
Copy link

dhardtke commented Oct 5, 2013

How can I get this working with Packagist/Composer?
The following instead of that require_once doesn't work:

namespace JBBCode;

@laslingerland
Copy link

In my fork of this gist, the asText functionality now also works for list tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment