]> git.scottworley.com Git - paperdoorknob/commitdiff
Invoke pandoc to convert HTML to LaTeX
authorScott Worley <scottworley@scottworley.com>
Thu, 30 Nov 2023 16:26:35 +0000 (08:26 -0800)
committerScott Worley <scottworley@scottworley.com>
Wed, 20 Dec 2023 01:37:10 +0000 (17:37 -0800)
default.nix
paperdoorknob.py
paperdoorknob_test.py

index 6bbed2273e21a61fed51aeda480968a10b06840a..58f98862f305518503a2d8ee8896f882bb109897 100644 (file)
@@ -9,7 +9,7 @@ pkgs.python3Packages.callPackage ({ lib, buildPythonPackage, makeWrapper
     nativeBuildInputs = [ makeWrapper ];
     propagatedBuildInputs =
       [ beautifulsoup4 requests requests-cache xdg-base-dirs ];
     nativeBuildInputs = [ makeWrapper ];
     propagatedBuildInputs =
       [ beautifulsoup4 requests requests-cache xdg-base-dirs ];
-    nativeCheckInputs = [ mypy types-beautifulsoup4 types-requests ]
+    nativeCheckInputs = [ mypy pandoc-cli types-beautifulsoup4 types-requests ]
       ++ lib.optionals lint [ autopep8 pylint ];
     doCheck = true;
     checkPhase = "./test.sh";
       ++ lib.optionals lint [ autopep8 pylint ];
     doCheck = true;
     checkPhase = "./test.sh";
index a31b659d2d403c67e0a44d4e32ebfd218168cc1d..32daf4f4d9372b07ee1e5eb801037ea2efa129ad 100644 (file)
@@ -8,6 +8,7 @@
 from argparse import ArgumentParser
 import itertools
 import os.path
 from argparse import ArgumentParser
 import itertools
 import os.path
+import subprocess
 
 from typing import IO, Iterable
 
 
 from typing import IO, Iterable
 
@@ -69,22 +70,35 @@ def replies(html: BeautifulSoup) -> Iterable[Tag]:
     return itertools.chain([text()], the_replies())
 
 
     return itertools.chain([text()], the_replies())
 
 
+def html_to_tex(pandoc: str, tag: Tag) -> bytes:
+    return subprocess.run([pandoc, '--from=html', '--to=latex'],
+                          input=tag.encode(),
+                          stdout=subprocess.PIPE,
+                          check=True).stdout
+
+
 def process(
         url: str,
         session: requests.Session,
         timeout: int,
 def process(
         url: str,
         session: requests.Session,
         timeout: int,
-        texout: IO[str],
+        texout: IO[bytes],
+        pandoc: str,
 ) -> None:
     html = clean(fetch(url, session, timeout))
 ) -> None:
     html = clean(fetch(url, session, timeout))
-    replies(html)
-    print("soon", file=texout)
+    for r in replies(html):
+        texout.write(html_to_tex(pandoc, r))
 
 
 def main() -> None:
     args = command_line_parser().parse_args()
     with requests_cache.CachedSession(args.cache_path, cache_control=True) as session:
 
 
 def main() -> None:
     args = command_line_parser().parse_args()
     with requests_cache.CachedSession(args.cache_path, cache_control=True) as session:
-        with open(args.out + '.tex', 'w', encoding='UTF-8') as texout:
-            process(args.url, session, args.timeout, texout)
+        with open(args.out + '.tex', 'wb') as texout:
+            process(
+                args.url,
+                session,
+                args.timeout,
+                texout,
+                args.pandoc or 'pandoc')
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':
index 363751436325a04a7007eaac7163b2f8edef17db..7cb73b7bcd7ee920711a0a846e40481c36c7b4ff 100644 (file)
@@ -6,6 +6,7 @@
 
 
 import unittest
 
 
 import unittest
+import io
 import threading
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import requests
 import threading
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import requests
@@ -119,6 +120,18 @@ class TestFetch(unittest.TestCase):
                 paperdoorknob.fetch(
                     f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
 
                 paperdoorknob.fetch(
                     f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
 
+    def testProcess(self) -> None:
+        with requests.session() as s:
+            buf = io.BytesIO()
+            paperdoorknob.process(
+                f"http://localhost:{self._port()}",
+                s,
+                TIMEOUT,
+                buf,
+                'pandoc')
+            self.assertEqual(buf.getvalue(),
+                             b'This is glowfic\nYou sure?\nPretty sure.\n')
+
 
 if __name__ == '__main__':
     unittest.main()
 
 if __name__ == '__main__':
     unittest.main()