-
Notifications
You must be signed in to change notification settings - Fork 13
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
112 changed files
with
4,343 additions
and
11 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
52 changes: 52 additions & 0 deletions
52
app/MarkDown/CustomHL/CommonMark/CustomHighlightExtension.php
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\CommonMark; | ||
|
||
use League\CommonMark\Environment\EnvironmentBuilderInterface; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode; | ||
use League\CommonMark\Extension\CommonMark\Node\Inline\Code; | ||
use League\CommonMark\Extension\ExtensionInterface; | ||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\Themes\InlineTheme; | ||
use Tempest\Highlight\CommonMark\CodeBlockRenderer; | ||
use Tempest\Highlight\CommonMark\InlineCodeBlockRenderer; | ||
use App\MarkDown\CustomHL\Languages\Php\PhpLanguage; | ||
use App\MarkDown\CustomHL\Languages\Shell\ShellLanguage; | ||
use App\MarkDown\CustomHL\Languages\Bash\BashLanguage; | ||
use App\MarkDown\CustomHL\Languages\Ini\IniLanguage; | ||
use App\MarkDown\CustomHL\Languages\Blade\BladeLanguage; | ||
use App\MarkDown\CustomHL\Languages\Vue\VueLanguage; | ||
use App\MarkDown\CustomHL\Languages\Nginx\NginxLanguage; | ||
use App\MarkDown\CustomHL\Languages\JavaScript\JavaScriptLanguage; | ||
//use App\MarkDown\CustomHL\Languages\Html\HtmlLanguage; | ||
use App\MarkDown\CustomHL\Languages\Xml\XmlLanguage; | ||
|
||
final class CustomHighlightExtension implements ExtensionInterface | ||
{ | ||
public function __construct( | ||
private ?Highlighter $highlighter = new Highlighter(new InlineTheme(__DIR__ . '/../style.css')), | ||
) { | ||
$this->highlighter->addLanguage(new PhpLanguage()); | ||
$this->highlighter->addLanguage(new ShellLanguage()); | ||
$this->highlighter->addLanguage(new BashLanguage()); | ||
$this->highlighter->addLanguage(new IniLanguage()); | ||
$this->highlighter->addLanguage(new BladeLanguage()); | ||
$this->highlighter->addLanguage(new VueLanguage()); | ||
$this->highlighter->addLanguage(new NginxLanguage()); | ||
$this->highlighter->addLanguage(new JavaScriptLanguage()); | ||
// $this->highlighter->addLanguage(new HtmlLanguage()); | ||
$this->highlighter->addLanguage(new XmlLanguage()); | ||
} | ||
|
||
public function register(EnvironmentBuilderInterface $environment): void | ||
{ | ||
$environment | ||
->addRenderer(FencedCode::class, new CodeBlockRenderer($this->highlighter), 10) | ||
->addRenderer(IndentedCode::class, new IndentedCodeBlockRenderer($this->highlighter), 10) | ||
//->addRenderer(Code::class, new InlineCodeBlockRenderer($this->highlighter), 10) | ||
; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/MarkDown/CustomHL/CommonMark/IndentedCodeBlockRenderer.php
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\CommonMark; | ||
|
||
use InvalidArgumentException; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode; | ||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\ChildNodeRendererInterface; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\WebTheme; | ||
|
||
final class IndentedCodeBlockRenderer implements NodeRendererInterface | ||
{ | ||
public function __construct( | ||
private Highlighter $highlighter = new Highlighter(), | ||
) { | ||
} | ||
|
||
public function render(Node $node, ChildNodeRendererInterface $childRenderer) | ||
{ | ||
//dd($node); //print_r($node); | ||
if (! $node instanceof IndentedCode) { | ||
//dd($node); | ||
throw new InvalidArgumentException('Block must be instance of ' . IndentedCode::class); | ||
} | ||
//dd($node); | ||
|
||
//preg_match('/^(?<language>[\w]+)(\{(?<startAt>[\d]+)\})?/', $node->getInfoWords()[0] ?? 'php', $matches); | ||
|
||
$highlighter = $this->highlighter; | ||
|
||
//if ($startAt = ($matches['startAt']) ?? null) { | ||
// $highlighter = $highlighter->withGutter((int)$startAt); | ||
//} | ||
|
||
//$language = $matches['language'] ?? 'php'; | ||
$language = 'php'; | ||
|
||
$parsed = $highlighter->parse($node->getLiteral(), $language); | ||
|
||
$theme = $highlighter->getTheme(); | ||
|
||
if ($theme instanceof WebTheme) { | ||
return $theme->preBefore($highlighter) . $parsed . $theme->preAfter($highlighter); | ||
} else { | ||
return '<pre data-lang="' . $language . '" class="notranslate">' . $parsed . '</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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL; | ||
|
||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\ParsedInjection; | ||
|
||
trait IsInjection | ||
{ | ||
abstract public function getPattern(): string; | ||
|
||
abstract public function parseContent(string $content, Highlighter $highlighter): string; | ||
|
||
private function clearContent(string $input):string | ||
{ | ||
return preg_replace( | ||
['/❿(.*?)❿/', '/❷span(.*?)span❸/'], | ||
'', | ||
$input, | ||
); | ||
} | ||
|
||
public function parse(string $content, Highlighter $highlighter): ParsedInjection | ||
{ | ||
$pattern = $this->getPattern(); | ||
|
||
if (! str_starts_with($pattern, '/')) { | ||
$pattern = "/{$pattern}/"; | ||
} | ||
|
||
$cc = $this->clearContent($content); | ||
$result = preg_replace_callback( | ||
pattern: $pattern, | ||
callback: function ($matches) use ($highlighter) { | ||
$content = $matches['match'] ?? ''; | ||
|
||
if (! $content) { | ||
return $matches[0]; | ||
} | ||
|
||
return str_replace( | ||
search: $content, | ||
replace: $this->parseContent($content, $highlighter), | ||
subject: $matches[0], | ||
); | ||
}, | ||
subject: $cc, //$content, | ||
); | ||
|
||
return new ParsedInjection($result ?? $content); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\Languages\Bash; | ||
|
||
use App\MarkDown\CustomHL\Languages\Shell\ShellLanguage; | ||
//use App\MarkDown\CustomHL\Languages\Bash\Patterns\BashKeyPattern; | ||
//use App\MarkDown\CustomHL\Languages\Bash\Patterns\DoubleQuoteValuePattern; | ||
|
||
class BashLanguage extends ShellLanguage | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'bash'; | ||
} | ||
|
||
public function getInjections(): array | ||
{ | ||
return [ | ||
...parent::getInjections(), | ||
]; | ||
} | ||
|
||
public function getPatterns(): array | ||
{ | ||
return [ | ||
...parent::getPatterns(), | ||
//new BashKeyPattern(), | ||
//new DoubleQuoteValuePattern(), | ||
]; | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\Languages\Blade; | ||
|
||
use App\MarkDown\CustomHL\Languages\Blade\Injections\BladeEchoInjection; | ||
//use App\Tempest\Highlight\Languages\Blade\Injections\BladeKeywordInjection; | ||
use App\MarkDown\CustomHL\Languages\Blade\Injections\BladePhpInjection; | ||
use App\MarkDown\CustomHL\Languages\Blade\Injections\BladeRawEchoInjection; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\BladeCommentPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\BladeComponentCloseTagPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\BladeComponentOpenTagPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\BladeKeywordPattern; | ||
use App\MarkDown\CustomHL\Languages\Html\HtmlLanguage; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\DelimeterPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\KeywordPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Injections\SingleQuoteValueInjection; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\GenericPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Patterns\EscapeSymbolPattern; | ||
use App\MarkDown\CustomHL\Languages\Blade\Injections\BladeKeywordParametersInjection; | ||
|
||
class BladeLanguage extends HtmlLanguage | ||
{ | ||
public function getName(): string | ||
{ | ||
return 'blade'; | ||
} | ||
|
||
public function getAliases(): array | ||
{ | ||
return ['html']; | ||
} | ||
|
||
public function getInjections(): array | ||
{ | ||
return [ | ||
...parent::getInjections(), | ||
//new BladeKeywordInjection(), | ||
new BladePhpInjection(), | ||
new BladeKeywordParametersInjection(), | ||
new BladeEchoInjection(), | ||
new BladeRawEchoInjection(), | ||
new SingleQuoteValueInjection(), | ||
]; | ||
} | ||
|
||
public function getPatterns(): array | ||
{ | ||
return [ | ||
...parent::getPatterns(), | ||
//new BladeComponentOpenTagPattern(), | ||
//new BladeComponentCloseTagPattern(), | ||
new BladeKeywordPattern(), | ||
new BladeCommentPattern(), | ||
|
||
new DelimeterPattern('({!!|!!}|=\>)'), // {{|}}| // /(?<match>({!!|!!}|\<[^!](\/)?|\<(?!\!\--)|\>|=\>))/ | ||
new GenericPattern('/(?<match>({{(?!\-\-)))/', 'hl-blade-delimeter'), // /(?<match>({{(?!\-\-)|[^\-]}}|{!!|!!}))/ | ||
new GenericPattern('/[^\-](?<match>(}}))/', 'hl-blade-delimeter'), // /(?<match>({{(?!\-\-)|[^\-]}}|{!!|!!}))/ | ||
new GenericPattern('/(?<match>(\<(\/)?))[^!]/', 'hl-blade-delimeter'), // /(?<match>({{(?!\-\-)|[^\-]}}|{!!|!!}))/ | ||
new GenericPattern('/[^\-](?<match>(\>))/', 'hl-blade-delimeter'), // /(?<match>({{(?!\-\-)|[^\-]}}|{!!|!!}))/ | ||
new EscapeSymbolPattern(), | ||
|
||
new KeywordPattern('as'), | ||
]; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
app/MarkDown/CustomHL/Languages/Blade/Injections/BladeEchoInjection.php
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\Languages\Blade\Injections; | ||
|
||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\Injection; | ||
use Tempest\Highlight\IsInjection; | ||
use Tempest\Highlight\Escape; | ||
use Tempest\Highlight\Tokens\DynamicTokenType; | ||
|
||
final readonly class BladeEchoInjection implements Injection | ||
{ | ||
use IsInjection; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '({{(?!--))(?<match>.*)(}})'; | ||
} | ||
|
||
public function parseContent(string $content, Highlighter $highlighter): string | ||
{ | ||
return $highlighter->parse($content, 'php'); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/MarkDown/CustomHL/Languages/Blade/Injections/BladeKeywordParametersInjection.php
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\Languages\Blade\Injections; | ||
|
||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\Injection; | ||
use Tempest\Highlight\IsInjection; | ||
|
||
final readonly class BladeKeywordParametersInjection implements Injection | ||
{ | ||
use IsInjection; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '/\@[\w]+\b(\s)*\((?<match>.*)\)/'; | ||
} | ||
|
||
public function parseContent(string $content, Highlighter $highlighter): string | ||
{ | ||
return $highlighter->parse($content, 'php'); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/MarkDown/CustomHL/Languages/Blade/Injections/BladePhpInjection.php
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\MarkDown\CustomHL\Languages\Blade\Injections; | ||
|
||
use Tempest\Highlight\Highlighter; | ||
use Tempest\Highlight\Injection; | ||
use Tempest\Highlight\IsInjection; | ||
|
||
final readonly class BladePhpInjection implements Injection | ||
{ | ||
use IsInjection; | ||
|
||
public function getPattern(): string | ||
{ | ||
return '/(?<match>(\@php|<\?php)(.|\n)*?(\@endphp|\?>))/'; | ||
} | ||
|
||
public function parseContent(string $content, Highlighter $highlighter): string | ||
{ | ||
return $highlighter->parse($content, 'php'); | ||
} | ||
} |
Oops, something went wrong.