diff options
author | jaseg <git-bigdata-wsl-arch@jaseg.de> | 2021-07-12 14:01:13 +0200 |
---|---|---|
committer | jaseg <git-bigdata-wsl-arch@jaseg.de> | 2021-07-12 14:01:13 +0200 |
commit | 4019ad514b68e44951b0a1d31316e26877467175 (patch) | |
tree | 19b46c104e610b4b4473bb7ef975aab422abbbe4 | |
parent | 7ea8300468e24cdf9756fee90fcecc1b08d845f6 (diff) | |
download | ihsm-4019ad514b68e44951b0a1d31316e26877467175.tar.gz ihsm-4019ad514b68e44951b0a1d31316e26877467175.tar.bz2 ihsm-4019ad514b68e44951b0a1d31316e26877467175.zip |
Add diff pdf generator
-rw-r--r-- | paper/Makefile | 8 | ||||
-rw-r--r-- | paper/diffinator.py | 78 |
2 files changed, 86 insertions, 0 deletions
diff --git a/paper/Makefile b/paper/Makefile index 1118e71..eb94566 100644 --- a/paper/Makefile +++ b/paper/Makefile @@ -8,6 +8,8 @@ SHELL := bash MAKEFLAGS += --warn-undefined-variables MAKEFLAGS += --no-builtin-rules +DIFF_VERSION ?= v2.0 + main_tex ?= ihsm_paper brief_tex ?= ihsm_tech_report @@ -15,6 +17,12 @@ VERSION_STRING := $(shell git describe --tags --long --dirty) all: ${main_tex}.pdf ${brief_tex}.pdf +.PHONY: diff +diff: ihsm_paper_diff.pdf + +ihsm_paper_diff.tex: ihsm_paper.tex ihsm.bib + python3 diffinator.py $^ $(DIFF_VERSION) > $@ + %.pdf: %.tex ihsm.bib version.tex pdflatex -shell-escape $< biber $* diff --git a/paper/diffinator.py b/paper/diffinator.py new file mode 100644 index 0000000..2972d47 --- /dev/null +++ b/paper/diffinator.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +import re +import subprocess + +import click + + +@click.command() +@click.argument('texfile') +@click.argument('bibliography') +@click.argument('revision') +def generate_git_tex_diff(texfile, bibliography, revision): + with open(texfile) as f: + tex_lines = len(list(f)) + with open(bibliography) as f: + bib_lines = len(list(f)) + + tex_proc = subprocess.run(['git', 'diff', f'-U{tex_lines+1}', '--word-diff', '--color=always', revision, texfile], + check=True, capture_output=True) + + bib_proc = subprocess.run(['git', 'diff', f'-U{bib_lines+1}', '--word-diff', '--color=always', revision, bibliography], + check=True, capture_output=True) + + addition_re = re.compile('\033\\[32m\\{\\+(.*?)\\+\\}\033\\[m') + deletion_re = re.compile('\033\\[31m\\[-(.*?)\\-]\033\\[m') + csi_re = re.compile('\033\\[.*?m') + bibtex_entry_def_re = re.compile('@.*?{(.*?),') + + bibliography_categories = '\\DeclareBibliographyCategory{diff_new_entry}\n' + bibliography_categories += '\\DeclareBibliographyCategory{diff_deleted_entry}\n' + bibliography_categories += '\\AtEveryBibitem{\\ifcategory{diff_new_entry}{\\color{diffgreen}}{\\ifcategory{diff_deleted_entry}{\\color{diffred}}{\\color{black}}}}\n' + + added_entries, removed_entries = [], [] + for line in bib_proc.stdout.decode().splitlines(): + if (match := addition_re.fullmatch(line.strip())): + if (entry_def := bibtex_entry_def_re.fullmatch(match.group(1))): + added_entries.append(entry_def.group(1)) + + if (match := deletion_re.fullmatch(line.strip())): + if (entry_def := bibtex_entry_def_re.fullmatch(match.group(1))): + removed_entries.append(entry_def.group(1)) + + if added_entries: + bibliography_categories += '\\addtocategory{diff_new_entry}{' + ','.join(added_entries) + '}\n' + if removed_entries: + bibliography_categories += '\\addtocategory{diff_deleted_entry}{' + ','.join(removed_entries) + '}\n' + + content_started = False + document_started = False + for line in tex_proc.stdout.decode().splitlines(): + + if not content_started: + if '@@' in line: + content_started = True + continue + + line = line.rstrip() + if document_started: # diff results in preamble + line = addition_re.sub(r'\\color{diffgreen}\1\\color{black}', line) + line = deletion_re.sub(r'\\color{diffred}\1\\color{black}', line) + + else: + if '\\begin{document}' in line: + document_started = True + print(bibliography_categories) + print('\\definecolor{diffgreen}{HTML}{1e8449}') + print('\\definecolor{diffred}{HTML}{cb4335}') + + + line = addition_re.sub(r'\1', line) + line = deletion_re.sub(r'\1', line) + + line = csi_re.sub('', line) + print(line) + +if __name__ == '__main__': + generate_git_tex_diff() |