From 57adb88526eb1ca26bf8aa2c301feedf38c9ea63 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Fri, 15 Mar 2024 12:54:10 +0100 Subject: [PATCH] wip --- README.md | 42 ++++++++++++++-- src/Languages/BladeLanguage.php | 13 +++++ src/Patterns/Php/ClassNamePattern.php | 2 +- src/Themes/highlight-light-lite.css | 2 +- test-server.php | 5 ++ tests/Patterns/Php/ClassNamePatternTest.php | 6 +++ tests/index.php | 54 +++++++++++++++++++++ tests/test.md | 10 ++++ 8 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 src/Languages/BladeLanguage.php create mode 100644 test-server.php create mode 100644 tests/index.php create mode 100644 tests/test.md diff --git a/README.md b/README.md index 4416a3b..feb4c8c 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ composer require league/commonmark; This package makes it easy for developers to add new languages or extend existing languages. Right now, these languages are supported: `php`, `html`, `css`. More will be added. -In order to build your own highlighter functionality, you need to understand _two_ concepts of how code is highlighted. +In order to build your own highlighter functionality, you need to understand _three_ concepts of how code is highlighted: _patterns_, _injections_, and _languages_. ### 1. Patterns @@ -178,14 +178,46 @@ The second step in providing an _injection_ is to parse the matched content into - Just like patterns, injection regexes should contain a group named `match`, which is written like so `(?…)` - Finally, an injection will use the highlighter to parse its matched content into another language -### Extending existing languages +### 3. Languages -Instead of starting from scratch, the best approach to adding new languages is by extending existing ones. For example, let's add support for `blade`: +The last concept to understand, although it doesn't mean much. _Languages_ are classes that bring these two concepts together. They are nothing more than a collection of patterns and injections. Take a look at the `HtmlLanguage`, for example: ```php -class BladeLanguage extends HtmlLanguage +class HtmlLanguage implements Language { + public function getInjections(): array + { + return [ + new PhpInjection(), + new PhpShortEchoInjection(), + new CssInjection(), + ]; + } + + public function getPatterns(): array + { + return [ + new OpenTagPattern(), + new CloseTagPattern(), + new TagAttributePattern(), + new HtmlCommentPattern(), + ]; + } } ``` -// TODO +This `HtmlLanguage` class specifies the following things: + +- PHP can be injected within HTML, both with the short echo tag `[\w]+)'; + return '(class|interface) (?[\w]+)'; } public function getTokenType(): TokenType diff --git a/src/Themes/highlight-light-lite.css b/src/Themes/highlight-light-lite.css index cbe40d5..adaedd0 100644 --- a/src/Themes/highlight-light-lite.css +++ b/src/Themes/highlight-light-lite.css @@ -18,7 +18,7 @@ } .hl-attribute { - font-family: "Radon", serif; + font-family: "Radon", inherit, monospace; } .hl-type { diff --git a/test-server.php b/test-server.php new file mode 100644 index 0000000..5ffee91 --- /dev/null +++ b/test-server.php @@ -0,0 +1,5 @@ +assertMatches( + pattern: new ClassNamePattern(), + content: 'interface Foo implements Bar', + expected: 'Foo', + ); } } diff --git a/tests/index.php b/tests/index.php new file mode 100644 index 0000000..cc2cad5 --- /dev/null +++ b/tests/index.php @@ -0,0 +1,54 @@ +addExtension(new CommonMarkCoreExtension()) + ->addRenderer(FencedCode::class, new HighlightCodeBlockRenderer()); + +$markdown = new MarkdownConverter($environment); + +$contents = $markdown->convert(file_get_contents(__DIR__ . '/test.md'))->getContent(); + +?> + + + + Test + + + +
+ +
+ + diff --git a/tests/test.md b/tests/test.md new file mode 100644 index 0000000..bacb8fa --- /dev/null +++ b/tests/test.md @@ -0,0 +1,10 @@ +```php +declare(strict_types=1); + +namespace Tempest\Highlight; + +interface Injection +{ + public function parse(string $content, Highlighter $highlighter): string; +} +``` \ No newline at end of file