Skip to content

Commit

Permalink
Merge pull request #150 from ma4nn/normalize-newline-chars
Browse files Browse the repository at this point in the history
Normalize newline formats
  • Loading branch information
brendt authored Sep 12, 2024
2 parents fb3309c + 026e7ae commit e21979d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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>
{
}
}

0 comments on commit e21979d

Please sign in to comment.