from argparse import ArgumentParser
import itertools
import os.path
-import subprocess
from typing import IO, Iterable
from xdg_base_dirs import xdg_cache_home
from fetch import CachingFetcher, Fetcher
+from texify import PandocTexifier, Texifier
def command_line_parser() -> ArgumentParser:
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,
fetcher: Fetcher,
- texout: IO[bytes],
- pandoc: str) -> None:
+ texifier: Texifier,
+ texout: IO[bytes]) -> None:
texout.write(b'\\documentclass{article}\n\\begin{document}\n')
html = clean(parse(fetcher.fetch(url)))
for r in replies(html):
- texout.write(html_to_tex(pandoc, r))
+ texout.write(texifier.texify(r))
texout.write(b'\\end{document}\n')
def main() -> None:
args = command_line_parser().parse_args()
+ texifier = PandocTexifier(args.pandoc or 'pandoc')
with CachingFetcher(args.cache_path, args.timeout) as fetcher:
with open(args.out + '.tex', 'wb') as texout:
- process(args.url, fetcher, texout, args.pandoc or 'pandoc')
+ process(args.url, fetcher, texifier, texout)
if __name__ == '__main__':
import paperdoorknob
from testing.fakeserver import FakeGlowficServer
from fetch import DirectFetcher
+from texify import PandocTexifier
TIMEOUT = 8
["This is glowfic", "You sure?", "Pretty sure."])
def testProcess(self) -> None:
+ texifier = PandocTexifier('pandoc')
with DirectFetcher(TIMEOUT) as f:
buf = io.BytesIO()
paperdoorknob.process(
- f"http://localhost:{self._port}", f, buf, 'pandoc')
+ f"http://localhost:{self._port}", f, texifier, buf)
self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
\\begin{document}
This is glowfic
''')
def testPDF(self) -> None:
+ texifier = PandocTexifier('pandoc')
with DirectFetcher(TIMEOUT) as f:
with open("test.tex", 'wb') as out:
paperdoorknob.process(
- f"http://localhost:{self._port}", f, out, 'pandoc')
+ f"http://localhost:{self._port}", f, texifier, out)
subprocess.run(['pdflatex', 'test.tex'],
stdin=subprocess.DEVNULL, check=True)
py_modules=[
'fetch',
'paperdoorknob',
+ 'texify',
],
license="GPL-3.0",
entry_points={'console_scripts': ['paperdoorknob = paperdoorknob:main']},
--- /dev/null
+# paperdoorknob: Print glowfic
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation, version 3.
+
+
+from abc import ABC, abstractmethod
+import subprocess
+
+from bs4.element import Tag
+
+
+class Texifier(ABC):
+ @abstractmethod
+ def texify(self, html: Tag) -> bytes:
+ raise NotImplementedError()
+
+
+class PandocTexifier(Texifier):
+
+ def __init__(self, pandoc_path: str) -> None:
+ self._pandoc_path = pandoc_path
+
+ def texify(self, html: Tag) -> bytes:
+ return subprocess.run([self._pandoc_path, '--from=html', '--to=latex'],
+ input=html.encode(),
+ stdout=subprocess.PIPE,
+ check=True).stdout