Added [align=] bbcode.

This commit is contained in:
flash 2020-06-05 12:38:28 +00:00
parent baaf3738f0
commit e6b69a23e8
2 changed files with 20 additions and 3 deletions

View file

@ -12,6 +12,7 @@ class BBCodeParser implements ParserInterface {
// Advanced markup
new Tags\CodeTag,
new Tags\QuoteTag,
new Tags\AlignTag,
// Slightly more advanced markup
new Tags\AudioTag,
@ -39,10 +40,8 @@ class BBCodeParser implements ParserInterface {
}
public function parseText(string $text): string {
foreach($this->tags as $tag) {
foreach($this->tags as $tag)
$text = $tag->parseText($text);
}
return $text;
}

View file

@ -0,0 +1,18 @@
<?php
namespace Misuzu\Parsers\BBCode\Tags;
use Misuzu\Parsers\BBCode\BBCodeTag;
final class AlignTag extends BBCodeTag {
public function parseText(string $text): string {
return preg_replace_callback(
'#\[align=(left|right|center|centre|justify)\](.+?)\[/align\]#',
function ($matches) {
if($matches[1] === 'centre')
$matches[1] = 'center';
return sprintf('<div style="text-align: %s;">%s</div>', $matches[1], $matches[2]);
},
$text
);
}
}