diff options
Diffstat (limited to 'paper/diffinator.py')
-rw-r--r-- | paper/diffinator.py | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/paper/diffinator.py b/paper/diffinator.py index 00a5b4f..a54e844 100644 --- a/paper/diffinator.py +++ b/paper/diffinator.py @@ -22,8 +22,8 @@ def generate_git_tex_diff(texfile, bibliography, revision): bib_proc = subprocess.run(['git', 'diff', f'-U{bib_lines+1}', '--word-diff', '--color=always', revision, bibliography], check=True, capture_output=True) - ADDITION_RE_R = '\033\\[32m\\{\\+(.*?)([^\\\\]%.*?)?\\+\\}\033\\[m' - DELETION_RE_R = '\033\\[31m\\[-(.*?)([^\\\\]%.*?)?\\-]\033\\[m' + ADDITION_RE_R = '\033\\[32m\\{\\+([^\033]*?)([^\\\\]%.*?)?\\+\\}\033\\[m' + DELETION_RE_R = '\033\\[31m\\[-([^\033]*?)([^\\\\]%.*?)?\\-]\033\\[m' addition_re = re.compile(ADDITION_RE_R) deletion_re = re.compile(DELETION_RE_R) combined_re = re.compile(f'{DELETION_RE_R}{ADDITION_RE_R}') @@ -61,8 +61,16 @@ def generate_git_tex_diff(texfile, bibliography, revision): line = line.rstrip() if document_started: # diff results in preamble + import sys + debug = 'battery' in line + if debug: + print('orig:', repr(line), file=sys.stderr) + def suppress_small_changes(match): + nonlocal debug old, _1, new, _2 = match.groups() + if debug: + print(f'old={repr(old)}, new={repr(new)}', file=sys.stderr) if len(old) < 12 and len(new) < 12: return new @@ -70,24 +78,48 @@ def generate_git_tex_diff(texfile, bibliography, revision): if old.count(' ') < 3 and new.count(' ') < 3: return new + if '}' in old or '{' in old or '{' in new or '}' in new: + return new + new_chars = list(new) for char in old: if char not in string.ascii_letters: continue if char not in new_chars: - return match.group(0) # return original text + return r' \color{diffred}' + old + r' \color{diffgreen}' + new + ' \color{black}' new_chars.remove(char) if any(char in string.ascii_letters for char in new_chars): - return match.group(0) # return original text + return r' \color{diffred}' + old + r' \color{diffgreen}' + new + ' \color{black}' return new line = combined_re.sub(suppress_small_changes, line) - line = addition_re.sub(r' \\color{diffgreen}\1 \\color{black}', line) - line = deletion_re.sub(r' \\color{diffred}\1 \\color{black}', line) + if debug: + print('[1]', line, file=sys.stderr) + + def suppress_small_changes(match, action): + change = match.group(1) + + if len(change) < 12 or change.count(' ') < 3 or '}' in change or '{' in change: + if action == 'addition': + return change + else: # deletion + return '' + + if action == 'addition': + return r' \color{diffgreen}' + change + r' \color{black}' + else: # deletion + return r' \color{diffred}' + change + r' \color{black}' + + line = addition_re.sub(lambda match: suppress_small_changes(match, 'addition'), line) + if debug: + print('[2]', line, file=sys.stderr) + line = deletion_re.sub(lambda match: suppress_small_changes(match, 'deletion'), line) + if debug: + print('[3]', line, file=sys.stderr) else: if '\\begin{document}' in line: |