Skip to content

Commit

Permalink
feat: add function to output registered languages
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Mar 1, 2024
1 parent 8212fa2 commit 6b8a593
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- Function to output registered languages

## [0.1.0] - 2024-01-28

Initial preview release
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ An addition to the highlighting of code this Twig extension provides additional
- [line numbers](#line-numbers)
- [emphasize lines](#emphasize-lines)
- [classes](#classes)
- [list of available languages](#list-of-available-languages)

> This package is in beta phase! You can use it already, but API might change.
Expand Down Expand Up @@ -170,3 +171,17 @@ Using both variants together results in the following HTML code:
```html
<pre class="some-default-class some-special-class another-special-class"><code class="hljs plaintext">some text</code></pre>
```

### List of available languages

Sometimes it can be useful to provide a list of available languages. The function `codehighlight_languages()`
is available to output such a list:

```twig
<ul>
{% for language in codehighlight_languages() %}
<li>{{ language }}</li>
{% endfor %}
</ul>
```

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 $registeredLanguages;
}
}
37 changes: 36 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,33 @@ 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' => <<<TEMPLATE
{% for language in codehighlight_languages() %}
{{ language }}
{% endfor %}
TEMPLATE,
]);
$twig = new Environment($loader, [
'debug' => true,
'cache' => false,
]);
$twig->addExtension($this->subject);

$template = $twig->load('index');

$actual = $template->render();
$actualAsArray = \explode("\n", $actual);

// Validate sorting
self::assertStringStartsWith('1c', $actualAsArray[0]);
self::assertStringEndsWith('zephir', $actualAsArray[\count($actualAsArray) - 2]);

// Validate random language is available
self::assertContains('php', $actualAsArray);
}
}

0 comments on commit 6b8a593

Please sign in to comment.