Skip to content

Commit

Permalink
Terminal escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 25, 2024
1 parent 94747e0 commit 75fbc9d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ Note that `pre` tag styling isn't included in this package.

```php
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Themes\TerminalTheme;
use Tempest\Highlight\Themes\LightTerminalTheme;

$highlighter = new Highlighter(new TerminalTheme());
$highlighter = new Highlighter(new LightTerminalTheme());

echo $highlighter->parse($code, 'php');
```
Expand Down
5 changes: 5 additions & 0 deletions src/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public static function injection(string $input): string
return self::INJECTION_TOKEN . $input . self::INJECTION_TOKEN;
}

public static function terminal(string $input): string
{
return str_replace(self::INJECTION_TOKEN, '', $input);
}

public static function html(string $input): string
{
return self::reverse(
Expand Down
10 changes: 6 additions & 4 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Tempest\Highlight\Languages\Xml\XmlLanguage;
use Tempest\Highlight\Languages\Yaml\YamlLanguage;
use Tempest\Highlight\Themes\CssTheme;
use Tempest\Highlight\Themes\TerminalTheme;
use Tempest\Highlight\Tokens\GroupTokens;
use Tempest\Highlight\Tokens\ParseTokens;
use Tempest\Highlight\Tokens\RenderTokens;
Expand Down Expand Up @@ -106,8 +105,11 @@ private function parseContent(string $content, Language $language): string

$output = (new RenderTokens($this->theme))($content, $groupedTokens);

return $this->shouldEscape
? Escape::html($output)
: $output;
// Determine proper escaping
return match(true) {
$this->theme instanceof TerminalTheme => Escape::terminal($output),
$this->shouldEscape => Escape::html($output),
default => $output,
};
}
}
9 changes: 9 additions & 0 deletions src/TerminalTheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight;

interface TerminalTheme extends Theme
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Tempest\Highlight\Themes;

use Tempest\Highlight\Theme;
use Tempest\Highlight\TerminalTheme;
use Tempest\Highlight\Tokens\TokenType;

final readonly class TerminalTheme implements Theme
class LightTerminalTheme implements TerminalTheme
{
public function before(string|TokenType $tokenType): string
{
Expand Down
6 changes: 3 additions & 3 deletions test-terminal.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Themes\TerminalTheme;
use Tempest\Highlight\Themes\LightTerminalTheme;

require_once __DIR__ . '/vendor/autoload.php';

$highlighter = new Highlighter(new TerminalTheme());
$highlighter = new Highlighter(new LightTerminalTheme());

$target = $argc > 1
? $argv[1]
Expand All @@ -25,4 +25,4 @@
$code,
);

echo PHP_EOL, html_entity_decode($highlighter->parse($code, $language)), PHP_EOL, PHP_EOL;
echo PHP_EOL, $highlighter->parse($code, $language), PHP_EOL, PHP_EOL;
4 changes: 2 additions & 2 deletions tests/Languages/Sql/SqlLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Themes\TerminalTheme;
use Tempest\Highlight\Themes\LightTerminalTheme;

class SqlLanguageTest extends TestCase
{
Expand Down Expand Up @@ -66,7 +66,7 @@ public static function data(): array

public function test_highlight_in_terminal()
{
$highlighter = new Highlighter(new TerminalTheme());
$highlighter = new Highlighter(new LightTerminalTheme());

$sql = <<<SQL
ALTER TABLE "packages" DROP COLUMN updated_at_orig;
Expand Down
22 changes: 22 additions & 0 deletions tests/Themes/LightTerminalThemeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Tests\Themes;

use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Escape;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Themes\LightTerminalTheme;

class LightTerminalThemeTest extends TestCase
{
public function test_injection_tokens_are_filtered(): void
{
$highlighter = new Highlighter(new LightTerminalTheme());

$parsed = $highlighter->parse('{~}): Foo {}~}', 'php');

$this->assertStringNotContainsString(Escape::INJECTION_TOKEN, $parsed);
}
}
4 changes: 2 additions & 2 deletions tests/Themes/TerminalThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Themes\TerminalTheme;
use Tempest\Highlight\Themes\LightTerminalTheme;

class TerminalThemeTest extends TestCase
{
Expand All @@ -16,7 +16,7 @@ public function test_terminal_theme()
public function before
TXT;

$highlighter = new Highlighter(new TerminalTheme());
$highlighter = new Highlighter(new LightTerminalTheme());

$output = $highlighter->parse($content, 'php');

Expand Down
5 changes: 4 additions & 1 deletion tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
```php
{~}): Foo {}~}
$sql = <<<SQL
SELECT * FROM t;
SQL;

```

0 comments on commit 75fbc9d

Please sign in to comment.