├── .github ├── FUNDING.yml ├── highlight.png ├── terminal.png ├── highlight-2.png ├── highlight-3.png └── highlight-4.png ├── .gitignore ├── tests ├── targets │ ├── test.md │ ├── python.md │ ├── ellison.md │ └── gdscript.md ├── stylesheets │ └── ellison.css ├── Themes │ ├── InlineThemeTest.php │ ├── LightTerminalThemeTest.php │ └── TerminalThemeTest.php ├── Languages │ ├── Php │ │ ├── Patterns │ │ │ ├── UsePatternTest.php │ │ │ ├── ExtendsPatternTest.php │ │ │ ├── FunctionCallPatternTest.php │ │ │ ├── NamespacePatternTest.php │ │ │ ├── FunctionNamePatternTest.php │ │ │ ├── SinglelineDocCommentPatternTest.php │ │ │ ├── ClassNamePatternTest.php │ │ │ ├── ConstantPropertyPatternTest.php │ │ │ ├── NestedFunctionCallPatternTest.php │ │ │ ├── MultilineSingleDocCommentPatternTest.php │ │ │ ├── KeywordPatternTest.php │ │ │ ├── PropertyAccessPatternTest.php │ │ │ └── NamedArgumentPatternTest.php │ │ └── Injections │ │ │ └── PhpShortEchoInjectionTest.php │ ├── Base │ │ └── Injections │ │ │ ├── AdditionInjectionTest.php │ │ │ └── DeletionInjectionTest.php │ ├── Blade │ │ ├── Injections │ │ │ ├── BladeEchoInjectionTest.php │ │ │ ├── BladeRawEchoInjectionTest.php │ │ │ ├── BladeKeywordInjectionTest.php │ │ │ └── BladePhpInjectionTest.php │ │ └── BladeLanguageTest.php │ ├── Css │ │ └── Injections │ │ │ └── CssAttributeInjectionTest.php │ ├── Twig │ │ ├── Injections │ │ │ └── TwigEchoInjectionTest.php │ │ └── Patterns │ │ │ ├── TwigKeywordPatternTest.php │ │ │ └── TwigTokenPatternTest.php │ ├── Text │ │ └── TextLanguageTest.php │ ├── Diff │ │ └── DiffLanguageTest.php │ ├── Xml │ │ └── XmlLanguageTest.php │ └── Json │ │ └── JsonLanguageTest.php ├── TokenTest.php ├── TestsInjections.php └── CommonMark │ └── HighlightExtensionTest.php ├── phpstan.neon.dist ├── test-server.php ├── src ├── TerminalTheme.php ├── Injection.php ├── Tokens │ ├── TokenType.php │ ├── IgnoreTokenType.php │ ├── DynamicTokenType.php │ └── TokenTypeEnum.php ├── After.php ├── Before.php ├── WebTheme.php ├── Pattern.php ├── Themes │ ├── EscapesWebTheme.php │ ├── EscapesTerminalTheme.php │ └── Css │ │ ├── ellison.css │ │ ├── highlight-dark-lite.css │ │ └── highlight-light-lite.css ├── Theme.php ├── PatternTest.php ├── Languages │ ├── Text │ │ └── TextLanguage.php │ ├── Base │ │ ├── Injections │ │ │ ├── BlurInjection.php │ │ │ ├── StrongInjection.php │ │ │ └── EmphasizeInjection.php │ │ └── Patterns │ │ │ ├── InjectionTokenPattern.php │ │ │ ├── AdditionEndTokenPattern.php │ │ │ ├── AdditionStartTokenPattern.php │ │ │ ├── DeletionEndTokenPattern.php │ │ │ └── DeletionStartTokenPattern.php │ ├── Php │ │ ├── Patterns │ │ │ ├── UsePattern.php │ │ │ ├── NamedArgumentPattern.php │ │ │ ├── NamespacePattern.php │ │ │ ├── PropertyAccessPattern.php │ │ │ ├── ConstantPropertyPattern.php │ │ │ ├── FunctionCallPattern.php │ │ │ ├── FunctionNamePattern.php │ │ │ ├── SinglelineCommentPattern.php │ │ │ ├── NestedFunctionCallPattern.php │ │ │ ├── MultilineSingleDocCommentPattern.php │ │ │ ├── PhpCloseTagPattern.php │ │ │ ├── VariablePattern.php │ │ │ ├── ExtendsPattern.php │ │ │ ├── ShortFunctionReferencePattern.php │ │ │ ├── ClassResolutionPattern.php │ │ │ ├── OperatorPattern.php │ │ │ ├── GenericPattern.php │ │ │ ├── PhpDocCommentTemplateTypePattern.php │ │ │ ├── DoubleQuoteValuePattern.php │ │ │ ├── UseFunctionNamePattern.php │ │ │ ├── UseFunctionPattern.php │ │ │ ├── PhpDocCommentGenericTypePattern.php │ │ │ ├── PhpOpenTagPattern.php │ │ │ ├── UseAsPattern.php │ │ │ ├── StaticPropertyPattern.php │ │ │ ├── ImplementsPattern.php │ │ │ ├── EnumBackedTypePattern.php │ │ │ ├── InstanceOfPattern.php │ │ │ ├── PhpDocCommentReturnTypePattern.php │ │ │ ├── PhpDocCommentParamTypePattern.php │ │ │ ├── StaticClassCallPattern.php │ │ │ ├── CatchTypePattern.php │ │ │ ├── GroupedTypePattern.php │ │ │ ├── PhpDocCommentReturnTypeSingleLinePattern.php │ │ │ ├── NewObjectPattern.php │ │ │ ├── UntypedClassPropertyPattern.php │ │ │ ├── PropertyHookSetParameterTypePattern.php │ │ │ ├── EnumCasePattern.php │ │ │ ├── ConstantTypesPattern.php │ │ │ ├── ClassNamePattern.php │ │ │ ├── PropertyHookGetPattern.php │ │ │ ├── PhpDocCommentVariablePattern.php │ │ │ ├── ConstantNamePattern.php │ │ │ ├── PropertyHookSetPattern.php │ │ │ ├── SingleQuoteValuePattern.php │ │ │ ├── PhpDocCommentVarTypePattern.php │ │ │ ├── AttributeTypePattern.php │ │ │ ├── KeywordPattern.php │ │ │ ├── TypeForVariablePattern.php │ │ │ ├── ReturnTypePattern.php │ │ │ ├── ClassPropertyPattern.php │ │ │ └── PhpAsymmetricPropertyPattern.php │ │ └── Injections │ │ │ ├── PhpDocCommentInjection.php │ │ │ ├── PhpFunctionParametersInjection.php │ │ │ ├── PhpAttributePlainInjection.php │ │ │ └── PhpAttributeInstanceInjection.php │ ├── Ellison │ │ └── EllisonLanguage.php │ ├── Python │ │ └── Patterns │ │ │ ├── PyCommentPattern.php │ │ │ ├── PyArgumentPattern.php │ │ │ ├── PyLogicalValuePattern.php │ │ │ ├── PyTripleDoubleQuoteStringPattern.php │ │ │ ├── PyTripleSingleQuoteStringPattern.php │ │ │ ├── PyOperatorPattern.php │ │ │ ├── PyFunctionPattern.php │ │ │ ├── PyNumberPattern.php │ │ │ ├── PyClassNamePattern.php │ │ │ ├── PyDecoratorPattern.php │ │ │ └── PyKeywordPattern.php │ ├── Blade │ │ ├── Injections │ │ │ ├── BladeEchoInjection.php │ │ │ ├── BladePhpInjection.php │ │ │ ├── BladeRawEchoInjection.php │ │ │ └── BladeKeywordInjection.php │ │ └── Patterns │ │ │ ├── BladeKeywordPattern.php │ │ │ ├── BladeCommentPattern.php │ │ │ ├── BladeComponentCloseTagPattern.php │ │ │ └── BladeComponentOpenTagPattern.php │ ├── Html │ │ ├── Injections │ │ │ ├── PhpInHtmlInjection.php │ │ │ ├── CssInHtmlInjection.php │ │ │ ├── PhpShortEchoInHtmlInjection.php │ │ │ ├── CssAttributeInHtmlInjection.php │ │ │ └── JavaScriptInHtmlInjection.php │ │ └── HtmlLanguage.php │ ├── Twig │ │ ├── Patterns │ │ │ ├── TwigKeywordPattern.php │ │ │ ├── TwigMethodPattern.php │ │ │ ├── TwigPropertyPattern.php │ │ │ ├── TwigFilterPattern.php │ │ │ ├── TwigNamedArgumentPattern.php │ │ │ ├── TwigArrayKeyPattern.php │ │ │ ├── TwigDoubleQuoteValuePattern.php │ │ │ ├── TwigSingleQuoteValuePattern.php │ │ │ ├── TwigTokenPattern.php │ │ │ └── TwigCommentPattern.php │ │ ├── Injections │ │ │ ├── TwigEchoInjection.php │ │ │ └── TwigTagInjection.php │ │ └── TwigLanguage.php │ ├── Css │ │ └── Patterns │ │ │ ├── CssImportPattern.php │ │ │ ├── CssVariablePattern.php │ │ │ ├── CssCommentPattern.php │ │ │ ├── CssAttributePattern.php │ │ │ ├── CssFunctionPattern.php │ │ │ ├── CssMediaQueryPattern.php │ │ │ └── CssSelectorPattern.php │ ├── Gdscript │ │ └── Patterns │ │ │ ├── ExtendsPattern.php │ │ │ ├── KeywordPattern.php │ │ │ ├── AsTypePattern.php │ │ │ ├── ClassNamePattern.php │ │ │ ├── FunctionNamePattern.php │ │ │ ├── SinglelineCommentPattern.php │ │ │ ├── PropertyAccessPattern.php │ │ │ ├── DoubleQuoteValuePattern.php │ │ │ ├── SingleQuoteValuePattern.php │ │ │ ├── FunctionCallPattern.php │ │ │ ├── OperatorPattern.php │ │ │ ├── ReturnTypePattern.php │ │ │ └── VarTypePattern.php │ ├── Yaml │ │ └── Patterns │ │ │ ├── YamlCommentPattern.php │ │ │ ├── YamlDoubleQuoteValuePattern.php │ │ │ ├── YamlSingleQuoteValuePattern.php │ │ │ ├── YamlDashPattern.php │ │ │ ├── YamlArrayBracketsPattern.php │ │ │ ├── YamlPipePattern.php │ │ │ ├── YamlVariablePattern.php │ │ │ ├── YamlDoubleAccoladesValuePattern.php │ │ │ ├── YamlObjectBracketsPattern.php │ │ │ ├── YamlPropertyPattern.php │ │ │ └── YamlColonPattern.php │ ├── DocComment │ │ ├── Patterns │ │ │ └── DocCommentTagPattern.php │ │ └── DocCommentLanguage.php │ ├── JavaScript │ │ ├── Patterns │ │ │ ├── JsMethodPattern.php │ │ │ ├── JsPropertyPattern.php │ │ │ ├── JsClassNamePattern.php │ │ │ ├── JsStaticPropertyPattern.php │ │ │ ├── JsSinglelineCommentPattern.php │ │ │ ├── JsObjectPropertyPattern.php │ │ │ ├── JsDocTypePattern.php │ │ │ ├── JsDoubleQuoteValuePattern.php │ │ │ ├── JsSingleQuoteValuePattern.php │ │ │ ├── JsStaticClassPattern.php │ │ │ ├── JsMultilineCommentPattern.php │ │ │ ├── JsNewObjectPattern.php │ │ │ ├── JsDocParamNamePattern.php │ │ │ └── JsKeywordPattern.php │ │ ├── Injections │ │ │ └── JsDocInjection.php │ │ └── JsDocLanguage.php │ ├── Json │ │ ├── Patterns │ │ │ ├── JsonAccoladesPattern.php │ │ │ ├── JsonDoubleQuoteValuePattern.php │ │ │ ├── JsonPropertyPattern.php │ │ │ └── JsonArrayBracketsPattern.php │ │ └── JsonLanguage.php │ ├── Sql │ │ └── Patterns │ │ │ ├── SqlDoubleQuoteValuePattern.php │ │ │ ├── SqlSingleQuoteValuePattern.php │ │ │ ├── SqlTablePropertyPattern.php │ │ │ ├── SqlTableAccessPattern.php │ │ │ ├── SqlPropertyPattern.php │ │ │ ├── SqlSinglelineCommentPattern.php │ │ │ ├── SqlFunctionPattern.php │ │ │ ├── SqlFromTablePattern.php │ │ │ ├── SqlAsTablePattern.php │ │ │ ├── SqlJoinTablePattern.php │ │ │ └── SqlMultilineCommentPattern.php │ ├── Diff │ │ ├── DiffLanguage.php │ │ └── Injections │ │ │ ├── DiffAdditionInjection.php │ │ │ └── DiffDeletionInjection.php │ ├── Dockerfile │ │ └── Patterns │ │ │ ├── KeywordPattern.php │ │ │ ├── ImageAliasKeywordPattern.php │ │ │ ├── ImageAliasNamePattern.php │ │ │ ├── ImageNamePattern.php │ │ │ └── ImageTagPattern.php │ └── Xml │ │ ├── Patterns │ │ ├── XmlCloseTagPattern.php │ │ ├── XmlOpenTagPattern.php │ │ ├── XmlAttributePattern.php │ │ └── XmlCommentPattern.php │ │ └── XmlLanguage.php ├── Language.php ├── IsPattern.php ├── ParsedInjection.php ├── CommonMark │ ├── HighlightExtension.php │ └── InlineCodeBlockRenderer.php └── IsInjection.php ├── README.md ├── test-terminal.php ├── phpunit.xml └── LICENCE.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: brendt 4 | -------------------------------------------------------------------------------- /.github/highlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yilanboy/highlight/main/.github/highlight.png -------------------------------------------------------------------------------- /.github/terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yilanboy/highlight/main/.github/terminal.png -------------------------------------------------------------------------------- /.github/highlight-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yilanboy/highlight/main/.github/highlight-2.png -------------------------------------------------------------------------------- /.github/highlight-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yilanboy/highlight/main/.github/highlight-3.png -------------------------------------------------------------------------------- /.github/highlight-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yilanboy/highlight/main/.github/highlight-4.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | .phpunit.result.cache 3 | composer.lock 4 | /.php-cs-fixer.cache 5 | .env 6 | .idea 7 | build/ -------------------------------------------------------------------------------- /tests/targets/test.md: -------------------------------------------------------------------------------- 1 | ```php 2 | public function __construct( 3 | private(set) Author $author, 4 | ) {} 5 | ``` -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | parameters: 2 | level: 1 3 | reportUnmatchedIgnoredErrors: false 4 | paths: 5 | - src 6 | - tests -------------------------------------------------------------------------------- /test-server.php: -------------------------------------------------------------------------------- 1 | expectException(Exception::class); 16 | 17 | new InlineTheme('invalid/path.css'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Language.php: -------------------------------------------------------------------------------- 1 | value; 16 | } 17 | 18 | public function canContain(TokenType $other): bool 19 | { 20 | return $this->getValue() !== $other->getValue(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/IsPattern.php: -------------------------------------------------------------------------------- 1 | getPattern(); 14 | 15 | if (! str_starts_with($pattern, '/')) { 16 | $pattern = "/$pattern/"; 17 | } 18 | 19 | preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE); 20 | 21 | return $matches; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Languages/Base/Injections/BlurInjection.php: -------------------------------------------------------------------------------- 1 | [\w\\\\]+)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::TYPE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Ellison/EllisonLanguage.php: -------------------------------------------------------------------------------- 1 | [\w]+):\s'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::PROPERTY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/NamespacePattern.php: -------------------------------------------------------------------------------- 1 | [\w\\\\]+)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::TYPE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PropertyAccessPattern.php: -------------------------------------------------------------------------------- 1 | (?[\w]+)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::PROPERTY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyCommentPattern.php: -------------------------------------------------------------------------------- 1 | #.*)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::COMMENT; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ConstantPropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::PROPERTY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/FunctionCallPattern.php: -------------------------------------------------------------------------------- 1 | [a-z][\w]+)\('; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::PROPERTY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/FunctionNamePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::PROPERTY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/SinglelineCommentPattern.php: -------------------------------------------------------------------------------- 1 | \/\/(.)*)'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::COMMENT; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fast, extensible, server-side code highlighting 2 | [![Coverage Status](https://coveralls.io/repos/github/tempestphp/highlight/badge.svg?branch=main)](https://coveralls.io/github/tempestphp/highlight?branch=main) 3 | 4 | ## Quickstart 5 | 6 | ```php 7 | composer require tempest/highlight 8 | ``` 9 | 10 | Highlight code like this: 11 | 12 | ```php 13 | $highlighter = new \Tempest\Highlight\Highlighter(); 14 | 15 | $code = $highlighter->parse($code, 'php'); 16 | ``` 17 | 18 | Continue reading in the docs: [https://tempestphp.com/docs/highlight/01-getting-started](https://tempestphp.com/docs/highlight/01-getting-started). 19 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyArgumentPattern.php: -------------------------------------------------------------------------------- 1 | \w+)s*='; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::VARIABLE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/NestedFunctionCallPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\('; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::PROPERTY; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/UsePatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new UsePattern(), 19 | content: 'use Foo\Bar\Baz;', 20 | expected: 'Foo\Bar\Baz', 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/targets/ellison.md: -------------------------------------------------------------------------------- 1 | ```ellison 2 | The app highlights lengthy, complex sentences and common errors; if you see a yellow sentence, shorten or split it. If you see a red highlight, your sentence is so dense and complicated that your readers will get lost trying to follow its meandering, splitting logic — try editing this sentence to remove the red. 3 | 4 | You can utilize a shorter word in place of a purple one. Click on highlights to fix them. 5 | 6 | Adverbs and weakening phrases are helpfully shown in blue. Get rid of them and pick words with force, perhaps. 7 | 8 | Phrases in green have been marked to show passive voice. 9 | ``` 10 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyLogicalValuePattern.php: -------------------------------------------------------------------------------- 1 | (?:False|None|True))\b'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::LITERAL; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/ExtendsPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new ExtendsPattern(), 19 | content: 'class Foo extends Bar', 20 | expected: 'Bar', 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/FunctionCallPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new FunctionCallPattern(), 19 | content: 'foo()', 20 | expected: 'foo', 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/MultilineSingleDocCommentPattern.php: -------------------------------------------------------------------------------- 1 | \/\*(.|\n)*?\*\/)/m'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::COMMENT; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyTripleDoubleQuoteStringPattern.php: -------------------------------------------------------------------------------- 1 | """(.|\n)*?""")/m'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::VALUE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/NamespacePatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new NamespacePattern(), 19 | content: 'namespace Foo\Bar', 20 | expected: 'Foo\Bar', 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyTripleSingleQuoteStringPattern.php: -------------------------------------------------------------------------------- 1 | \'\'\'(.|\n)*?\'\'\')/m'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::VALUE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/FunctionNamePatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new FunctionNamePattern(), 19 | content: 'function foo()', 20 | expected: 'foo', 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Languages/Blade/Injections/BladeEchoInjection.php: -------------------------------------------------------------------------------- 1 | .*)(}})'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'php'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Html/Injections/PhpInHtmlInjection.php: -------------------------------------------------------------------------------- 1 | (.|\n)*?)\?>'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'php'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Blade/Injections/BladePhpInjection.php: -------------------------------------------------------------------------------- 1 | (.|\n)*?)\@endphp'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'php'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Blade/Injections/BladeRawEchoInjection.php: -------------------------------------------------------------------------------- 1 | .*)(!!})'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'php'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyOperatorPattern.php: -------------------------------------------------------------------------------- 1 | ([-+&%=]=?|!=|:=|>>=|<<=|\|=|\^=|\*\*?=?|\/\/?=?|<[<=]?|>[=>]?|[\|^~]))"; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::OPERATOR; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Blade/Injections/BladeKeywordInjection.php: -------------------------------------------------------------------------------- 1 | .*)\)'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'php'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Html/Injections/CssInHtmlInjection.php: -------------------------------------------------------------------------------- 1 | (?(.|\n)*?)<\/style>'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'css'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/SinglelineDocCommentPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new SinglelineCommentPattern(), 19 | content: '$bar // foo', 20 | expected: '// foo', 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/Themes/LightTerminalThemeTest.php: -------------------------------------------------------------------------------- 1 | parse('{~}): Foo {}~}', 'php'); 19 | 20 | $this->assertStringNotContainsString(Escape::INJECTION_TOKEN, $parsed); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Languages/Html/Injections/PhpShortEchoInHtmlInjection.php: -------------------------------------------------------------------------------- 1 | .*)\s\?>/'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'php'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Html/Injections/CssAttributeInHtmlInjection.php: -------------------------------------------------------------------------------- 1 | (.|\n)*?)"'; 18 | } 19 | 20 | public function parseContent(string $content, Highlighter $highlighter): string 21 | { 22 | return $highlighter->parse($content, 'css'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpCloseTagPattern.php: -------------------------------------------------------------------------------- 1 | ', output: '?>')] 13 | final readonly class PhpCloseTagPattern implements Pattern 14 | { 15 | use IsPattern; 16 | 17 | public function getPattern(): string 18 | { 19 | return '(?\?\>+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::KEYWORD; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/VariablePattern.php: -------------------------------------------------------------------------------- 1 | \\$[\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VARIABLE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ExtendsPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Base/Patterns/InjectionTokenPattern.php: -------------------------------------------------------------------------------- 1 | (.|\n)*?)' . Escape::INJECTION_TOKEN; 19 | } 20 | 21 | public function getTokenType(): TokenTypeEnum 22 | { 23 | return TokenTypeEnum::INJECTION; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigKeywordPattern.php: -------------------------------------------------------------------------------- 1 | {$this->keyword})\b"; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::KEYWORD; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssImportPattern.php: -------------------------------------------------------------------------------- 1 | \@import)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::KEYWORD; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/ExtendsPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ShortFunctionReferencePattern.php: -------------------------------------------------------------------------------- 1 | ', output: 'fn')] 13 | final class ShortFunctionReferencePattern implements Pattern 14 | { 15 | use IsPattern; 16 | 17 | public function getPattern(): string 18 | { 19 | return "(?fn)\&"; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::KEYWORD; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigMethodPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\('; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigPropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlCommentPattern.php: -------------------------------------------------------------------------------- 1 | \#(.)*)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::COMMENT; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/KeywordPattern.php: -------------------------------------------------------------------------------- 1 | {$this->keyword})\b/"; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::KEYWORD; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ClassResolutionPattern.php: -------------------------------------------------------------------------------- 1 | class)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::KEYWORD; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/OperatorPattern.php: -------------------------------------------------------------------------------- 1 | {$this->operator})(\s|\()/"; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::OPERATOR; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigFilterPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/DocComment/Patterns/DocCommentTagPattern.php: -------------------------------------------------------------------------------- 1 | \@[\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/AsTypePattern.php: -------------------------------------------------------------------------------- 1 | [a-zA-Z][a-zA-Z0-9]+)\b'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/ClassNamePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/FunctionNamePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/SinglelineCommentPattern.php: -------------------------------------------------------------------------------- 1 | #(.)*)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::COMMENT; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsMethodPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\('; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsPropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Json/Patterns/JsonAccoladesPattern.php: -------------------------------------------------------------------------------- 1 | (\{|\}))'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Json/Patterns/JsonDoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | \".*?\")'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlDoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)\"'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlSingleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)\''; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigNamedArgumentPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\='; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlDoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)\"'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlSingleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)\''; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/PropertyAccessPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Json/Patterns/JsonPropertyPattern.php: -------------------------------------------------------------------------------- 1 | \"[\w\\\-]+\")(\s)*\:'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::KEYWORD; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlTablePropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlDashPattern.php: -------------------------------------------------------------------------------- 1 | -)/m'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Diff/DiffLanguage.php: -------------------------------------------------------------------------------- 1 | {$this->keyword})[\s].*/m"; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::KEYWORD; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsClassNamePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Json/Patterns/JsonArrayBracketsPattern.php: -------------------------------------------------------------------------------- 1 | (\[|\]))'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlTableAccessPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\.'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlArrayBracketsPattern.php: -------------------------------------------------------------------------------- 1 | (\[|\]))'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlPipePattern.php: -------------------------------------------------------------------------------- 1 | \|)/m'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsStaticPropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyFunctionPattern.php: -------------------------------------------------------------------------------- 1 | \w*)(?=\s*\()'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlPropertyPattern.php: -------------------------------------------------------------------------------- 1 | (\w)+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/GenericPattern.php: -------------------------------------------------------------------------------- 1 | pattern; 24 | } 25 | 26 | public function getTokenType(): TokenTypeEnum 27 | { 28 | return $this->tokenType; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsSinglelineCommentPattern.php: -------------------------------------------------------------------------------- 1 | \/\/(.)*)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::COMMENT; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpDocCommentTemplateTypePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::GENERIC; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigArrayKeyPattern.php: -------------------------------------------------------------------------------- 1 | [\w\-\'\"]+)\:'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlVariablePattern.php: -------------------------------------------------------------------------------- 1 | [\w\s\.]+)\}\}'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/Themes/TerminalThemeTest.php: -------------------------------------------------------------------------------- 1 | parse($content, 'php'); 22 | 23 | $this->assertSame( 24 | "\e[34mpublic\e[0m \e[34mfunction\e[0m \e[32mbefore\e[0m", 25 | $output, 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/DoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)"'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/DoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | "(\\\"|.)*?")'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/UseFunctionNamePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+);'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/UseFunctionPattern.php: -------------------------------------------------------------------------------- 1 | [\w\\\\]+\\\\)[\w]+\;'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigDoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)"'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Base/Patterns/AdditionEndTokenPattern.php: -------------------------------------------------------------------------------- 1 | \+})/'; 21 | } 22 | 23 | public function getTokenType(): TokenType 24 | { 25 | return new IgnoreTokenType(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/SingleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)'"; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Injections/JsDocInjection.php: -------------------------------------------------------------------------------- 1 | \/\*\*(.|\n)*?\*\/)/m'; 19 | } 20 | 21 | public function parseContent(string $content, Highlighter $highlighter): string 22 | { 23 | return $highlighter->parse($content, new JsDocLanguage()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/JsDocLanguage.php: -------------------------------------------------------------------------------- 1 | $className', output: 'T')] 13 | final readonly class PhpDocCommentGenericTypePattern implements Pattern 14 | { 15 | use IsPattern; 16 | 17 | public function getPattern(): string 18 | { 19 | return '\<(?[\w]+)\>'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::GENERIC; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpOpenTagPattern.php: -------------------------------------------------------------------------------- 1 | \<\?[=|php]+)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::KEYWORD; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlSinglelineCommentPattern.php: -------------------------------------------------------------------------------- 1 | \-\-(.)*)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::COMMENT; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigSingleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | .*?)'"; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlDoubleAccoladesValuePattern.php: -------------------------------------------------------------------------------- 1 | (\{\{|\}\}))'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::VALUE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Base/Patterns/AdditionStartTokenPattern.php: -------------------------------------------------------------------------------- 1 | {\+)/'; 21 | } 22 | 23 | public function getTokenType(): TokenType 24 | { 25 | return new IgnoreTokenType(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/UseAsPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::TYPE; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyNumberPattern.php: -------------------------------------------------------------------------------- 1 | \b0(?:[bB](?:_?[01])+|[oO](?:_?[0-7])+|[xX](?:_?[a-fA-F0-9])+)\b|(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:[eE][+-]?\d+(?:_\d+)*)?j?(?!\w))'; 18 | } 19 | 20 | public function getTokenType(): TokenTypeEnum 21 | { 22 | return TokenTypeEnum::NUMBER; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigTokenPattern.php: -------------------------------------------------------------------------------- 1 | {$this->regex})/"; 24 | } 25 | 26 | public function getTokenType(): TokenTypeEnum 27 | { 28 | return $this->type; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Languages/Xml/Patterns/XmlCloseTagPattern.php: -------------------------------------------------------------------------------- 1 | ', output: 'x-hello')] 13 | #[PatternTest(input: '', output: 'a')] 14 | final readonly class XmlCloseTagPattern implements Pattern 15 | { 16 | use IsPattern; 17 | 18 | public function getPattern(): string 19 | { 20 | return '<\/(?[\w\-]+)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::KEYWORD; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Blade/Patterns/BladeKeywordPattern.php: -------------------------------------------------------------------------------- 1 | \@[\w]+)\b'; 24 | } 25 | 26 | public function getTokenType(): TokenTypeEnum 27 | { 28 | return TokenTypeEnum::KEYWORD; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsObjectPropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\:'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::PROPERTY; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Injections/PhpDocCommentInjection.php: -------------------------------------------------------------------------------- 1 | \/\*\*(.|\n)*?\*\/)/m'; 19 | } 20 | 21 | public function parseContent(string $content, Highlighter $highlighter): string 22 | { 23 | return $highlighter->parse($content, new PhpDocCommentLanguage()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlFunctionPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)\(/i'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/FunctionCallPattern.php: -------------------------------------------------------------------------------- 1 | [a-z][\w]+)\('; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/OperatorPattern.php: -------------------------------------------------------------------------------- 1 | operator, '/'); 22 | 23 | return "/(?{$quoted})/"; 24 | } 25 | 26 | public function getTokenType(): TokenTypeEnum 27 | { 28 | return TokenTypeEnum::OPERATOR; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsDocTypePattern.php: -------------------------------------------------------------------------------- 1 | \{(.*?)\})'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsDoubleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | "(\\\"|.)*?")'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsSingleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | '(\\\'|.)*?')"; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Blade/Patterns/BladeCommentPattern.php: -------------------------------------------------------------------------------- 1 | \{\{\-\-(.|\n)*?\-\-\}\})'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::COMMENT; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Html/Injections/JavaScriptInHtmlInjection.php: -------------------------------------------------------------------------------- 1 | (?(.|\n)*)<\/script>'; 19 | } 20 | 21 | public function parseContent(string $content, Highlighter $highlighter): string 22 | { 23 | return $highlighter->parse($content, new JavaScriptLanguage()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/StaticPropertyPattern.php: -------------------------------------------------------------------------------- 1 | \\$[\w]+)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlFromTablePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)/i'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Twig/Injections/TwigEchoInjection.php: -------------------------------------------------------------------------------- 1 | ({{(.|\n)*?}}))'; 20 | } 21 | 22 | public function parseContent(string $content, Highlighter $highlighter): string 23 | { 24 | return Escape::injection($highlighter->parse($content, new TwigEchoLanguage())); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Twig/Injections/TwigTagInjection.php: -------------------------------------------------------------------------------- 1 | ({%(.|\n)*?%}))'; 20 | } 21 | 22 | public function parseContent(string $content, Highlighter $highlighter): string 23 | { 24 | return Escape::injection($highlighter->parse($content, new TwigTagLanguage())); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ImplementsPattern.php: -------------------------------------------------------------------------------- 1 | [\s\,\w]+)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlAsTablePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)/i'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test-terminal.php: -------------------------------------------------------------------------------- 1 | withGutter(); 9 | 10 | $target = $argc > 1 11 | ? $argv[1] 12 | : 'targets' . DIRECTORY_SEPARATOR . 'test.md'; 13 | 14 | $code = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . $target); 15 | 16 | preg_match('/```(\w+)/', $code, $matches); 17 | 18 | $language = count($matches) > 0 19 | ? $matches[1] 20 | : 'html'; 21 | 22 | $code = str_replace( 23 | ['```' . $language, '```'], 24 | '', 25 | $code, 26 | ); 27 | 28 | echo PHP_EOL, $highlighter->parse($code, $language), PHP_EOL, PHP_EOL; 29 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssVariablePattern.php: -------------------------------------------------------------------------------- 1 | \-\-[\w\-]+)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/DocComment/DocCommentLanguage.php: -------------------------------------------------------------------------------- 1 | [A-Z][\w]+)\.'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlJoinTablePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)/i'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlObjectBracketsPattern.php: -------------------------------------------------------------------------------- 1 | (\{|\}))'; 20 | } 21 | 22 | public function getTokenType(): TokenTypeEnum 23 | { 24 | return TokenTypeEnum::PROPERTY; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/ReturnTypePattern.php: -------------------------------------------------------------------------------- 1 | Bar:', output: 'Bar')] 13 | #[PatternTest(input: 'func foo() -> Bar :', output: 'Bar')] 14 | final readonly class ReturnTypePattern implements Pattern 15 | { 16 | use IsPattern; 17 | 18 | public function getPattern(): string 19 | { 20 | return '\)\s*\-\>\s*(?.+?)[\s*:\n]'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/EnumBackedTypePattern.php: -------------------------------------------------------------------------------- 1 | int|string)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlPropertyPattern.php: -------------------------------------------------------------------------------- 1 | [\w-]+)\:'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::KEYWORD; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/InstanceOfPattern.php: -------------------------------------------------------------------------------- 1 | [\w\\\\]+)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsMultilineCommentPattern.php: -------------------------------------------------------------------------------- 1 | \/\*(.|\n)*?\*\/)/m'; 27 | } 28 | 29 | public function getTokenType(): TokenTypeEnum 30 | { 31 | return TokenTypeEnum::COMMENT; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyClassNamePattern.php: -------------------------------------------------------------------------------- 1 | \w*)(?=[\s*\:(])'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Sql/Patterns/SqlMultilineCommentPattern.php: -------------------------------------------------------------------------------- 1 | \/\*(.|\n)*?\*\/)/m'; 27 | } 28 | 29 | public function getTokenType(): TokenTypeEnum 30 | { 31 | return TokenTypeEnum::COMMENT; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Languages/Yaml/Patterns/YamlColonPattern.php: -------------------------------------------------------------------------------- 1 | :)/m'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyDecoratorPattern.php: -------------------------------------------------------------------------------- 1 | @\s*\w*(?:\.\w+)*)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::PROPERTY; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Base/Patterns/DeletionEndTokenPattern.php: -------------------------------------------------------------------------------- 1 | \-})/'; 22 | } 23 | 24 | public function getTokenType(): TokenType 25 | { 26 | return new IgnoreTokenType(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsNewObjectPattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)(\s)*\('; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Xml/Patterns/XmlOpenTagPattern.php: -------------------------------------------------------------------------------- 1 | ', output: 'x-hello')] 13 | #[PatternTest(input: '', output: 'a')] 14 | #[PatternTest(input: '
', output: 'br')] 15 | final readonly class XmlOpenTagPattern implements Pattern 16 | { 17 | use IsPattern; 18 | 19 | public function getPattern(): string 20 | { 21 | return '<(?[\w\-]+)'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::KEYWORD; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Base/Patterns/DeletionStartTokenPattern.php: -------------------------------------------------------------------------------- 1 | {\-)(?!-)/'; 22 | } 23 | 24 | public function getTokenType(): TokenType 25 | { 26 | return new IgnoreTokenType(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Gdscript/Patterns/VarTypePattern.php: -------------------------------------------------------------------------------- 1 | \w+)'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::TYPE; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpDocCommentReturnTypePattern.php: -------------------------------------------------------------------------------- 1 | .*?)(\*\/|$|\R)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Languages/Base/Injections/AdditionInjectionTest.php: -------------------------------------------------------------------------------- 1 | class Foo 22 | TXT; 23 | 24 | $parsedInjection = (new AdditionInjection())->parse($content, new Highlighter()); 25 | 26 | $this->assertSame($expected, Escape::html($parsedInjection->content)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Languages/Base/Injections/DeletionInjectionTest.php: -------------------------------------------------------------------------------- 1 | class Foo 22 | TXT; 23 | 24 | $parsedInjection = (new DeletionInjection())->parse($content, new Highlighter()); 25 | 26 | $this->assertSame($expected, Escape::html($parsedInjection->content)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Languages/Blade/Injections/BladeEchoInjectionTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 20 | injection: new BladeEchoInjection(), 21 | content: '{{ count($foo) }}', 22 | expectedContent: '{{ count($foo) }}', 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests 15 | 16 | 17 | 18 | 19 | 20 | ./src 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpDocCommentParamTypePattern.php: -------------------------------------------------------------------------------- 1 | .*?) \\$'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/TokenTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($a->containsOrOverlaps($b)); 30 | $this->assertFalse($b->containsOrOverlaps($a)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/StaticClassCallPattern.php: -------------------------------------------------------------------------------- 1 | [\\\\\w]+)\:\:'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::TYPE; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Twig/Patterns/TwigCommentPattern.php: -------------------------------------------------------------------------------- 1 | {# foo #}', output: '{# foo #}')] 21 | final readonly class TwigCommentPattern implements Pattern 22 | { 23 | use IsPattern; 24 | 25 | public function getPattern(): string 26 | { 27 | return '(?\{#(.|\n)*?#})'; 28 | } 29 | 30 | public function getTokenType(): TokenTypeEnum 31 | { 32 | return TokenTypeEnum::COMMENT; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Languages/Xml/Patterns/XmlAttributePattern.php: -------------------------------------------------------------------------------- 1 | ', output: 'attr')] 13 | #[PatternTest(input: '
', output: 'href')] 14 | #[PatternTest(input: '', output: 'data-type')] 15 | final readonly class XmlAttributePattern implements Pattern 16 | { 17 | use IsPattern; 18 | 19 | public function getPattern(): string 20 | { 21 | return '(?[\w\-]+)="'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::PROPERTY; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/CatchTypePattern.php: -------------------------------------------------------------------------------- 1 | [\w\||]+)\)'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::TYPE; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/GroupedTypePattern.php: -------------------------------------------------------------------------------- 1 | \(\w+(&\w+)+\))'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::TYPE; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Languages/Blade/Injections/BladeRawEchoInjectionTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 20 | injection: new BladeRawEchoInjection(), 21 | content: '{!! count($foo) !!}', 22 | expectedContent: '{!! count($foo) !!}', 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Languages/Css/Injections/CssAttributeInjectionTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 20 | injection: new CssAttributeInHtmlInjection(), 21 | content: '', 22 | expectedContent: '<x-slot style="color: green">' 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/ClassNamePatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new ClassNamePattern(), 19 | content: 'class Foo implements Bar', 20 | expected: 'Foo', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new ClassNamePattern(), 25 | content: 'interface Foo implements Bar', 26 | expected: 'Foo', 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/ConstantPropertyPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new ConstantPropertyPattern(), 19 | content: 'Foo::BAR', 20 | expected: 'BAR', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new ConstantPropertyPattern(), 25 | content: 'Foo::BAR()', 26 | expected: 'BAR', 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssCommentPattern.php: -------------------------------------------------------------------------------- 1 | \/\*(.|\n)*?\*\/)/m'; 26 | } 27 | 28 | public function getTokenType(): TokenTypeEnum 29 | { 30 | return TokenTypeEnum::COMMENT; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpDocCommentReturnTypeSingleLinePattern.php: -------------------------------------------------------------------------------- 1 | .*?)(\s*\*\/|$)'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::TYPE; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/NestedFunctionCallPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new NestedFunctionCallPattern(), 19 | content: ' foo()', 20 | expected: 'foo', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new NestedFunctionCallPattern(), 25 | content: '(foo()', 26 | expected: 'foo', 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Diff/Injections/DiffAdditionInjection.php: -------------------------------------------------------------------------------- 1 | + '); 19 | $close = Escape::tokens(''); 20 | 21 | return $open . $matches[1] . $close; 22 | }, 23 | $content 24 | ); 25 | 26 | return new ParsedInjection($content); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/MultilineSingleDocCommentPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new MultilineSingleDocCommentPattern(), 19 | content: ' 20 | use Tempest\Highlight\Token; 21 | 22 | /* world 23 | * hello 24 | */ 25 | final class PhpLanguage implements Language 26 | ', 27 | expected: '/* world 28 | * hello 29 | */', 30 | ); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/NewObjectPattern.php: -------------------------------------------------------------------------------- 1 | [\w\\\\]+)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::TYPE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/UntypedClassPropertyPattern.php: -------------------------------------------------------------------------------- 1 | \\$[\w]+)'; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::PROPERTY; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Languages/Twig/Injections/TwigEchoInjectionTest.php: -------------------------------------------------------------------------------- 1 | Version {{ appVersion }}'; 20 | 21 | $expected = '<b>Version</b> {{ appVersion }}'; 22 | 23 | $this->assertMatches( 24 | injection: new TwigEchoInjection(), 25 | content: $content, 26 | expectedContent: $expected, 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PropertyHookSetParameterTypePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 23 | } 24 | 25 | public function getTokenType(): TokenType 26 | { 27 | return TokenTypeEnum::TYPE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssAttributePattern.php: -------------------------------------------------------------------------------- 1 | [\w\-]+):'; 27 | } 28 | 29 | public function getTokenType(): TokenTypeEnum 30 | { 31 | return TokenTypeEnum::PROPERTY; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Languages/Php/Injections/PhpFunctionParametersInjection.php: -------------------------------------------------------------------------------- 1 | (.|\n)*?)({|\)[\s]*({|;|:|=>))'; 20 | } 21 | 22 | public function parseContent(string $content, Highlighter $highlighter): string 23 | { 24 | return Escape::injection( 25 | $highlighter->parse($content, new PhpTypeLanguage()) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/EnumCasePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)(\s)*(=|;)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::PROPERTY; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/ParsedInjection.php: -------------------------------------------------------------------------------- 1 | .+?)\s[\w]+\s='; 22 | } 23 | 24 | public function getTokenType(): TokenTypeEnum 25 | { 26 | return TokenTypeEnum::TYPE; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ClassNamePattern.php: -------------------------------------------------------------------------------- 1 | [\w]+)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::TYPE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Languages/Twig/Patterns/TwigKeywordPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new TwigKeywordPattern("extends"), 19 | content: "{% extends 'admin/empty_base.html.twig' %}", 20 | expected: 'extends', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new TwigKeywordPattern("if"), 25 | content: "{% if is_granted('IS_IMPERSONATOR') %}", 26 | expected: 'if', 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Languages/Blade/Injections/BladeKeywordInjectionTest.php: -------------------------------------------------------------------------------- 1 | parse($content, $highlighter); 23 | 24 | $this->assertStringContainsString( 25 | 'count', 26 | $parsedInjection->content, 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssFunctionPattern.php: -------------------------------------------------------------------------------- 1 | [\w\-]+)\('; 28 | } 29 | 30 | public function getTokenType(): TokenTypeEnum 31 | { 32 | return TokenTypeEnum::KEYWORD; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssMediaQueryPattern.php: -------------------------------------------------------------------------------- 1 | \@media(.*)?){'; 21 | } 22 | 23 | public function getTokenType(): TokenTypeEnum 24 | { 25 | return TokenTypeEnum::KEYWORD; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Languages/Text/TextLanguageTest.php: -------------------------------------------------------------------------------- 1 | assertSame( 19 | $expected, 20 | $highlighter->parse($content, 'txt'), 21 | ); 22 | 23 | $this->assertSame( 24 | $expected, 25 | $highlighter->parse($content, 'text'), 26 | ); 27 | } 28 | 29 | public static function data(): array 30 | { 31 | return [ 32 | ['test', 'test'], 33 | ]; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Languages/Diff/Injections/DiffDeletionInjection.php: -------------------------------------------------------------------------------- 1 | - '); 19 | $close = Escape::tokens(''); 20 | 21 | return $open . $matches[1] . $close; // Wraps the matched line with the span 22 | }, 23 | $content 24 | ); 25 | 26 | return new ParsedInjection($content); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PropertyHookGetPattern.php: -------------------------------------------------------------------------------- 1 | ', 21 | 'get' 22 | )] 23 | #[PatternTest('get;', 'get')] 24 | final readonly class PropertyHookGetPattern implements Pattern 25 | { 26 | use IsPattern; 27 | 28 | public function getPattern(): string 29 | { 30 | return '(?get)\s*({|=>|;)'; 31 | } 32 | 33 | public function getTokenType(): TokenType 34 | { 35 | return TokenTypeEnum::KEYWORD; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Languages/Blade/BladeLanguageTest.php: -------------------------------------------------------------------------------- 1 | assertSame( 19 | $expected, 20 | $highlighter->parse($content, 'blade'), 21 | ); 22 | } 23 | 24 | public static function data(): array 25 | { 26 | return [ 27 | ['{{-- Blade comment --}}', '{{-- Blade comment --}}'], 28 | ['{{-- if --}}', '{{-- if --}}'], 29 | ]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Languages/Blade/Patterns/BladeComponentCloseTagPattern.php: -------------------------------------------------------------------------------- 1 | ', output: 'a')] 13 | #[PatternTest(input: '', output: 'x-hello')] 14 | #[PatternTest(input: '', output: 'x-hello::world')] 15 | #[PatternTest(input: '', output: 'x-hello::world.lorem')] 16 | final readonly class BladeComponentCloseTagPattern implements Pattern 17 | { 18 | use IsPattern; 19 | 20 | public function getPattern(): string 21 | { 22 | return '<\/(?[\w\-\:\.]+)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::KEYWORD; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsDocParamNamePattern.php: -------------------------------------------------------------------------------- 1 | (.*?))(\s|$)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Dockerfile/Patterns/ImageAliasKeywordPattern.php: -------------------------------------------------------------------------------- 1 | AS)[\s][\S]+/m"; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::KEYWORD; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpDocCommentVariablePattern.php: -------------------------------------------------------------------------------- 1 | \\$[\w]+)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VARIABLE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Dockerfile/Patterns/ImageAliasNamePattern.php: -------------------------------------------------------------------------------- 1 | [\S]+)/m"; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::VALUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Languages/Blade/Injections/BladePhpInjectionTest.php: -------------------------------------------------------------------------------- 1 | parse($content, $highlighter); 27 | 28 | $this->assertStringContainsString( 29 | 'count', 30 | $parsedInjection->content, 31 | ); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ConstantNamePattern.php: -------------------------------------------------------------------------------- 1 | [A-Z_]+)\b'; 31 | } 32 | 33 | public function getTokenType(): TokenTypeEnum 34 | { 35 | return TokenTypeEnum::PROPERTY; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Languages/Twig/Patterns/TwigTokenPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 19 | pattern: new TwigTokenPattern("^{%", TokenTypeEnum::TYPE), 20 | content: "{% extends 'admin/empty_base.html.twig' %}", 21 | expected: '{%', 22 | ); 23 | 24 | $this->assertMatches( 25 | pattern: new TwigTokenPattern("}}$", TokenTypeEnum::TYPE), 26 | content: "{{ path('app.logout') }}", 27 | expected: '}}', 28 | ); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/CommonMark/HighlightExtension.php: -------------------------------------------------------------------------------- 1 | addRenderer(FencedCode::class, new CodeBlockRenderer($this->highlighter), 10) 24 | ->addRenderer(Code::class, new InlineCodeBlockRenderer($this->highlighter), 10) 25 | ; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Languages/Blade/Patterns/BladeComponentOpenTagPattern.php: -------------------------------------------------------------------------------- 1 | ', output: 'a')] 13 | #[PatternTest(input: '', output: 'x-hello')] 14 | #[PatternTest(input: '', output: 'x-hello::world')] 15 | #[PatternTest(input: '', output: 'x-hello::world.lorem')] 16 | final readonly class BladeComponentOpenTagPattern implements Pattern 17 | { 18 | use IsPattern; 19 | 20 | public function getPattern(): string 21 | { 22 | return '<(?[\w\-\:\.]+)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::KEYWORD; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PropertyHookSetPattern.php: -------------------------------------------------------------------------------- 1 | ', 'set')] 20 | #[PatternTest('set (string $value', 'set')] 21 | #[PatternTest('set(string $value', 'set')] 22 | final readonly class PropertyHookSetPattern implements Pattern 23 | { 24 | use IsPattern; 25 | 26 | public function getPattern(): string 27 | { 28 | return '(?set)\s*({|;|=>|\()'; 29 | } 30 | 31 | public function getTokenType(): TokenType 32 | { 33 | return TokenTypeEnum::KEYWORD; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Languages/Twig/TwigLanguage.php: -------------------------------------------------------------------------------- 1 | [\w\/]+)/m"; 25 | } 26 | 27 | public function getTokenType(): TokenTypeEnum 28 | { 29 | return TokenTypeEnum::VALUE; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Languages/Dockerfile/Patterns/ImageTagPattern.php: -------------------------------------------------------------------------------- 1 | \S+)[\s]?/m"; 25 | } 26 | 27 | public function getTokenType(): TokenTypeEnum 28 | { 29 | return TokenTypeEnum::VALUE; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/SingleQuoteValuePattern.php: -------------------------------------------------------------------------------- 1 | '(\\\'|.)*?')"; 31 | } 32 | 33 | public function getTokenType(): TokenTypeEnum 34 | { 35 | return TokenTypeEnum::VALUE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Languages/Xml/Patterns/XmlCommentPattern.php: -------------------------------------------------------------------------------- 1 | 17 | test', 18 | output: '' 21 | )] 22 | #[PatternTest(input: '
', output: ['',''])] 23 | final readonly class XmlCommentPattern implements Pattern 24 | { 25 | use IsPattern; 26 | 27 | public function getPattern(): string 28 | { 29 | return '/(?\)/mU'; 30 | } 31 | 32 | public function getTokenType(): TokenTypeEnum 33 | { 34 | return TokenTypeEnum::COMMENT; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpDocCommentVarTypePattern.php: -------------------------------------------------------------------------------- 1 | .*?)( \$|\*|$)'; 23 | } 24 | 25 | public function getTokenType(): TokenTypeEnum 26 | { 27 | return TokenTypeEnum::TYPE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/KeywordPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new KeywordPattern('match'), 19 | content: 'match ()', 20 | expected: 'match', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new KeywordPattern('return'), 25 | content: 'return ()', 26 | expected: 'return', 27 | ); 28 | 29 | $this->assertMatches( 30 | pattern: new KeywordPattern('class'), 31 | content: 'class ()', 32 | expected: 'class', 33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Languages/Diff/DiffLanguageTest.php: -------------------------------------------------------------------------------- 1 | assertSame( 19 | $expected, 20 | $highlighter->parse($content, 'diff'), 21 | ); 22 | } 23 | 24 | public static function data(): array 25 | { 26 | return [ 27 | [<<<'TXT' 28 | + $this->newCode(); 29 | - $this->oldCode(); 30 | TXT, 31 | <<<'TXT' 32 | + $this->newCode(); 33 | - $this->oldCode(); 34 | TXT 35 | ], 36 | ]; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/AttributeTypePattern.php: -------------------------------------------------------------------------------- 1 | [\\\\]*[A-Z][\w\\\\]+)/m'; 31 | } 32 | 33 | public function getTokenType(): TokenTypeEnum 34 | { 35 | return TokenTypeEnum::TYPE; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Languages/Php/Patterns/PropertyAccessPatternTest.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new PropertyAccessPattern(), 19 | content: '$this->foo', 20 | expected: 'foo', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new PropertyAccessPattern(), 25 | content: '$this->foo()', 26 | expected: 'foo', 27 | ); 28 | 29 | $this->assertMatches( 30 | pattern: new PropertyAccessPattern(), 31 | content: '$obj->foo', 32 | expected: 'foo', 33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Languages/Php/Injections/PhpAttributePlainInjection.php: -------------------------------------------------------------------------------- 1 | \#\[[\w]+\])'; 20 | } 21 | 22 | public function parseContent(string $content, Highlighter $highlighter): string 23 | { 24 | $theme = $highlighter->getTheme(); 25 | 26 | $parsed = '#[' . $highlighter->parse(str_replace(['#[', ']'], '', $content), 'php') . ']'; 27 | 28 | return Escape::tokens($theme->before(TokenTypeEnum::ATTRIBUTE)) 29 | . $parsed 30 | . Escape::tokens($theme->after(TokenTypeEnum::ATTRIBUTE)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/KeywordPattern.php: -------------------------------------------------------------------------------- 1 | caseInsensitive = true; 24 | 25 | return $this; 26 | } 27 | 28 | public function getPattern(): string 29 | { 30 | $pattern = "/\b(?)(?{$this->keyword})(\$|\,|\)|\;|\:|\s|\()/"; 31 | 32 | if ($this->caseInsensitive) { 33 | $pattern .= 'i'; 34 | } 35 | 36 | return $pattern; 37 | } 38 | 39 | public function getTokenType(): TokenTypeEnum 40 | { 41 | return TokenTypeEnum::KEYWORD; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Languages/JavaScript/Patterns/JsKeywordPattern.php: -------------------------------------------------------------------------------- 1 | caseInsensitive = true; 24 | 25 | return $this; 26 | } 27 | 28 | public function getPattern(): string 29 | { 30 | $pattern = "/\b(?{$this->keyword})(\,|\.|\)|\;|\:|\s|\()/"; 31 | 32 | if ($this->caseInsensitive) { 33 | $pattern .= 'i'; 34 | } 35 | 36 | return $pattern; 37 | } 38 | 39 | public function getTokenType(): TokenTypeEnum 40 | { 41 | return TokenTypeEnum::KEYWORD; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Languages/Xml/XmlLanguage.php: -------------------------------------------------------------------------------- 1 | \#\[[\w]+\((.|\n)*?\)\])'; 20 | } 21 | 22 | public function parseContent(string $content, Highlighter $highlighter): string 23 | { 24 | $theme = $highlighter->getTheme(); 25 | 26 | $parsed = '#[' . $highlighter->parse(str_replace(['#[', ')]'], '', $content), 'php') . ')]'; 27 | 28 | return Escape::injection( 29 | Escape::tokens($theme->before(TokenTypeEnum::ATTRIBUTE)) 30 | . $parsed 31 | . Escape::tokens($theme->after(TokenTypeEnum::ATTRIBUTE)) 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Languages/Python/Patterns/PyKeywordPattern.php: -------------------------------------------------------------------------------- 1 | keywords); 27 | 28 | return "\b(?(?:_(?=\s*:){$keywords}))\b"; 29 | } 30 | 31 | public function getTokenType(): TokenTypeEnum 32 | { 33 | return TokenTypeEnum::KEYWORD; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Languages/Php/Injections/PhpShortEchoInjectionTest.php: -------------------------------------------------------------------------------- 1 | name ?> 21 | Hello, other ?> 22 | '; 23 | 24 | $expected = ' 25 | Hello, <?= $this->name ?> 26 | Hello, <?= $this->other ?> 27 | '; 28 | 29 | $this->assertMatches( 30 | injection: new PhpShortEchoInHtmlInjection(), 31 | content: $content, 32 | expectedContent: $expected, 33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Languages/Json/JsonLanguage.php: -------------------------------------------------------------------------------- 1 | assertMatches( 18 | pattern: new NamedArgumentPattern(), 19 | content: '#[Foo(prop: hi)]', 20 | expected: 'prop', 21 | ); 22 | 23 | $this->assertMatches( 24 | pattern: new NamedArgumentPattern(), 25 | content: 'foo(bar: $a, baz: $b)', 26 | expected: ['bar', 'baz'], 27 | ); 28 | 29 | $this->assertMatches( 30 | pattern: new NamedArgumentPattern(), 31 | content: 'foo( 32 | bar: $a, 33 | baz: $b 34 | )', 35 | expected: ['bar', 'baz'], 36 | ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/TestsInjections.php: -------------------------------------------------------------------------------- 1 | nested(); 21 | 22 | if ($currentLanguage) { 23 | $highlighter->setCurrentLanguage($currentLanguage); 24 | } 25 | 26 | $parsedInjection = $injection->parse($content, $highlighter); 27 | 28 | if (is_string($parsedInjection)) { 29 | $content = $parsedInjection; 30 | } else { 31 | $content = $parsedInjection->content; 32 | } 33 | 34 | $output = Escape::html($content); 35 | 36 | $this->assertSame( 37 | $expectedContent, 38 | $output, 39 | ); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/Languages/Xml/XmlLanguageTest.php: -------------------------------------------------------------------------------- 1 | assertSame( 19 | $expected, 20 | $highlighter->parse($content, 'xml'), 21 | ); 22 | } 23 | 24 | public static function data(): array 25 | { 26 | return [ 27 | [<< 29 |
30 | 31 | 32 | 33 | TXT, 34 | <<tag attr=""> 36 | <br/> 37 | 38 | <!-- comment --> 39 | </tag> 40 | TXT], 41 | ]; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/TypeForVariablePattern.php: -------------------------------------------------------------------------------- 1 | [\w\&\(\)\|\\\\\?]+)\s+(\.*)\\$'; 26 | } 27 | 28 | public function getTokenType(): TokenTypeEnum 29 | { 30 | return TokenTypeEnum::TYPE; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/CommonMark/InlineCodeBlockRenderer.php: -------------------------------------------------------------------------------- 1 | [\w]+)}(?.*)/', $node->getLiteral(), $match); 28 | 29 | $language = $match['match'] ?? 'txt'; 30 | $code = $match['code'] ?? $node->getLiteral(); 31 | 32 | return '' . $this->highlighter->parse($code, $language) . ''; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/CommonMark/HighlightExtensionTest.php: -------------------------------------------------------------------------------- 1 | addExtension(new CommonMarkCoreExtension()) 22 | ->addExtension(new HighlightExtension()) 23 | ->addExtension(new FrontMatterExtension()); 24 | 25 | $markdown = new MarkdownConverter($environment); 26 | 27 | $parsed = $markdown->convert("```php 28 | class Foo {} 29 | ```"); 30 | 31 | $this->assertStringContainsString('hl-keyword', $parsed->getContent()); 32 | $this->assertStringContainsString('data-lang="php"', $parsed->getContent()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Tokens/TokenTypeEnum.php: -------------------------------------------------------------------------------- 1 | value; 26 | } 27 | 28 | public function canContain(TokenType $other): bool 29 | { 30 | if ($this === $other) { 31 | return false; 32 | } 33 | 34 | return match ($this) { 35 | self::TYPE => ! in_array($other->getValue(), [self::KEYWORD->getValue(), self::PROPERTY->getValue()]), 36 | self::GENERIC => ! in_array($other->getValue(), [self::TYPE->getValue()]), 37 | self::ATTRIBUTE => true, 38 | default => false, 39 | }; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ReturnTypePattern.php: -------------------------------------------------------------------------------- 1 | [\w\(\)\&\|\?]+)[\s{;\n]'; 28 | } 29 | 30 | public function getTokenType(): TokenTypeEnum 31 | { 32 | return TokenTypeEnum::TYPE; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Languages/Json/JsonLanguageTest.php: -------------------------------------------------------------------------------- 1 | assertSame( 19 | $expected, 20 | $highlighter->parse($content, 'json'), 21 | ); 22 | } 23 | 24 | public static function data(): array 25 | { 26 | return [ 27 | [ 28 | '{ 29 | "key": "value", 30 | "array": ["bar"] 31 | }', 32 | '{ 33 | "key": "value", 34 | "array": ["bar"] 35 | }', 36 | ], 37 | ]; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/IsInjection.php: -------------------------------------------------------------------------------- 1 | getPattern(); 16 | 17 | if (! str_starts_with($pattern, '/')) { 18 | $pattern = "/{$pattern}/"; 19 | } 20 | 21 | $result = preg_replace_callback( 22 | pattern: $pattern, 23 | callback: function ($matches) use ($highlighter) { 24 | $content = $matches['match'] ?? ''; 25 | 26 | if (! $content) { 27 | return $matches[0]; 28 | } 29 | 30 | return str_replace( 31 | search: $content, 32 | replace: $this->parseContent($content, $highlighter), 33 | subject: $matches[0], 34 | ); 35 | }, 36 | subject: $content, 37 | ); 38 | 39 | return new ParsedInjection($result ?? $content); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Languages/Html/HtmlLanguage.php: -------------------------------------------------------------------------------- 1 | void: 11 | agent.max_speed = speed 12 | 13 | agent.connect("velocity_computed", func(new_velocity: Vector2): 14 | # use velocity_computed so that obstacles are avoided 15 | if not agent.is_navigation_finished(): 16 | velocity = new_velocity 17 | move_and_slide() 18 | ) 19 | 20 | func _physics_process(_delta) -> void: 21 | var next_location = agent.get_next_path_position() 22 | 23 | if global_position.distance_to(next_location) > 1: 24 | update_navigation() 25 | 26 | if not agent.is_navigation_finished(): 27 | var next_move_position = agent.get_next_path_position() - global_position 28 | agent.velocity = next_move_position * speed 29 | 30 | if Input.is_action_pressed("ui_move"): 31 | agent.target_position = get_global_mouse_position() 32 | 33 | func update_navigation() -> void: 34 | # move the navigation region so that the player is always in the center 35 | navigation.global_position = global_position - (get_viewport().get_visible_rect().size / 2) 36 | navigation.bake_navigation_polygon() 37 | ``` 38 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/ClassPropertyPattern.php: -------------------------------------------------------------------------------- 1 | \\$[\w]+)'; 28 | } 29 | 30 | public function getTokenType(): TokenTypeEnum 31 | { 32 | return TokenTypeEnum::PROPERTY; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Languages/Css/Patterns/CssSelectorPattern.php: -------------------------------------------------------------------------------- 1 | [\[\]\'\"\=\@\-\#\.\w\s,\n\+\:\(\)\*]+)\{'; 35 | } 36 | 37 | public function getTokenType(): TokenTypeEnum 38 | { 39 | return TokenTypeEnum::KEYWORD; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Languages/Php/Patterns/PhpAsymmetricPropertyPattern.php: -------------------------------------------------------------------------------- 1 | getPattern(); 23 | 24 | if (! str_starts_with($pattern, '/')) { 25 | $pattern = "/$pattern/"; 26 | } 27 | 28 | preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE); 29 | 30 | return $matches; 31 | } 32 | 33 | public function getPattern(): string 34 | { 35 | return '/(public|private|protected)\((?set)\)/'; 36 | } 37 | 38 | public function getTokenType(): TokenType 39 | { 40 | return TokenTypeEnum::KEYWORD; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Themes/Css/highlight-light-lite.css: -------------------------------------------------------------------------------- 1 | pre, code { 2 | color: #000; 3 | background-color: #f3f3f3; 4 | } 5 | 6 | .hl-keyword { 7 | color: #4285F4; 8 | } 9 | 10 | .hl-property { 11 | color: #34A853; 12 | } 13 | 14 | .hl-attribute { 15 | font-style: italic; 16 | } 17 | 18 | .hl-type { 19 | color: #EA4334; 20 | } 21 | 22 | .hl-generic { 23 | color: #9d3af6; 24 | } 25 | 26 | .hl-number, 27 | .hl-literal, 28 | .hl-value { 29 | color: #000; 30 | } 31 | 32 | .hl-variable { 33 | color: #000; 34 | } 35 | 36 | .hl-comment { 37 | color: #888888; 38 | } 39 | 40 | .hl-blur { 41 | filter: blur(2px); 42 | } 43 | 44 | .hl-strong { 45 | font-weight: bold; 46 | } 47 | 48 | .hl-em { 49 | font-style: italic; 50 | } 51 | 52 | .hl-addition { 53 | min-width: 100%; 54 | background-color: #00FF0022; 55 | } 56 | 57 | .hl-deletion { 58 | min-width: 100%; 59 | background-color: #FF000011; 60 | } 61 | 62 | .hl-gutter { 63 | display: inline-block; 64 | font-size: 0.9em; 65 | color: #555; 66 | padding: 0 1ch; 67 | margin-right: 1ch; 68 | user-select: none; 69 | } 70 | 71 | .hl-gutter-addition { 72 | background-color: #34A853; 73 | color: #fff; 74 | } 75 | 76 | .hl-gutter-deletion { 77 | background-color: #EA4334; 78 | color: #fff; 79 | } --------------------------------------------------------------------------------