This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/BBcodeDefinitions/Lists.php
2016-07-26 19:29:53 +02:00

65 lines
1.3 KiB
PHP

<?php
/**
* Holds the list bbcode class.
*
* @package Sakura
*/
namespace Sakura\BBcodeDefinitions;
use JBBCode\CodeDefinition;
use JBBCode\ElementNode;
/**
* Implements a [list] code definition that provides the following syntax:
*
* [list]
* [*] first item
* [*] second item
* [*] third item
* [/list]
*
* @package Sakura
* @author Jackson Owens <jackson_owens@alumni.brown.edu>
*/
class Lists extends CodeDefinition
{
/**
* Constructor
*/
public function __construct()
{
$this->parseContent = true;
$this->useOption = false;
$this->setTagName('list');
$this->nestLimit = -1;
}
/**
* Compiles the list bbcode to HTML.
*
* @param ElementNode $el The JBBCode element node.
*
* @return string The compiled HTML list.
*/
public function asHtml(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>";
}, $listPieces);
$list = implode('', $listPieces);
return "<ul>{$list}</ul>";
}
}