misuzu/src/Parsers/BBCode/BBCodeParser.php

59 lines
1.5 KiB
PHP

<?php
namespace Misuzu\Parsers\BBCode;
use Misuzu\Parsers\ParserInterface;
class BBCodeParser implements ParserInterface {
private $tags = [];
public function __construct(array $tags = []) {
if(empty($tags)) {
$tags = [
// Advanced markup
new Tags\CodeTag,
new Tags\QuoteTag,
new Tags\AlignTag,
// Slightly more advanced markup
new Tags\AudioTag,
new Tags\VideoTag,
// Basic markup
new Tags\H1Tag,
new Tags\H2Tag,
new Tags\H3Tag,
new Tags\H4Tag,
new Tags\H5Tag,
new Tags\H6Tag,
new Tags\BoldTag,
new Tags\ItalicsTag,
new Tags\UnderlineTag,
new Tags\StrikeTag,
new Tags\ImageTag,
new Tags\ZalgoTag,
// Links
new Tags\NamedUrlTag,
new Tags\UrlTag,
new Tags\LinkifyTag,
// Finally parse leftover newlines
new Tags\NewLineTag,
new Tags\CodeNewLineHackTag,
];
}
$this->tags = $tags;
}
public function parseText(string $text): string {
foreach($this->tags as $tag)
$text = $tag->parseText($text);
return $text;
}
public function parseLine(string $line): string {
return $this->parseText($line);
}
}