<?php


namespace App\Twig;


use Kadet\Highlighter\Formatter\HtmlFormatter;
use Kadet\Highlighter\KeyLighter;
use Kadet\Highlighter\Language\Language;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use function Kadet\Highlighter\highlight;

class KeyLighterExtension extends AbstractExtension
{
    public function getFilters()
    {
        $formatter = new HtmlFormatter(['prefix' => 'kl-']);

        return [
            new TwigFilter('highlight', function($source, $language) use ($formatter) {
                if (!$language instanceof Language) {
                    $language = KeyLighter::get()->getLanguage($language);
                }

                return highlight($source, $language, $formatter);
            }, ['pre_escape' => 'html', 'is_safe' => ['html']])
        ];
    }
}
