Skip to content

Commit

Permalink
Merge branch 'main' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 15, 2024
2 parents a34e121 + 6116e31 commit c05485d
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 13 deletions.
14 changes: 14 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ interface Injection

- `Highlighter::withGutter()` is now an immutable function

## 1.3.4

- Fallback for preg JIT exhaustion

## 1.3.3

- Fix bug with FQCN and `::class`

## 1.3.2

- Fix bug where two or more `style` tags within the same HTML file caused an error
- Fix CSS media query bug
- Fix missing CSS selector tokens bug

## 1.3.0

- Add `data-lang` attribute to pre tags (#90)
Expand Down
6 changes: 4 additions & 2 deletions src/CommonMark/CodeBlockRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer)
$highlighter = $highlighter->withGutter((int)$startAt);
}

$parsed = $highlighter->parse($node->getLiteral(), $matches['language']);
$language = $matches['language'] ?? 'txt';

$parsed = $highlighter->parse($node->getLiteral(), $language);

$theme = $highlighter->getTheme();

if ($theme instanceof WebTheme) {
return $theme->preBefore($highlighter) . $parsed . $theme->preAfter($highlighter);
} else {
return '<pre data-lang="' . $matches['language'] . '">' . $parsed . '</pre>';
return '<pre data-lang="' . $language . '">' . $parsed . '</pre>';
}
}
}
6 changes: 3 additions & 3 deletions src/IsInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
$pattern = "/{$pattern}/";
}

$content = preg_replace_callback(
$result = preg_replace_callback(
pattern: $pattern,
callback: function ($matches) use ($highlighter) {
$content = $matches['match'] ?? null;
$content = $matches['match'] ?? '';

if (! $content) {
return $matches[0];
Expand All @@ -36,6 +36,6 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
subject: $content,
);

return new ParsedInjection($content);
return new ParsedInjection($result ?? $content);
}
}
2 changes: 2 additions & 0 deletions src/Languages/Css/CssLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Highlight\Languages\Css\Patterns\CssAttributePattern;
use Tempest\Highlight\Languages\Css\Patterns\CssCommentPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssFunctionPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssMediaQueryPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssSelectorPattern;
use Tempest\Highlight\Languages\Css\Patterns\CssVariablePattern;

Expand All @@ -29,6 +30,7 @@ public function getPatterns(): array
{
return [
...parent::getPatterns(),
new CssMediaQueryPattern(),
new CssCommentPattern(),
new CssSelectorPattern(),
new CssAttributePattern(),
Expand Down
27 changes: 27 additions & 0 deletions src/Languages/Css/Patterns/CssMediaQueryPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Css\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest(input: '@media only screen and (max-width: 500px) {', output: '@media only screen and (max-width: 500px) ')]
#[PatternTest(input: '@media (min-width: 30em) and (max-width: 50em) {', output: '@media (min-width: 30em) and (max-width: 50em) ')]
final readonly class CssMediaQueryPattern implements Pattern
{
use IsPattern;

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

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::KEYWORD;
}
}
3 changes: 2 additions & 1 deletion src/Languages/Css/Patterns/CssSelectorPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#[PatternTest(input: "[data-x='asd'] .light {", output: "[data-x='asd'] .light ")]
#[PatternTest(input: "foo + bar {", output: "foo + bar ")]
#[PatternTest(input: "foo:hover {", output: "foo:hover ")]
#[PatternTest(input: "body *:not(html):not(style):not(br):not(tr):not(code) {", output: "body *:not(html):not(style):not(br):not(tr):not(code) ")]
final readonly class CssSelectorPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>[\[\]\'\"\=\@\-\#\.\w\s,\n\+\:]+)\{';
return '(?<match>[\[\]\'\"\=\@\-\#\.\w\s,\n\+\:\(\)\*]+)\{';
}

public function getTokenType(): TokenTypeEnum
Expand Down
2 changes: 1 addition & 1 deletion src/Languages/Html/Injections/CssInHtmlInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

public function parseContent(string $content, Highlighter $highlighter): string
Expand Down
3 changes: 2 additions & 1 deletion src/Languages/Php/Patterns/StaticClassCallPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

#[PatternTest(input: 'Foo::bar()', output: 'Foo')]
#[PatternTest(input: 'Foo::BAR', output: 'Foo')]
#[PatternTest(input: 'Foo\\Bar::BAR', output: 'Foo\\Bar')]
final readonly class StaticClassCallPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<!\\$)(?<match>[\w]+)\:\:';
return '(?<!\\$)(?<match>[\\\\\w]+)\:\:';
}

public function getTokenType(): TokenTypeEnum
Expand Down
26 changes: 26 additions & 0 deletions tests/CommonMark/CodeBlockRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public function test_commonmark_with_pre(): void
<pre data-lang="php" style="color: #212121; background-color: #ffffff;"><span style="color: #D32F2F;">echo</span>;
</pre>

TXT;

$this->assertSame($expected, $markdown->convert($input)->getContent());
}

public function test_commonmark_with_no_language(): void
{
$environment = new Environment();

$environment
->addExtension(new CommonMarkCoreExtension())
->addExtension(new FrontMatterExtension())
->addRenderer(FencedCode::class, new CodeBlockRenderer());

$markdown = new MarkdownConverter($environment);

$input = <<<'TXT'
```
echo;
```
TXT;

$expected = <<<'TXT'
<pre data-lang="txt">echo;
</pre>

TXT;

$this->assertSame($expected, $markdown->convert($input)->getContent());
Expand Down
1 change: 1 addition & 0 deletions tests/Languages/Css/CssLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function test_highlight(string $content, string $expected): void
public static function data(): array
{
return [
['@media only screen and (max-width: 500px) {}', '<span class="hl-keyword">@media only screen and (max-width: 500px) </span>{}'],
[
<<<TXT
.foo {
Expand Down
5 changes: 3 additions & 2 deletions tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

$environment = new Environment();

$highlighter = (new Highlighter(new InlineTheme(__DIR__ . '/../src/Themes/highlight-light-lite.css')))->withGutter();
$highlighter = (new Highlighter(new InlineTheme(__DIR__ . '/../src/Themes/highlight-light-lite.css')));

$environment
->addExtension(new CommonMarkCoreExtension())
Expand Down Expand Up @@ -56,7 +56,8 @@
pre {
margin: 3em auto;
box-shadow: 0 0 10px 0 #00000044;
padding: 2em 2em 2em 1ch;
/*padding: 2em 2em 2em 1ch;*/
padding: 2em;
/*background-color: #fafafa;*/
border-radius: 3px;
color: #000;
Expand Down
Loading

0 comments on commit c05485d

Please sign in to comment.