Skip to content

Commit

Permalink
bc: remove transformer api
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Nov 2, 2024
1 parent ebd1544 commit c772015
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 280 deletions.
63 changes: 0 additions & 63 deletions src/Contracts/TransformerInterface.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Environment/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,19 @@
use Phiki\Contracts\ExtensionInterface;
use Phiki\Contracts\GrammarRepositoryInterface;
use Phiki\Contracts\ThemeRepositoryInterface;
use Phiki\Contracts\TransformerInterface;
use Phiki\Exceptions\EnvironmentException;
use Phiki\Extensions\DefaultExtension;
use Phiki\Grammar\Grammar;
use Phiki\Grammar\ParsedGrammar;
use Phiki\Theme\ParsedTheme;
use Phiki\Theme\Theme;
use Phiki\Transformers\ProxyTransformer;

class Environment
{
protected GrammarRepositoryInterface $grammarRepository;

protected ThemeRepositoryInterface $themeRepository;

/**
* @var TransformerInterface[]
*/
protected array $transformers = [];

protected ?ProxyTransformer $proxyTransformer = null;

protected bool $strictMode = false;

public function addExtension(ExtensionInterface $extension): static
Expand Down Expand Up @@ -55,35 +46,6 @@ public function isStrictModeEnabled(): bool
return $this->strictMode;
}

public function addTransformer(TransformerInterface $transformer): static
{
$this->transformers[] = $transformer;

return $this;
}

public function addTransformers(array $transformers): static
{
foreach ($transformers as $transformer) {
$this->addTransformer($transformer);
}

return $this;
}

/**
* @return TransformerInterface[]
*/
public function getTransformers(): array
{
return $this->transformers;
}

public function getProxyTransformer(): ProxyTransformer
{
return $this->proxyTransformer ??= new ProxyTransformer($this->transformers);
}

public function useGrammarRepository(GrammarRepositoryInterface $grammarRepository): static
{
$this->grammarRepository = $grammarRepository;
Expand Down
61 changes: 30 additions & 31 deletions src/Generators/HtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,48 @@ class HtmlGenerator implements OutputGeneratorInterface
public function __construct(
protected ?string $grammarName,
protected ParsedTheme $theme,
protected ProxyTransformer $proxy,
) {}

public function generate(array $tokens): string
{
$lines = [];
$html = [];

$html[] = sprintf(
'<div class="phiki-wrapper" style="%s"%s>',
$this->theme->base()->toStyleString(),
$this->grammarName ? sprintf(' data-language="%s"', $this->grammarName) : '',
);

$html[] = sprintf(
'<pre class="%s" style="%s"%s>',
sprintf('phiki %s%s', $this->theme->name, $this->grammarName ? ' language-' . $this->grammarName : ''),
$this->theme->base()->toStyleString(),
$this->grammarName ? sprintf(' data-language="%s"', $this->grammarName) : '',
);

$html[] = '<code>';

foreach ($tokens as $i => $line) {
$children = [];
$html[] = sprintf(
'<span class="line" data-line="%d">',
$i + 1,
);

foreach ($line as $token) {
$children[] = $this->proxy->token(new Span(
new AttributeList([
'class' => 'token',
'style' => $token->settings?->toStyleString(),
]),
[new Text($token->token->text)],
));
$html[] = sprintf(
'<span class="token" style="%s">%s</span>',
$token->settings?->toStyleString(),
htmlspecialchars($token->token->text),
);
}

$lines[] = $this->proxy->line(new Span(
new AttributeList([
'class' => 'line',
'data-line' => $i + 1,
]),
$children,
), $i + 1);
$html[] = '</span>';
}

$code = $this->proxy->code(new Code(children: $lines));

$pre = $this->proxy->pre(new Pre($code, new AttributeList([
'class' => sprintf('phiki %s%s', $this->theme->name, $this->grammarName ? ' language-'.$this->grammarName : ''),
'style' => $this->theme->base()->toStyleString(),
'data-language' => $this->grammarName,
])));

$root = $this->proxy->root(new Root($pre, new AttributeList([
'class' => 'phiki-wrapper',
'style' => $this->theme->base()->toStyleString(),
'data-language' => $this->grammarName,
])));
$html[] = '</code>';
$html[] = '</pre>';
$html[] = '</div>';

return $this->proxy->postprocess($root->__toString());
return implode('', $html);
}
}
4 changes: 1 addition & 3 deletions src/Phiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function __construct(?Environment $environment = null)

public function codeToTokens(string $code, string|Grammar $grammar): array
{
$code = $this->environment->getProxyTransformer()->preprocess($code);
$grammar = $this->environment->resolveGrammar($grammar);
$tokenizer = new Tokenizer($grammar, $this->environment);

Expand All @@ -33,7 +32,7 @@ public function codeToHighlightedTokens(string $code, string|Grammar $grammar, s
$theme = $this->environment->resolveTheme($theme);
$highlighter = new Highlighter($theme);

return $this->environment->getProxyTransformer()->tokens($highlighter->highlight($tokens));
return $highlighter->highlight($tokens);
}

public function codeToTerminal(string $code, string|Grammar $grammar, string|Theme $theme): string
Expand All @@ -53,7 +52,6 @@ public function codeToHtml(string $code, string|Grammar $grammar, string|Theme $
default => $this->environment->resolveGrammar($grammar)->name,
},
$this->environment->resolveTheme($theme),
$this->environment->getProxyTransformer()
);

return $generator->generate($tokens);
Expand Down
53 changes: 0 additions & 53 deletions src/Transformers/AbstractTransformer.php

This file was deleted.

92 changes: 0 additions & 92 deletions src/Transformers/ProxyTransformer.php

This file was deleted.

0 comments on commit c772015

Please sign in to comment.