Skip to content

Commit 35e37b1

Browse files
authored
SimpleDiff: Add method renderDiff() for return HTML code with changes.
1 parent 0573fa0 commit 35e37b1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/SimpleDiff.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ public function compare(string $left, string $right, bool $strict = false): Diff
4646
}
4747

4848

49+
public function renderDiff(Diff|string $diff): string
50+
{
51+
$return = [];
52+
foreach (explode("\n", is_string($diff) ? $diff : $diff->getDiff()) as $line) {
53+
if (($line[0] ?? '') === '+') {
54+
$return[] = '<div style="background:#a2f19c">' . htmlspecialchars($line) . '</div>';
55+
} elseif (($line[0] ?? '') === '-') {
56+
$return[] = '<div style="background:#e7acac">' . htmlspecialchars($line) . '</div>';
57+
} else {
58+
$return[] = '<div>' . htmlspecialchars($line) . '</div>';
59+
}
60+
}
61+
62+
return '<pre class="code">' . implode("\n", $return) . '</pre>';
63+
}
64+
65+
4966
private function prettyRender(string $haystack): string
5067
{
5168
return str_replace(["\t", ' '], ['→→→→', '·'], $haystack);

0 commit comments

Comments
 (0)