Skip to content

Commit

Permalink
feat: add new function to output registered languages as list
Browse files Browse the repository at this point in the history
to do:
- add tests
- add documentation
  • Loading branch information
brotkrueml committed Feb 20, 2024
1 parent 8212fa2 commit 189abdc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Psr\Log\NullLogger;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

/**
* @see \Brotkrueml\TwigCodeHighlight\Tests\ExtensionTest
Expand Down Expand Up @@ -49,6 +50,9 @@ public function __construct(
}
}

/**
* @return list<TwigFilter>
*/
public function getFilters(): array
{
return [
Expand All @@ -62,6 +66,19 @@ public function getFilters(): array
];
}

/**
* @return list<TwigFunction>
*/
public function getFunctions(): array
{
return [
new TwigFunction(
'codehighlight_languages',
$this->languages(...),
),
];
}

private function highlight(
string $code,
?string $language,
Expand Down Expand Up @@ -160,4 +177,15 @@ private function addEmphasizeLines(string $code): string

return \implode("\n", $newLines);
}

/**
* @return list<string>
*/
private function languages(): array
{
$registeredLanguages = Highlighter::listRegisteredLanguages();
\sort($registeredLanguages);

return \array_map(static fn(string $language): string => '* ' . $language, $registeredLanguages);
}
}
32 changes: 31 additions & 1 deletion tests/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ protected function setUp(): void
}

#[Test]
public function getNameReturnExtensionName(): void
public function registeredFiltersAreAvailable(): void
{
self::assertSame('codehighlight', $this->subject->getFilters()[0]->getName());
}

#[Test]
public function registeredFunctionsAreAvailable(): void
{
self::assertSame('codehighlight_languages', $this->subject->getFunctions()[0]->getName());
}

#[Test]
#[DataProvider('providerForHighlightThroughTwigTemplate')]
public function highlightThroughTwigTemplate(string $filterArguments, string $code, string $expected): void
Expand Down Expand Up @@ -322,4 +328,28 @@ public function instantiatedWithClassesAndClassesGivenViaFilter(): void

self::assertSame('<pre class="some-default-class some-special-class"><code class="hljs plaintext">some text</code></pre>', $template->render());
}

// #[Test]
// public function registeredLanguagesAreReturnedCorrectly(): void
// {
// $loader = new ArrayLoader([
// 'index' => '{{ codehighlight_languages() }}',
// ]);
// $twig = new Environment($loader, [
// 'debug' => true,
// 'cache' => false,
// ]);
// $twig->addExtension($this->subject);
//
// $template = $twig->load('index');
//
// // Validate sorting
// $actual = $template->render();
// self::assertStringStartsWith('* 1c', $actual);
// self::assertStringEndsWith('* zephir', $actual);
//
// // Validate random language is available and languages are separated by a line feed
// $actualAsArray = \explode("\n", $actual);
// self::assertContains('* php', $actualAsArray);
// }
}

0 comments on commit 189abdc

Please sign in to comment.