From 07f9b178a58e87f90aa7ab1e459c17561345154d Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Fri, 1 Dec 2023 01:01:21 -0800 Subject: [PATCH] LaTeX output is a valid LaTeX file --- default.nix | 6 ++++-- paperdoorknob.py | 2 ++ paperdoorknob_test.py | 23 ++++++++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/default.nix b/default.nix index 58f9886..3e02638 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,8 @@ { pkgs ? import { }, lint ? false }: pkgs.python3Packages.callPackage ({ lib, buildPythonPackage, makeWrapper , autopep8, mypy, pylint, beautifulsoup4, requests, requests-cache - , types-beautifulsoup4, types-requests, xdg-base-dirs, pandoc-cli }: + , texliveBasic, types-beautifulsoup4, types-requests, xdg-base-dirs + , pandoc-cli }: buildPythonPackage rec { pname = "paperdoorknob"; version = "0.0.1"; @@ -9,7 +10,8 @@ pkgs.python3Packages.callPackage ({ lib, buildPythonPackage, makeWrapper nativeBuildInputs = [ makeWrapper ]; propagatedBuildInputs = [ beautifulsoup4 requests requests-cache xdg-base-dirs ]; - nativeCheckInputs = [ mypy pandoc-cli types-beautifulsoup4 types-requests ] + nativeCheckInputs = + [ mypy pandoc-cli texliveBasic types-beautifulsoup4 types-requests ] ++ lib.optionals lint [ autopep8 pylint ]; doCheck = true; checkPhase = "./test.sh"; diff --git a/paperdoorknob.py b/paperdoorknob.py index 32daf4f..8ab62f1 100644 --- a/paperdoorknob.py +++ b/paperdoorknob.py @@ -84,9 +84,11 @@ def process( texout: IO[bytes], pandoc: str, ) -> None: + texout.write(b'\\documentclass{article}\n\\begin{document}\n') html = clean(fetch(url, session, timeout)) for r in replies(html): texout.write(html_to_tex(pandoc, r)) + texout.write(b'\\end{document}\n') def main() -> None: diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index ee4de04..9ea877d 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -7,6 +7,7 @@ import unittest import io +import subprocess import threading from http.server import BaseHTTPRequestHandler, HTTPServer import requests @@ -129,9 +130,25 @@ class TestFetch(unittest.TestCase): TIMEOUT, buf, 'pandoc') - self.assertEqual( - buf.getvalue(), - b'This is glowfic\nYou \\emph{sure}?\nPretty sure.\n') + self.assertEqual(buf.getvalue(), b'''\\documentclass{article} +\\begin{document} +This is glowfic +You \\emph{sure}? +Pretty sure. +\\end{document} +''') + + def testPDF(self) -> None: + with requests.session() as s: + with open("test.tex", 'wb') as out: + paperdoorknob.process( + f"http://localhost:{self._port()}", + s, + TIMEOUT, + out, + 'pandoc') + subprocess.run(['pdflatex', 'test.tex'], + stdin=subprocess.DEVNULL, check=True) if __name__ == '__main__': -- 2.44.1