Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize newline formats #150

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function parse(string $content, string|Language $language): string

$this->currentLanguage = $language;

$content = $this->normalizeNewline($content);

return $this->parseContent($content, $language);
}

Expand Down Expand Up @@ -198,4 +200,9 @@ private function getAfterInjections(Language $language): Generator
yield $this->gutterInjection;
}
}

private function normalizeNewline(string $subject): string
{
return preg_replace('~\R~u', "\n", $subject);
}
}
1 change: 1 addition & 0 deletions tests/HighlighterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function data(): array
return [
['01', 'php'], // general
['02', 'html'], // deep injections
['03', 'php'], // windows line endings
];
}
}
37 changes: 37 additions & 0 deletions tests/stubs/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// controller for home
final readonly class HomeController
{
#[Get(uri: '/home')]
public function __invoke(): View
{
return view('Views/home.view.php')
->data(
name: 'Brent',
date: new DateTime(),
);
}

#[Post(uri: '/home')]
public function __invoke(): View
{
}
}
===
<span class="hl-comment">// controller for home</span>
<span class="hl-keyword">final</span> <span class="hl-keyword">readonly</span> <span class="hl-keyword">class</span> <span class="hl-type">HomeController</span>
{
<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">Get</span>(<span class="hl-property">uri</span>: '<span class="hl-value">/home</span>')]</span></span>
<span class="hl-keyword">public</span> <span class="hl-keyword">function</span> <span class="hl-property">__invoke</span>(): <span class="hl-type">View</span>
{
<span class="hl-keyword">return</span> <span class="hl-property">view</span>('<span class="hl-value">Views/home.view.php</span>')
-&gt;<span class="hl-property">data</span>(
<span class="hl-property">name</span>: '<span class="hl-value">Brent</span>',
<span class="hl-property">date</span>: <span class="hl-keyword">new</span> <span class="hl-type">DateTime</span>(),
);
}

<span class="hl-injection"><span class="hl-attribute">#[<span class="hl-type">Post</span>(<span class="hl-property">uri</span>: '<span class="hl-value">/home</span>')]</span></span>
<span class="hl-keyword">public</span> <span class="hl-keyword">function</span> <span class="hl-property">__invoke</span>(): <span class="hl-type">View</span>
{
}
}
Loading