Skip to content

Commit

Permalink
fix: use raw language string if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Oct 13, 2024
1 parent dc80751 commit 90e14aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/Generators/HtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Phiki\Generators;

use Phiki\Contracts\OutputGeneratorInterface;
use Phiki\Grammar\ParsedGrammar;
use Phiki\Html\AttributeList;
use Phiki\Html\Code;
use Phiki\Html\Pre;
Expand All @@ -16,7 +15,7 @@
class HtmlGenerator implements OutputGeneratorInterface
{
public function __construct(
protected ParsedGrammar $grammar,
protected ?string $grammarName,
protected ParsedTheme $theme,
protected ProxyTransformer $proxy,
) {}
Expand Down Expand Up @@ -49,9 +48,9 @@ public function generate(array $tokens): string
$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->grammar->name ? ' language-'.$this->grammar->name : ''),
'class' => sprintf('phiki %s%s', $this->theme->name, $this->grammarName ? ' language-'.$this->grammarName : ''),
'style' => $this->theme->base()->toStyleString(),
'data-language' => $this->grammar->name,
'data-language' => $this->grammarName,
])));

$root = $this->proxy->root(new Root($pre));
Expand Down
9 changes: 8 additions & 1 deletion src/Phiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public function codeToTerminal(string $code, string|Grammar $grammar, string|The
public function codeToHtml(string $code, string|Grammar $grammar, string|Theme $theme): string
{
$tokens = $this->codeToHighlightedTokens($code, $grammar, $theme);
$generator = new HtmlGenerator($this->environment->resolveGrammar($grammar), $this->environment->resolveTheme($theme), $this->environment->getProxyTransformer());
$generator = new HtmlGenerator(
match (true) {
is_string($grammar) => $grammar,
default => $this->environment->resolveGrammar($grammar)->name,
},
$this->environment->resolveTheme($theme),
$this->environment->getProxyTransformer()
);

return $generator->generate($tokens);
}
Expand Down

0 comments on commit 90e14aa

Please sign in to comment.