Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Output/Html/PendingHtmlOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Phiki\Theme\ParsedTheme;
use Phiki\Token\HighlightedToken;
use Phiki\Token\Token;
use Phiki\Transformers\Decorations\DecorationsTransformer;
use Phiki\Transformers\Decorations\DecorationTransformer;
use Phiki\Transformers\Decorations\LineDecoration;
use Psr\SimpleCache\CacheInterface;
use Stringable;

Expand All @@ -28,6 +31,8 @@ class PendingHtmlOutput implements Stringable

protected array $transformers = [];

protected array $decorations = [];

protected int $startingLineNumber = 1;

/**
Expand Down Expand Up @@ -80,6 +85,17 @@ public function transformer(TransformerInterface $transformer): self
return $this;
}

public function decoration(LineDecoration ...$decorations): self
{
if (! Arr::any($this->transformers, fn (TransformerInterface $transformer) => $transformer instanceof DecorationTransformer)) {
$this->transformers[] = new DecorationTransformer($this->decorations);
}

$this->decorations = array_merge($this->decorations, $decorations);

return $this;
}

public function startingLine(int $lineNumber): self
{
$this->startingLineNumber = $lineNumber;
Expand Down
5 changes: 5 additions & 0 deletions src/Phast/ClassList.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function remove(string ...$class): self
return $this;
}

public function all(): array
{
return $this->classes;
}

public function __toString(): string
{
return implode(' ', array_filter($this->classes, fn (string $class) => trim($class) !== ''));
Expand Down
29 changes: 29 additions & 0 deletions src/Transformers/Decorations/DecorationTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Phiki\Transformers\Decorations;

use Phiki\Phast\Element;
use Phiki\Transformers\AbstractTransformer;

class DecorationTransformer extends AbstractTransformer
{
/**
* @param array<int, LineDecoration> $decorations
*/
public function __construct(
public array &$decorations,
) {}

public function line(Element $span, array $tokens, int $index): Element
{
foreach ($this->decorations as $decoration) {
if (! $decoration->appliesToLine($index)) {
continue;
}

$span->properties->get('class')->add(...$decoration->classes->all());
}

return $span;
}
}
21 changes: 21 additions & 0 deletions src/Transformers/Decorations/LineDecoration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Phiki\Transformers\Decorations;

use Phiki\Phast\ClassList;

class LineDecoration
{
/**
* @param int | array<int> $line
*/
public function __construct(
public int | array $line,
public ClassList $classes,
) {}

public function appliesToLine(int $line): bool
{
return $this->line === $line || (is_array($this->line) && $line >= $this->line[0] && $line <= $this->line[1]);
}
}
49 changes: 49 additions & 0 deletions tests/Unit/Transformers/Decorations/DecorationTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Phiki\Grammar\Grammar;
use Phiki\Phast\ClassList;
use Phiki\Phiki;
use Phiki\Theme\Theme;
use Phiki\Transformers\Decorations\LineDecoration;

it('can apply decorations to a line', function () {
$output = (new Phiki)
->codeToHtml(<<<'PHP'
echo "Hello, world!";
PHP, Grammar::Php, Theme::GithubLight)
->decoration(new LineDecoration(0, new ClassList(['test-class'])))
->toString();

expect($output)->toContain('<span class="line test-class">');
});

it('can apply decorations to multiple lines with multiple instances', function () {
$output = (new Phiki)
->codeToHtml(<<<'PHP'
echo "Hello, world!";
echo "Goodbye, world!";
PHP, Grammar::Php, Theme::GithubLight)
->decoration(
new LineDecoration(0, new ClassList(['first-line'])),
new LineDecoration(1, new ClassList(['second-line'])),
new LineDecoration(0, new ClassList(['also-first-line'])),
)
->toString();

expect($output)
->toContain('<span class="line first-line also-first-line">')
->toContain('<span class="line second-line">');
});

it('can apply decorations to a range of lines', function () {
$output = (new Phiki)
->codeToHtml(<<<'PHP'
echo "Hello, world!";
echo "Goodbye, world!";
echo "Farewell, world!";
PHP, Grammar::Php, Theme::GithubLight)
->decoration(new LineDecoration([0, 2], new ClassList(['multi-line'])))
->toString();

expect(substr_count($output, 'multi-line'))->toBe(3);
});