Spaces:
Sleeping
Sleeping
File size: 1,327 Bytes
6085b61 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | """Diff viewer component for showing before/after file changes."""
from __future__ import annotations
import difflib
def render_diff(old_text: str, new_text: str, filepath: str = "") -> str:
"""Return a syntax-highlighted unified diff as HTML."""
diff = difflib.unified_diff(
old_text.splitlines(keepends=True),
new_text.splitlines(keepends=True),
fromfile=f"a/{filepath}",
tofile=f"b/{filepath}",
)
lines = list(diff)
if not lines:
return "<p style='color:#6b7280;font-style:italic'>No changes.</p>"
html_parts = [
"<div style='font-family:monospace;font-size:13px;line-height:1.6;"
"background:#1e1e2e;color:#cdd6f4;padding:12px;border-radius:8px;"
"overflow-x:auto;white-space:pre'>"
]
for line in lines:
if line.startswith("---") or line.startswith("+++"):
cls = "color:#6c7086;font-weight:600"
elif line.startswith("@@"):
cls = "color:#f5c2e7;font-weight:600"
elif line.startswith("+"):
cls = "color:#a6e3a1"
elif line.startswith("-"):
cls = "color:#f38ba8"
else:
cls = "color:#cdd6f4"
html_parts.append(f"<div style='{cls}'>{line.rstrip()}</div>")
html_parts.append("</div>")
return "".join(html_parts)
|