cz-base / diff_text.py
james-d-taboola's picture
feat: modify separator text and password ui
379b4ed
# from difflib import unified_diff
from difflib import SequenceMatcher
SEPERATOR = "\n\n\n=====以下為教案變更內容=====\n\n\n"
def compare_text(text1, text2):
"""
Compare two text strings and return their differences in a human-readable format.
Args:
text1 (str): Original text string
text2 (str): New text string
Returns:
str: Human-readable formatted differences
"""
if not text1:
return text2
if text1 == text2:
return text2
# Split the texts into lines
text1_lines = text1.splitlines()
text2_lines = text2.splitlines()
# Create a more readable diff output
output = []
# Add the full text2 content
output.append(text2)
# Add a separator line
output.append(SEPERATOR)
matcher = SequenceMatcher(None, text1_lines, text2_lines)
for op, i1, i2, j1, j2 in matcher.get_opcodes():
# if op == 'equal':
# # For unchanged sections, just add them without markers
# for line in text2_lines[j1:j2]:
# output.append(line)
if op == 'replace':
# Show both removed and added content
output.append("\n【修改前】")
for line in text1_lines[i1:i2]:
output.append(f"- {line}")
output.append("\n【修改後】")
for line in text2_lines[j1:j2]:
output.append(f"+ {line}")
elif op == 'delete':
# Show removed content
output.append("\n【已移除】")
for line in text1_lines[i1:i2]:
output.append(f"- {line}")
elif op == 'insert':
# Show added content
output.append("\n【新增內容】")
for line in text2_lines[j1:j2]:
output.append(f"+ {line}")
return "\n".join(output)
def extract_modified_sections(compared_text):
"""
Extract the part of the compared text that appears before the SEPERATOR line.
Args:
compared_text (str): The text returned by compare_text function
Returns:
str: The content before the SEPERATOR line
"""
if not compared_text:
return ""
# Split by the separator and return the first part
parts = compared_text.split(SEPERATOR, 1)
if len(parts) > 0:
return parts[0]
return ""