Skip to content

Commit

Permalink
2.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 22, 2024
1 parent 54a46d5 commit 37f22b0
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 22 deletions.
6 changes: 5 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 2.0.3
## 2.0.4

- Fix for overflowing highlight tags

## 2.0.3

- Fix for custom class overflows

Expand Down
16 changes: 12 additions & 4 deletions src/Languages/Base/IsHighlightInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Highlight\ParsedInjection;
use Tempest\Highlight\Tokens\DynamicTokenType;
use Tempest\Highlight\Tokens\Token;
use Tempest\Highlight\Tokens\TokenTypeEnum;

trait IsHighlightInjection
{
Expand All @@ -26,16 +27,23 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
$tokens = [];

foreach ($matches[0] as $key => $original) {
$startToken = $matches['start'][$key][0];
$endToken = $matches['end'][$key][0];
$tokens[] = new Token(
offset: (int) $matches['start'][$key][1],
value: $matches['start'][$key][0],
type: TokenTypeEnum::HIDDEN,
);

$tokens[] = new Token(
offset: (int) $matches['match'][$key][1] - strlen($startToken),
offset: (int) $matches['match'][$key][1],
value: $matches['match'][$key][0],
type: new DynamicTokenType($this->getClassname()),
);

$content = str_replace([$startToken, $endToken], '', $content);
$tokens[] = new Token(
offset: (int) $matches['end'][$key][1],
value: $matches['end'][$key][0],
type: TokenTypeEnum::HIDDEN,
);
}

return new ParsedInjection(
Expand Down
4 changes: 4 additions & 0 deletions src/Themes/CssTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

public function before(TokenType $tokenType): string
{
if ($tokenType === TokenTypeEnum::HIDDEN) {
return '<span style="display: none">';
}

$class = match ($tokenType) {
TokenTypeEnum::KEYWORD => 'hl-keyword',
TokenTypeEnum::PROPERTY => 'hl-property',
Expand Down
4 changes: 4 additions & 0 deletions src/Themes/InlineTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function __construct(string $themePath)

public function before(TokenType $tokenType): string
{
if ($tokenType === TokenTypeEnum::HIDDEN) {
return '<span style="display: none">';
}

$class = match ($tokenType) {
TokenTypeEnum::KEYWORD => 'hl-keyword',
TokenTypeEnum::PROPERTY => 'hl-property',
Expand Down
3 changes: 1 addition & 2 deletions src/Tokens/GroupTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
public function __invoke(array $tokens): array
{
// dump($tokens);
// Sort tokens in the right order
usort($tokens, function (Token $a, Token $b) {
if ($a->start === $b->start) {
Expand All @@ -26,7 +25,7 @@ public function __invoke(array $tokens): array
/** @var \Tempest\Highlight\Tokens\Token[] $groupedTokens */
$groupedTokens = [];

while($token = current($tokens)) {
while ($token = current($tokens)) {
$token = $token->cloneWithoutParent();

foreach ($tokens as $compareKey => $compareToken) {
Expand Down
1 change: 1 addition & 0 deletions src/Tokens/TokenTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum TokenTypeEnum: string implements TokenType
case COMMENT = 'comment';
case INJECTION = 'injection';
case OPERATOR = 'operator';
case HIDDEN = 'hidden';

public function getValue(): string
{
Expand Down
19 changes: 15 additions & 4 deletions tests/Languages/Base/Injections/BlurInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Languages\Base\Injections\BlurInjection;
use Tempest\Highlight\Tokens\TokenTypeEnum;

final class BlurInjectionTest extends TestCase
{
Expand All @@ -17,15 +18,25 @@ public function test_blur_injection()
TXT;

$expected = <<<TXT
class Foo
{~class Foo~}
TXT;

$parsed = (new BlurInjection())->parse($content, new Highlighter());

$this->assertSame($expected, $parsed->content);
$this->assertCount(1, $parsed->tokens);

$this->assertCount(3, $parsed->tokens);

$this->assertSame(0, $parsed->tokens[0]->start);
$this->assertSame(9, $parsed->tokens[0]->end);
$this->assertSame('hl-blur', $parsed->tokens[0]->type->getValue());
$this->assertSame(2, $parsed->tokens[0]->end);
$this->assertSame(TokenTypeEnum::HIDDEN, $parsed->tokens[0]->type);

$this->assertSame(2, $parsed->tokens[1]->start);
$this->assertSame(11, $parsed->tokens[1]->end);
$this->assertSame('hl-blur', $parsed->tokens[1]->type->getValue());

$this->assertSame(11, $parsed->tokens[2]->start);
$this->assertSame(13, $parsed->tokens[2]->end);
$this->assertSame(TokenTypeEnum::HIDDEN, $parsed->tokens[2]->type);
}
}
19 changes: 15 additions & 4 deletions tests/Languages/Base/Injections/EmphasizeInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Languages\Base\Injections\EmphasizeInjection;
use Tempest\Highlight\Tokens\TokenTypeEnum;

final class EmphasizeInjectionTest extends TestCase
{
Expand All @@ -17,15 +18,25 @@ public function test_emphasize_injection()
TXT;

$expected = <<<TXT
class Foo
{_class Foo_}
TXT;

$parsed = (new EmphasizeInjection())->parse($content, new Highlighter());

$this->assertSame($expected, $parsed->content);
$this->assertCount(1, $parsed->tokens);

$this->assertCount(3, $parsed->tokens);

$this->assertSame(0, $parsed->tokens[0]->start);
$this->assertSame(9, $parsed->tokens[0]->end);
$this->assertSame('hl-em', $parsed->tokens[0]->type->getValue());
$this->assertSame(2, $parsed->tokens[0]->end);
$this->assertSame(TokenTypeEnum::HIDDEN, $parsed->tokens[0]->type);

$this->assertSame(2, $parsed->tokens[1]->start);
$this->assertSame(11, $parsed->tokens[1]->end);
$this->assertSame('hl-em', $parsed->tokens[1]->type->getValue());

$this->assertSame(11, $parsed->tokens[2]->start);
$this->assertSame(13, $parsed->tokens[2]->end);
$this->assertSame(TokenTypeEnum::HIDDEN, $parsed->tokens[2]->type);
}
}
19 changes: 15 additions & 4 deletions tests/Languages/Base/Injections/StrongInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Languages\Base\Injections\StrongInjection;
use Tempest\Highlight\Tokens\TokenTypeEnum;

class StrongInjectionTest extends TestCase
{
Expand All @@ -17,15 +18,25 @@ public function test_strong_injection()
TXT;

$expected = <<<TXT
class Foo
{*class Foo*}
TXT;

$parsed = (new StrongInjection())->parse($content, new Highlighter());

$this->assertSame($expected, $parsed->content);
$this->assertCount(1, $parsed->tokens);

$this->assertCount(3, $parsed->tokens);

$this->assertSame(0, $parsed->tokens[0]->start);
$this->assertSame(9, $parsed->tokens[0]->end);
$this->assertSame('hl-strong', $parsed->tokens[0]->type->getValue());
$this->assertSame(2, $parsed->tokens[0]->end);
$this->assertSame(TokenTypeEnum::HIDDEN, $parsed->tokens[0]->type);

$this->assertSame(2, $parsed->tokens[1]->start);
$this->assertSame(11, $parsed->tokens[1]->end);
$this->assertSame('hl-strong', $parsed->tokens[1]->type->getValue());

$this->assertSame(11, $parsed->tokens[2]->start);
$this->assertSame(13, $parsed->tokens[2]->end);
$this->assertSame(TokenTypeEnum::HIDDEN, $parsed->tokens[2]->type);
}
}
4 changes: 2 additions & 2 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static function data(): array
['$foo = true;', '<span class="hl-variable">$foo</span> = <span class="hl-keyword">true</span>;'],
['$foo = false;', '<span class="hl-variable">$foo</span> = <span class="hl-keyword">false</span>;'],
['/** @var Foo $var */', '<span class="hl-comment">/** <span class="hl-value">@var</span> <span class="hl-type">Foo</span> <span class="hl-variable">$var</span> */</span>'],
['{~}): Foo {}~}', '<span class="hl-blur">}): <span class="hl-type">Foo</span> {}</span>'],
['{~class~} Foo {}', '<span class="hl-blur"><span class="hl-keyword">class</span></span> <span class="hl-type">Foo</span> {}'],
['{~}): Foo {}~}', '<span style="display: none">{~</span><span class="hl-blur">}): <span class="hl-type">Foo</span> {}</span><span style="display: none">~}</span>'],
['{~class~} Foo {}', '<span style="display: none">{~</span><span class="hl-blur">class</span><span style="display: none">~}</span> Foo {}'],
['#[ConsoleCommand()]', '<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">ConsoleCommand</span>()]</span></span>'],
['#[ConsoleCommand]', '<span class="hl-attribute">#[<span class="hl-type">ConsoleCommand</span>]</span>'],
['#[ConsoleCommand(foo: [])]', '<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">ConsoleCommand</span>(<span class="hl-property">foo</span>: [])]</span></span>'],
Expand Down
2 changes: 1 addition & 1 deletion tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```
{:hl-property:read:}({:hl-type:int:} $bytes): {:hl-type:string:}
{~class~} Foo {}
```

0 comments on commit 37f22b0

Please sign in to comment.