def compare_output(tex: str): r""" This checks whether plastex produces the same output as tex. This only works for things with simple plain text output, and is intended to be used for testing primitives such as \let, \def, \expandafter etc. """ cwd = os.getcwd() try: plastex_out = TeX().input(tex).parse().textContent with TemporaryDirectory() as tmpdir: os.chdir(tmpdir) if r'\documentclass{article}' not in tex: tex = r'\documentclass{article}\begin{document}' + tex + r'\end{document}' tex = r'\nonstopmode\AtBeginDocument{\thispagestyle{empty}}' + tex with open("test.tex", "w") as f: f.write(tex) subprocess.run(["pdflatex", "test.tex"], check=True) out = subprocess.run([ "gs", "-q", "-sDEVICE=txtwrite", "-o", "%stdout%", "test.pdf" ], check=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, universal_newlines=True) tex_out = out.stdout assert plastex_out.strip() == tex_out.strip() finally: os.chdir(cwd)
def compare_output(tex: str): r""" This checks whether plastex produces the same output as tex. This only works for things with simple plain text output, and is intended to be used for testing primitives such as \let, \def, \expandafter etc. """ if not CACHE_LOCATION.is_dir(): CACHE_LOCATION.mkdir() cwd = os.getcwd() try: plastex_out = TeX().input(tex).parse().textContent.strip() tex_hash = md5(tex.encode('utf-8')).hexdigest() tex_out = None try: tex_out = (CACHE_LOCATION / tex_hash).read_text() except IOError: with TemporaryDirectory() as tmpdir: os.chdir(tmpdir) if r'\documentclass{article}' not in tex: tex = r'\documentclass{article}\usepackage{microtype}\DisableLigatures{encoding = *, family = *}\begin{document}' + tex + r'\end{document}' tex = r'\nonstopmode\AtBeginDocument{\thispagestyle{empty}}' + tex with open("test.tex", "w") as f: f.write(tex) subprocess.run(["pdflatex", "test.tex"], check=True) if not os.path.exists("test.pdf"): # If pdflatex was successful but a pdf file was not produced, # this means 0 pages were produced, so the content is empty. return plastex_out.strip() == "" out = subprocess.run([ "gs", "-q", "-sDEVICE=txtwrite", "-o", "%stdout%", "test.pdf" ], check=True, stdout=subprocess.PIPE, stdin=subprocess.DEVNULL, universal_newlines=True) tex_out = out.stdout.strip() (CACHE_LOCATION / tex_hash).write_text(tex_out) assert plastex_out == tex_out, ('%r != %r ' % (plastex_out, tex_out)) finally: os.chdir(cwd)