Skip to content

Commit

Permalink
highlighter tmp commit 20241126
Browse files Browse the repository at this point in the history
  • Loading branch information
demn98 committed Nov 26, 2024
1 parent 145052d commit 49cdab6
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\MarkDown\CustomHL\Languages\JavaScript\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 JsSingleQuoteValue2Injection implements Injection
{
use IsInjection;

public function getPattern(): string
{
return "(?<match>`.*?`)";
}

public function parseContent(string $content, Highlighter $highlighter): string
{
$theme = $highlighter->getTheme();

$clear_content = Escape::terminal($content);

return Escape::injection(
Escape::tokens($theme->before(new DynamicTokenType('hl-js-value')))
. $clear_content
. Escape::tokens($theme->after(new DynamicTokenType('hl-js-value')))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\MarkDown\CustomHL\Languages\JavaScript\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 JsSingleQuoteValueInjection implements Injection
{
use IsInjection;

public function getPattern(): string
{
return "(?<match>'.*?')";
}

public function parseContent(string $content, Highlighter $highlighter): string
{
$theme = $highlighter->getTheme();

$clear_content = Escape::terminal($content);

return Escape::injection(
Escape::tokens($theme->before(new DynamicTokenType('hl-js-value')))
. $clear_content
. Escape::tokens($theme->after(new DynamicTokenType('hl-js-value')))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
//use App\Tempest\Highlight\Languages\JavaScript\Patterns\JsDoubleQuoteValuePattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\JsKeywordPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\JsMethodPattern;
//use App\Tempest\Highlight\Languages\JavaScript\Patterns\JsMultilineCommentPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\JsMultilineCommentPattern;
//use App\Tempest\Highlight\Languages\JavaScript\Patterns\JsNewObjectPattern;
//use App\Tempest\Highlight\Languages\JavaScript\Patterns\JsObjectPropertyPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\JsPropertyPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\JsSinglelineCommentPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\JsSingleQuoteValuePattern;
//use App\Tempest\Highlight\Languages\JavaScript\Patterns\JsStaticClassPattern;
//use App\Tempest\Highlight\Languages\JavaScript\Patterns\JsStaticPropertyPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\DigitsPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\ConstantNamePattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\OperatorPattern;
use App\MarkDown\CustomHL\Languages\JavaScript\Injections\JsSingleQuoteValueInjection;
use App\MarkDown\CustomHL\Languages\JavaScript\Injections\JsSingleQuoteValue2Injection;
use App\MarkDown\CustomHL\Languages\JavaScript\Patterns\FunctionEPattern;

class JavaScriptLanguage extends CustomBaseLanguage
{
Expand All @@ -42,6 +44,8 @@ public function getInjections(): array
return [
...parent::getInjections(),
//new JsDocInjection(),
new JsSingleQuoteValueInjection(),
new JsSingleQuoteValue2Injection(),
];
}

Expand All @@ -56,6 +60,7 @@ public function getPatterns(): array
new JsKeywordPattern('from'),
new JsKeywordPattern('import'),
new JsKeywordPattern('null', 'hl-js-slug'),
new JsKeywordPattern('this', 'hl-js-slug'),
new JsKeywordPattern('true', 'hl-js-slug'),
new JsKeywordPattern('var'),
//new JsKeywordPattern('set'),
Expand Down Expand Up @@ -111,7 +116,7 @@ public function getPatterns(): array
new OperatorPattern('(=|\?\?|===)'),

// COMMENTS
//new JsMultilineCommentPattern(),
new JsMultilineCommentPattern(),
new JsSinglelineCommentPattern(),

// TYPES
Expand All @@ -126,12 +131,12 @@ public function getPatterns(): array
//new JsStaticPropertyPattern(),

// VALUES
new JsSingleQuoteValuePattern(),
//new JsDoubleQuoteValuePattern(),

new ConstantNamePattern(),

new DigitsPattern(),
new FunctionEPattern(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
use Tempest\Highlight\Tokens\TokenType;
use Tempest\Highlight\Tokens\DynamicTokenType;

final readonly class JsSingleQuoteValuePattern implements Pattern
final readonly class FunctionEPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return "'(?<match>.*?)'";
return '\((?<match>[a-z]+)\)';
}

public function getTokenType(): TokenType
{
return new DynamicTokenType('hl-js-value');
return new DynamicTokenType('hl-js-constant');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\MarkDown\CustomHL\Languages\JavaScript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\Tokens\TokenType;
use Tempest\Highlight\Tokens\DynamicTokenType;

final readonly class JsMultilineCommentPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '/(?<match>\/\*(.|\n)*?\*\/)/m';
}

public function getTokenType(): TokenType
{
return new DynamicTokenType('hl-js-comment');
}
}
2 changes: 2 additions & 0 deletions app/MarkDown/CustomHL/Languages/Json/JsonLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\MarkDown\CustomHL\Languages\Json\Patterns\JsonDoubleQuoteValuePattern;
use App\MarkDown\CustomHL\Languages\Json\Patterns\JsonPropertyPattern;
use App\MarkDown\CustomHL\Languages\Json\Injections\JsonArrayInjection;
use App\MarkDown\CustomHL\Languages\Json\Patterns\DigitsValuePattern;

class JsonLanguage extends CustomBaseLanguage
{
Expand All @@ -34,6 +35,7 @@ public function getPatterns(): array
//new JsonAccoladesPattern(),
//new JsonArrayBracketsPattern(),
new JsonDoubleQuoteValuePattern(),
new DigitsValuePattern(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\MarkDown\CustomHL\Languages\Json\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\Tokens\TokenType;
use Tempest\Highlight\Tokens\DynamicTokenType;

final readonly class DigitsValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '/\:(\s)*(?<match>[0-9.]+)/';
}

public function getTokenType(): TokenType
{
return new DynamicTokenType('hl-js-number');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\Tokens\TokenType;
use Tempest\Highlight\Tokens\DynamicTokenType;
//use Tempest\Highlight\Tokens\DynamicTokenType;
use App\MarkDown\CustomHL\Tokens\QuotedValueTokenType;

final readonly class JsonDoubleQuoteValuePattern implements Pattern
{
Expand All @@ -20,6 +21,6 @@ public function getPattern(): string

public function getTokenType(): TokenType
{
return new DynamicTokenType('hl-json-value');
return new QuotedValueTokenType(); //DynamicTokenType('hl-json-value');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Tempest\Highlight\Tokens\DynamicTokenType;
use App\MarkDown\CustomHL\Languages\Php\PhpConst;

//use App\MarkDown\CustomHL\Languages\Php\PhpDocCommentParamTypes;

final readonly class PhpDocCommentReturnTypeInjection implements Injection
{
use IsInjection;
Expand All @@ -25,6 +27,28 @@ public function parseContent(string $content, Highlighter $highlighter): string
{
$keywords = PhpConst::SYS_KEYWORDS;

$types = preg_match_all('/(?!([\w]+)\\\\)(?<match>[\w]+)/', $content, $matches);

$theme = $highlighter->getTheme();

if (key_exists('match', $matches))
{
foreach($matches['match'] as $type)
{
$token = (in_array($type, $keywords)) ? 'hl-php-keyword' : 'hl-php-type';

$content = preg_replace(
'/\b' . $type . '[\b]*/',
Escape::tokens($theme->before(new DynamicTokenType($token)))
. $type
. Escape::tokens($theme->after(new DynamicTokenType($token))),
$content,
);
}
}


/*
$types = explode('|', trim($content));
$theme = $highlighter->getTheme();
Expand All @@ -34,7 +58,7 @@ public function parseContent(string $content, Highlighter $highlighter): string
if (key_exists('match', $matches))
{
$content = preg_replace(
'/\barray[\b]*/',
'/\barray[\b]* /',
Escape::tokens($theme->before(new DynamicTokenType('hl-php-keyword')))
. 'array'
. Escape::tokens($theme->after(new DynamicTokenType('hl-php-keyword'))),
Expand All @@ -56,7 +80,7 @@ public function parseContent(string $content, Highlighter $highlighter): string
$token = (in_array($tk, $keywords)) ? 'hl-php-keyword' : 'hl-php-type';
$content = preg_replace(
'/\b' . $tk . '[\b]*/',
'/\b' . $tk . '[\b]* /',
Escape::tokens($theme->before(new DynamicTokenType($token)))
. $tk
. Escape::tokens($theme->after(new DynamicTokenType($token))),
Expand All @@ -73,15 +97,15 @@ public function parseContent(string $content, Highlighter $highlighter): string
$token = (in_array($type, $keywords)) ? 'hl-php-keyword' : 'hl-php-type';
$content = preg_replace(
'/\b' . $type . '[\b]*/',
'/\b' . $type . '[\b] * /',
Escape::tokens($theme->before(new DynamicTokenType($token)))
. $type
. Escape::tokens($theme->after(new DynamicTokenType($token))),
$content,
);
}
}

*/
return $content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public function getPattern(): string
{
return "(?<match>'(.|\n)*?')";
return "(?<match>'(?!(s ))(.|\n)*?')";
}

public function parseContent(string $content, Highlighter $highlighter): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public function getPattern(): string
{
return '/[^\$][(^)| |>|:]?(?!\$)\b(?<match>[_\-a-z\w]+)\(/';
return '/[(^)| |>|:]?(?!\$)\b(?<match>[_\-a-z\w]+)\(/';
}

public function getTokenType(): TokenType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public function getPattern(): string
{
return '/use\s+(function(\s)+)?[\w\\\\]*\b(?<match>[\w]+)[;]*/';
return '/use\s+(function(\s)+)?[\w\\\\]*\b(?<match>[\w]+[, \w]*)[;]*/';
}

public function getTokenType(): TokenType
Expand Down
8 changes: 4 additions & 4 deletions app/MarkDown/CustomHL/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getPatterns(): array
new NamedArgumentPattern(),
////new OperatorPattern('&&'),
////new OperatorPattern('\|\|'),
new OperatorPattern('(!==|===|==|<|=>|>|=|\*|\+\+|&&|\?)'),
new OperatorPattern('(!==|===|==|<=>|<|=>|>|=|\*|\+\+|\+|&&|\?)'),
//new OperatorPattern('instanceof'),
////new OperatorPattern('\?'),
////new FunctionNamePattern(),
Expand All @@ -129,7 +129,7 @@ public function getPatterns(): array

// KEYWORDS
new KeywordPattern('null', 'hl-php-constant'),
new GenericPattern('/(?<match>\$this)(\-|\$|\,|\)|\;|\:|\s|\()/', 'hl-php-this'),
new GenericPattern('/(?<match>\$this)(\-|\$|\,|\)|\;|\:|\s|\(|\])/', 'hl-php-this'),
new GenericPattern('/\->(?<match>[\w]+?)\b[^\(]/', 'hl-php-delimeter'),
new GenericPattern('/\((?<match>(string))\)/', 'hl-php-keyword'),
////new KeywordPattern('parent'),
Expand Down Expand Up @@ -169,7 +169,7 @@ public function getPatterns(): array
////new KeywordPattern('exit'),
new KeywordPattern('extends'),
////new KeywordPattern('final'),
////new KeywordPattern('finally'),
new KeywordPattern('finally'),
new KeywordPattern('fn'),
////new KeywordPattern('for'),
new KeywordPattern('foreach'),
Expand Down Expand Up @@ -209,7 +209,7 @@ public function getPatterns(): array
new KeywordPattern('use'),
new KeywordPattern('validate', 'hl-php-constant'),
new KeywordPattern('value', 'hl-php-constant'),
////new KeywordPattern('while'),
new KeywordPattern('while'),
////new KeywordPattern('xor'),
new KeywordPattern('yield'),
////new KeywordPattern('yield from'),
Expand Down
Loading

0 comments on commit 49cdab6

Please sign in to comment.