Render Comment and description as BBCode.

This commit is contained in:
Jan Böhmer 2019-03-13 20:14:19 +01:00
parent daad7ec60a
commit 3da6b03b23
7 changed files with 114 additions and 8 deletions

View file

@ -34,22 +34,28 @@ namespace App\Twig;
use App\Entity\DBElement;
use App\Services\EntityURLGenerator;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
class AppExtension extends AbstractExtension
{
protected $entityURLGenerator;
protected $cache;
public function __construct(EntityURLGenerator $entityURLGenerator)
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache)
{
$this->entityURLGenerator = $entityURLGenerator;
$this->cache = $cache;
}
public function getFilters()
{
return [
new TwigFilter('entityURL', [$this, 'generateEntityURL'])
new TwigFilter('entityURL', [$this, 'generateEntityURL']),
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']])
];
}
@ -67,4 +73,18 @@ class AppExtension extends AbstractExtension
throw new \InvalidArgumentException('method is not supported!');
}
public function parseBBCode(string $bbcode) : string
{
if($bbcode === '') return '';
$item = $this->cache->getItem('bbcode_' . md5($bbcode));
if(!$item->isHit()) {
$xml = TextFormatter::parse($bbcode);
$item->set(TextFormatter::render($xml));
$this->cache->save($item);
}
return $item->get();
}
}