-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
155 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight\Themes; | ||
|
||
use Tempest\Highlight\Theme; | ||
use Tempest\Highlight\Tokens\TokenType; | ||
use Tempest\Highlight\Tokens\TokenTypeEnum; | ||
use Tempest\Highlight\WithPre; | ||
|
||
final class InlineTheme implements Theme, WithPre | ||
{ | ||
private array $map = []; | ||
|
||
public function __construct(string $themePath) | ||
{ | ||
preg_match_all('/(?<selector>[\w,\.\s\-]+){(?<style>(.|\n)*?)}/', @file_get_contents($themePath) ?? '', $matches); | ||
|
||
foreach ($matches[0] as $key => $match) { | ||
$selector = trim($matches['selector'][$key]); | ||
$style = str_replace([PHP_EOL, ' ', "\t"], [' ', '', ''], trim($matches['style'][$key])); | ||
|
||
$this->map[$selector] = $style; | ||
} | ||
} | ||
|
||
public function before(TokenType $tokenType): string | ||
{ | ||
$class = match ($tokenType) { | ||
TokenTypeEnum::KEYWORD => 'hl-keyword', | ||
TokenTypeEnum::PROPERTY => 'hl-property', | ||
TokenTypeEnum::TYPE => 'hl-type', | ||
TokenTypeEnum::GENERIC => 'hl-generic', | ||
TokenTypeEnum::VALUE => 'hl-value', | ||
TokenTypeEnum::COMMENT => 'hl-comment', | ||
TokenTypeEnum::ATTRIBUTE => 'hl-attribute', | ||
TokenTypeEnum::INJECTION => 'hl-injection', | ||
TokenTypeEnum::VARIABLE => 'hl-variable', | ||
TokenTypeEnum::OPERATOR => 'hl-operator', | ||
default => $tokenType->getValue(), | ||
}; | ||
|
||
$style = $this->map[".{$class}"] ?? null; | ||
|
||
if (! $style) { | ||
return '<span>'; | ||
} | ||
|
||
return "<span style=\"{$style}\">"; | ||
} | ||
|
||
public function after(TokenType $tokenType): string | ||
{ | ||
return '</span>'; | ||
} | ||
|
||
public function preBefore(): string | ||
{ | ||
$preStyle = $this->map['pre'] ?? $this->map['pre, code'] ?? ''; | ||
|
||
return "<pre style=\"{$preStyle}\">"; | ||
} | ||
|
||
public function preAfter(): string | ||
{ | ||
return '</pre>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ pre, code { | |
} | ||
|
||
.hl-variable { | ||
color: #000; | ||
color: #839496; | ||
} | ||
|
||
.hl-comment { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Highlight; | ||
|
||
interface WithPre | ||
{ | ||
public function preBefore(): string; | ||
|
||
public function preAfter(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,12 @@ | ||
```php | ||
foreach ($lines as $i => $line) { | ||
$gutterNumber = $gutterNumbers[$i]; | ||
```php{1} | ||
$environment = new Environment(); | ||
$gutterClass = 'hl-gutter ' . ($this->classes[$i + 1] ?? ''); | ||
{+ | ||
$lines[$i] = sprintf( | ||
Escape::tokens('<span class="%s">%s</span>%s'),+} | ||
$gutterClass, | ||
str_pad( | ||
string: {-$gutterNumber-}, | ||
length: $gutterWidth, | ||
pad_type: STR_PAD_LEFT, | ||
), | ||
$line, | ||
); | ||
} | ||
$theme = new InlineTheme(__DIR__ . {+'/../src/Themes/solarized-dark.css'+})); | ||
$highlighter = (new Highlighter($theme))->withGutter(); | ||
$environment | ||
->addExtension(new CommonMarkCoreExtension()) | ||
->addRenderer(FencedCode::class, new CodeBlockRenderer($highlighter)) | ||
->addRenderer(Code::class, new InlineCodeBlockRenderer($highlighter)); | ||
``` |