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

Addition/Deletion Injection Fixes #117

Merged
merged 5 commits into from
May 23, 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
3 changes: 3 additions & 0 deletions src/Languages/Base/Injections/AdditionInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{
public function parse(string $content, Highlighter $highlighter): ParsedInjection
{
// Standardize line endings
$content = preg_replace('/\R/', PHP_EOL, $content);

$content = str_replace('❷span class=❹ignore❹❸{+❷/span❸', '{+', $content);
$content = str_replace('❷span class=❹ignore❹❸+}❷/span❸', '+}', $content);

Expand Down
3 changes: 3 additions & 0 deletions src/Languages/Base/Injections/DeletionInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{
public function parse(string $content, Highlighter $highlighter): ParsedInjection
{
// Standardize line endings
$content = preg_replace('/\R/', PHP_EOL, $content);

$content = str_replace('❷span class=❹ignore❹❸{-❷/span❸', '{-', $content);
$content = str_replace('❷span class=❹ignore❹❸-}❷/span❸', '-}', $content);

Expand Down
3 changes: 2 additions & 1 deletion src/Languages/Base/Injections/GutterInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
foreach ($lines as $i => $line) {
$gutterNumber = $gutterNumbers[$i];

$gutterClass = 'hl-gutter ' . ($this->classes[$i + $this->startAt] ?? '');
$hasClasses = $this->classes[$i + $this->startAt] ?? '';
$gutterClass = 'hl-gutter' . ($hasClasses ? ' ' . $hasClasses : '');

$lines[$i] = sprintf(
Escape::tokens('<span class="%s">%s</span> %s'),
Expand Down
2 changes: 1 addition & 1 deletion src/Themes/InlineTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function before(TokenType $tokenType): string
$style = $this->map[".{$class}"] ?? null;

if (! $style) {
return '<span>';
return "<span class=\"{$class}\">";
}

return "<span style=\"{$style}\">";
Expand Down
2 changes: 1 addition & 1 deletion tests/CommonMark/CodeBlockRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Foo {}
TXT;

$expected = <<<'TXT'
<pre data-lang="php" class="notranslate"><span class="hl-gutter ">10</span> <span class="hl-keyword">class</span> <span class="hl-type">Foo</span> {}</pre>
<pre data-lang="php" class="notranslate"><span class="hl-gutter">10</span> <span class="hl-keyword">class</span> <span class="hl-type">Foo</span> {}</pre>

TXT;

Expand Down
40 changes: 20 additions & 20 deletions tests/Languages/Base/Injections/GutterInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public function test_gutter_injection(): void
TXT;

$expected = <<<'TXT'
<span class="hl-gutter "> 10</span> <span class="hl-keyword">foreach</span> (<span class="hl-variable">$lines</span> <span class="hl-keyword">as</span> <span class="hl-variable">$i</span> =&gt; <span class="hl-variable">$line</span>) {
<span class="hl-gutter "> 11</span> <span class="hl-variable">$gutterNumber</span> = <span class="hl-variable">$gutterNumbers</span>[<span class="hl-variable">$i</span>];
<span class="hl-gutter "> 12</span>
<span class="hl-gutter "> 13</span> <span class="hl-variable">$gutterClass</span> = '<span class="hl-value">hl-gutter </span>' . (<span class="hl-variable">$this</span>-&gt;<span class="hl-property">classes</span>[<span class="hl-variable">$i</span> + 1] ?? '<span class="hl-value"></span>');
<span class="hl-gutter"> 10</span> <span class="hl-keyword">foreach</span> (<span class="hl-variable">$lines</span> <span class="hl-keyword">as</span> <span class="hl-variable">$i</span> =&gt; <span class="hl-variable">$line</span>) {
<span class="hl-gutter"> 11</span> <span class="hl-variable">$gutterNumber</span> = <span class="hl-variable">$gutterNumbers</span>[<span class="hl-variable">$i</span>];
<span class="hl-gutter"> 12</span>
<span class="hl-gutter"> 13</span> <span class="hl-variable">$gutterClass</span> = '<span class="hl-value">hl-gutter </span>' . (<span class="hl-variable">$this</span>-&gt;<span class="hl-property">classes</span>[<span class="hl-variable">$i</span> + 1] ?? '<span class="hl-value"></span>');
<span class="hl-gutter hl-gutter-addition">14 +</span> <span class="hl-addition"></span>
<span class="hl-gutter hl-gutter-addition">15 +</span> <span class="hl-addition"> <span class="hl-variable">$lines</span>[<span class="hl-variable">$i</span>] = <span class="hl-property">sprintf</span>(</span>
<span class="hl-gutter hl-gutter-addition">16 +</span> <span class="hl-addition"> <span class="hl-type">Escape</span>::<span class="hl-property">tokens</span>('<span class="hl-value">&lt;span class=&quot;%s&quot;&gt;%s&lt;/span&gt;%s</span>'),</span>
<span class="hl-gutter "> 17</span> <span class="hl-variable">$gutterClass</span>,
<span class="hl-gutter "> 18</span> <span class="hl-property">str_pad</span>(
<span class="hl-gutter"> 17</span> <span class="hl-variable">$gutterClass</span>,
<span class="hl-gutter"> 18</span> <span class="hl-property">str_pad</span>(
<span class="hl-gutter hl-gutter-deletion">19 -</span> <span class="hl-property">string</span>: <span class="hl-deletion"><span class="hl-variable">$gutterNumber</span></span>,
<span class="hl-gutter "> 20</span> <span class="hl-property">length</span>: <span class="hl-variable">$gutterWidth</span>,
<span class="hl-gutter "> 21</span> <span class="hl-property">pad_type</span>: <span class="hl-property">STR_PAD_LEFT</span>,
<span class="hl-gutter "> 22</span> ),
<span class="hl-gutter "> 23</span> <span class="hl-variable">$line</span>,
<span class="hl-gutter "> 24</span> );
<span class="hl-gutter "> 25</span> }
<span class="hl-gutter"> 20</span> <span class="hl-property">length</span>: <span class="hl-variable">$gutterWidth</span>,
<span class="hl-gutter"> 21</span> <span class="hl-property">pad_type</span>: <span class="hl-property">STR_PAD_LEFT</span>,
<span class="hl-gutter"> 22</span> ),
<span class="hl-gutter"> 23</span> <span class="hl-variable">$line</span>,
<span class="hl-gutter"> 24</span> );
<span class="hl-gutter"> 25</span> }
TXT;
$highlighter = (new Highlighter())->withGutter(10);

Expand All @@ -65,11 +65,11 @@ public function test_gutter_injection_one_single_line(): void
TXT;

$expected = <<<'TXT'
<span class="hl-gutter "> 10</span> <span class="hl-property">on</span>:
<span class="hl-gutter "> 11</span> <span class="hl-property">pull_request</span>:
<span class="hl-gutter "> 12</span> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review]
<span class="hl-gutter"> 10</span> <span class="hl-property">on</span>:
<span class="hl-gutter"> 11</span> <span class="hl-property">pull_request</span>:
<span class="hl-gutter"> 12</span> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review]
<span class="hl-gutter hl-gutter-addition">13 +</span> <span class="hl-addition"> <span class="hl-property">pull_request_target</span>: </span>
<span class="hl-gutter "> 14</span> <span class="hl-property">other</span>: foo
<span class="hl-gutter"> 14</span> <span class="hl-property">other</span>: foo
TXT;

$highlighter = (new Highlighter())->withGutter(10);
Expand All @@ -91,14 +91,14 @@ public function test_gutter_injection_many_single_lines(): void
TXT;

$expected = <<<'TXT'
<span class="hl-gutter "> 10</span> <span class="hl-property">on</span>:
<span class="hl-gutter "> 11</span> <span class="hl-property">pull_request</span>:
<span class="hl-gutter "> 12</span> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review]
<span class="hl-gutter"> 10</span> <span class="hl-property">on</span>:
<span class="hl-gutter"> 11</span> <span class="hl-property">pull_request</span>:
<span class="hl-gutter"> 12</span> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review]
<span class="hl-gutter hl-gutter-addition">13 +</span> <span class="hl-addition"> <span class="hl-property">pull_request_target</span>: </span>
<span class="hl-gutter hl-gutter-addition">14 +</span> <span class="hl-addition"> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review] </span>
<span class="hl-gutter hl-gutter-addition">15 +</span> <span class="hl-addition"> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review] </span>
<span class="hl-gutter hl-gutter-addition">16 +</span> <span class="hl-addition"> <span class="hl-property">types</span>: [opened, synchronize, reopened, ready_for_review] </span>
<span class="hl-gutter "> 17</span> <span class="hl-property">other</span>: foo
<span class="hl-gutter"> 17</span> <span class="hl-property">other</span>: foo
TXT;

$highlighter = (new Highlighter())->withGutter(10);
Expand Down
Loading