# -*- coding: utf-8 -*- """Guard against mangled TeX control sequences. Shell heredocs silently turn a backslash escape into the control character it names, so \times becomes a tab followed by "imes" and \ref becomes a carriage return followed by "ef". LaTeX compiles both without an error and prints the wreckage, so the build log cannot catch this. A second failure mode has the same property. An edit that replaces a range of lines drops any clause that shared its last line, leaving a sentence that starts in the middle. That also compiles and prints. Both scans are here. """ import re import sys from pathlib import Path TEX = Path(__file__).resolve().parents[1] / "main.tex" CTRL = {"\t": "TAB", "\r": "CR", "\x08": "BS", "\x0c": "FF", "\x07": "BEL", "\x0b": "VT", "\x00": "NUL"} # a macro name shorn of its first letter, which is what the escape ate STUBS = ["ef{", "abel{", "ite{", "extbf{", "extit{", "ext{", "imes", "rac{", "eft(", "ight)", "ho_", "elta", "psilon", "ambda", "igma", "ewline", "otag", "uad", "nderline", "ag{", "egin{", "nd{", "aption{", "ilde{", "ar{", "at{", "ec{"] PAT = re.compile("(?