This commit is contained in:
parent
200a90c859
commit
1e8473eb7b
123
sync.py
123
sync.py
|
|
@ -425,6 +425,69 @@ def create_diff_html(title, en_diff, en_old_lines, en_new_lines, cn_content=None
|
|||
.replace('"', """)
|
||||
.replace("'", "'"))
|
||||
|
||||
def generate_inline_diff(old_text, new_text):
|
||||
"""生成GitHub风格的行内字符级diff"""
|
||||
if not old_text or not new_text:
|
||||
return html_escape(new_text or "")
|
||||
|
||||
escaped_old = html_escape(old_text)
|
||||
escaped_new = html_escape(new_text)
|
||||
|
||||
# 使用difflib进行字符级别的比较
|
||||
differ = difflib.SequenceMatcher(None, escaped_old, escaped_new)
|
||||
|
||||
result = []
|
||||
for tag, i1, i2, j1, j2 in differ.get_opcodes():
|
||||
if tag == 'equal':
|
||||
# 相同的部分
|
||||
result.append(escaped_new[j1:j2])
|
||||
elif tag == 'replace':
|
||||
# 替换的部分:删除的用红色背景,新增的用绿色背景
|
||||
deleted = escaped_old[i1:i2]
|
||||
added = escaped_new[j1:j2]
|
||||
result.append(f'<span class="diff-char-removed">{deleted}</span>')
|
||||
result.append(f'<span class="diff-char-added">{added}</span>')
|
||||
elif tag == 'delete':
|
||||
# 删除的部分用红色背景
|
||||
deleted = escaped_old[i1:i2]
|
||||
result.append(f'<span class="diff-char-removed">{deleted}</span>')
|
||||
elif tag == 'insert':
|
||||
# 新增的部分用绿色背景
|
||||
added = escaped_new[j1:j2]
|
||||
result.append(f'<span class="diff-char-added">{added}</span>')
|
||||
|
||||
return ''.join(result)
|
||||
|
||||
def generate_clean_new_content(old_text, new_text):
|
||||
"""生成干净的新内容,只显示新增部分的高亮,不包含删除部分"""
|
||||
if not old_text or not new_text:
|
||||
return html_escape(new_text or "")
|
||||
|
||||
escaped_old = html_escape(old_text)
|
||||
escaped_new = html_escape(new_text)
|
||||
|
||||
# 使用difflib进行字符级别的比较
|
||||
differ = difflib.SequenceMatcher(None, escaped_old, escaped_new)
|
||||
|
||||
result = []
|
||||
for tag, i1, i2, j1, j2 in differ.get_opcodes():
|
||||
if tag == 'equal':
|
||||
# 相同的部分
|
||||
result.append(escaped_new[j1:j2])
|
||||
elif tag == 'replace':
|
||||
# 替换的部分:只显示新增的内容(绿色高亮),跳过删除的内容
|
||||
added = escaped_new[j1:j2]
|
||||
result.append(f'<span class="diff-char-added">{added}</span>')
|
||||
elif tag == 'delete':
|
||||
# 删除的部分:跳过,不显示
|
||||
continue
|
||||
elif tag == 'insert':
|
||||
# 新增的部分用绿色背景
|
||||
added = escaped_new[j1:j2]
|
||||
result.append(f'<span class="diff-char-added">{added}</span>')
|
||||
|
||||
return ''.join(result)
|
||||
|
||||
# 生成HTML
|
||||
html = f'''<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
|
|
@ -535,8 +598,8 @@ def create_diff_html(title, en_diff, en_old_lines, en_new_lines, cn_content=None
|
|||
width: 400px;
|
||||
background-color: #f8f9fa;
|
||||
border-left: 1px solid #dee2e6;
|
||||
padding: 8px 12px;
|
||||
font-size: 12px;
|
||||
padding: 10px 14px;
|
||||
font-size: 14px;
|
||||
display: none;
|
||||
}}
|
||||
|
||||
|
|
@ -545,12 +608,12 @@ def create_diff_html(title, en_diff, en_old_lines, en_new_lines, cn_content=None
|
|||
}}
|
||||
|
||||
.annotation-item {{
|
||||
margin-bottom: 6px;
|
||||
padding: 6px 8px;
|
||||
margin-bottom: 8px;
|
||||
padding: 8px 10px;
|
||||
border-radius: 4px;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 11px;
|
||||
line-height: 1.4;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
}}
|
||||
|
||||
.annotation-item:last-child {{
|
||||
|
|
@ -593,14 +656,38 @@ def create_diff_html(title, en_diff, en_old_lines, en_new_lines, cn_content=None
|
|||
}}
|
||||
|
||||
.annotation-header {{
|
||||
font-size: 10px;
|
||||
font-size: 11px;
|
||||
color: #6c757d;
|
||||
margin-bottom: 4px;
|
||||
margin-bottom: 6px;
|
||||
font-weight: bold;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}}
|
||||
|
||||
/* GitHub风格的字符级diff样式 */
|
||||
.diff-char-added {{
|
||||
background-color: #acf2bd;
|
||||
color: #24292f;
|
||||
border-radius: 2px;
|
||||
padding: 1px 2px;
|
||||
}}
|
||||
|
||||
.diff-char-removed {{
|
||||
background-color: #ffd5d5;
|
||||
color: #24292f;
|
||||
border-radius: 2px;
|
||||
padding: 1px 2px;
|
||||
}}
|
||||
|
||||
/* 批注项内的字符级diff样式调整 */
|
||||
.annotation-item.removed {{
|
||||
text-decoration: none; /* 移除删除线 */
|
||||
}}
|
||||
|
||||
.annotation-item.replaced .old-content {{
|
||||
text-decoration: none; /* 移除删除线 */
|
||||
}}
|
||||
|
||||
/* 新页面提示 */
|
||||
.new-page-notice {{
|
||||
background-color: #d4edda;
|
||||
|
|
@ -696,18 +783,18 @@ def create_diff_html(title, en_diff, en_old_lines, en_new_lines, cn_content=None
|
|||
width: 400px;
|
||||
background-color: #f8f9fa;
|
||||
border-left: 1px solid #dee2e6;
|
||||
padding: 8px 12px;
|
||||
font-size: 12px;
|
||||
padding: 10px 14px;
|
||||
font-size: 14px;
|
||||
display: block;
|
||||
}}
|
||||
|
||||
.line-wrapper.blank-placeholder .annotation .annotation-item {{
|
||||
margin-bottom: 6px;
|
||||
padding: 6px 8px;
|
||||
margin-bottom: 8px;
|
||||
padding: 8px 10px;
|
||||
border-radius: 4px;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 11px;
|
||||
line-height: 1.4;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
}}
|
||||
|
||||
.line-wrapper.blank-placeholder .annotation .annotation-item:last-child {{
|
||||
|
|
@ -803,12 +890,12 @@ def create_diff_html(title, en_diff, en_old_lines, en_new_lines, cn_content=None
|
|||
html += f'<div>{escaped_change}</div>'
|
||||
html += '</div>'
|
||||
elif change['type'] == 'replaced':
|
||||
escaped_old = html_escape(change['old_content'])
|
||||
escaped_new = html_escape(change['new_content'])
|
||||
# 生成干净的新内容(只显示新增部分的高亮,不包含删除部分)
|
||||
clean_new_content = generate_clean_new_content(change['old_content'], change['new_content'])
|
||||
html += f'<div class="annotation-item replaced">'
|
||||
html += f'<div class="annotation-header">替换</div>'
|
||||
html += f'<div class="old-content">{escaped_old}</div>'
|
||||
html += f'<div class="new-content">{escaped_new}</div>'
|
||||
html += f'<div class="old-content">{html_escape(change["old_content"])}</div>'
|
||||
html += f'<div class="new-content">{clean_new_content}</div>'
|
||||
html += '</div>'
|
||||
html += '</div>'
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue