Skip to content

Commit

Permalink
feat(console): add support for printing hyperlinks (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi authored Dec 13, 2024
1 parent 4099b40 commit 6f457af
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Highlight\TempestConsoleLanguage\Injections;

use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Injection;
use Tempest\Highlight\ParsedInjection;
use function Tempest\Support\str;

final readonly class LinkInjection implements Injection
{
public function parse(string $content, Highlighter $highlighter): ParsedInjection
{
return new ParsedInjection(preg_replace_callback(
subject: $content,
pattern: '/(?<match>\<href=\"(?<href>.+)\"\>(?:(?!\<href).)*?\<\/href\>)/',
callback: function (array $matches) {
$match = $matches['match'];
$href = $matches['href'];

return str($match)
->replaceFirst("<href=\"{$href}\">", "\x1b]8;;{$href}\x1b\\")
->replaceLast('</href>', "\x1b]8;;\x1b\\")
->toString();
},
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\EmphasizeInjection;
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\H1Injection;
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\H2Injection;
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\LinkInjection;
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\StrongInjection;
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\UnderlineInjection;
use Tempest\Highlight\Language;
Expand All @@ -27,6 +28,7 @@ public function getAliases(): array
public function getInjections(): array
{
return [
new LinkInjection(),
new EmphasizeInjection(),
new StrongInjection(),
new UnderlineInjection(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Tests\TempestConsoleLanguage\Injections;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Tempest\Console\Highlight\TempestConsoleLanguage\Injections\LinkInjection;
use Tempest\Console\Highlight\TempestTerminalTheme;
use Tempest\Highlight\Highlighter;

/**
* @internal
*/
final class LinkInjectionTest extends TestCase
{
#[Test]
#[TestWith(['<href="https://tempestphp.com">Tempest</href>', "\e]8;;https://tempestphp.com\e\Tempest\e]8;;\e\\"])]
#[TestWith(['<href="http://example.com/path?param=value!@#$%^&*()_+-={}:<>?,./">My link</href>', "\e]8;;http://example.com/path?param=value!@#$%^&*()_+-={}:<>?,./\e\My link\e]8;;\e\\"])]
#[TestWith(['<href="tel:+1234567890">My link</href>', "\e]8;;tel:+1234567890\e\My link\e]8;;\e\\"])]
#[TestWith(['<href="mailto:[email protected]">My link</href>', "\e]8;;mailto:[email protected]\e\My link\e]8;;\e\\"])]
public function language(string $content, string $expected): void
{
$highlighter = new Highlighter(new TempestTerminalTheme());

$this->assertSame(
$expected,
(new LinkInjection())->parse($content, $highlighter)->content,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ final class TempestConsoleLanguageTest extends TestCase
#[TestWith(['<style="dim"><style="bg-dark-red fg-white">foo</style></style>', "\e[2m\e[41m\e[97mfoo\e[49m\e[39m\e[22m"])]
#[TestWith(['<style="fg-cyan">cyan</style>unstyled<style="bg-dark-red">dark red</style>', "\e[96mcyan\e[39munstyled\e[41mdark red\e[49m"])]
#[TestWith(['<style="dim"><style="fg-gray">dim-gray</style> just-gray</style>', "\e[2m\e[90mdim-gray\e[39m just-gray\e[22m"])]
#[TestWith(['<em>Tempest</em>', "\e[1m\e[4mTempest\e[22m\e[24m"])]
#[TestWith(['<href="https://tempestphp.com">Tempest</href>', "\e]8;;https://tempestphp.com\e\Tempest\e]8;;\e\\"])]
#[TestWith(['<em><href="https://tempestphp.com">Tempest</href></em>', "\e[1m\e[4m\e]8;;https://tempestphp.com\e\Tempest\e]8;;\e\\\e[22m\e[24m"])]
#[TestWith(['<style="fg-cyan"><href="https://tempestphp.com">Tempest</href></style>', "\e[96m\e]8;;https://tempestphp.com\e\Tempest\e]8;;\e\\\e[39m"])]
public function language(string $content, string $expected): void
{
$highlighter = new Highlighter(new TempestTerminalTheme());
Expand Down

0 comments on commit 6f457af

Please sign in to comment.