]> git.scottworley.com Git - paperdoorknob/blobdiff - paperdoorknob.py
LaTeX output is a valid LaTeX file
[paperdoorknob] / paperdoorknob.py
index a31b659d2d403c67e0a44d4e32ebfd218168cc1d..8ab62f10b655f6b9f7c21cc4dd5a1f5379d066be 100644 (file)
@@ -8,6 +8,7 @@
 from argparse import ArgumentParser
 import itertools
 import os.path
+import subprocess
 
 from typing import IO, Iterable
 
@@ -69,22 +70,37 @@ def replies(html: BeautifulSoup) -> Iterable[Tag]:
     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,
-        texout: IO[str],
+        texout: IO[bytes],
+        pandoc: str,
 ) -> None:
+    texout.write(b'\\documentclass{article}\n\\begin{document}\n')
     html = clean(fetch(url, session, timeout))
-    replies(html)
-    print("soon", file=texout)
+    for r in replies(html):
+        texout.write(html_to_tex(pandoc, r))
+    texout.write(b'\\end{document}\n')
 
 
 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__':