]>
git.scottworley.com Git - paperdoorknob/blob - texify.py
1 # paperdoorknob: Print glowfic
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
8 from abc
import ABC
, abstractmethod
12 from bs4
.element
import Tag
17 def texify(self
, html
: Tag
) -> bytes:
18 raise NotImplementedError()
21 class PandocTexifier(Texifier
):
23 def __init__(self
, pandoc_path
: str) -> None:
24 self
._pandoc
_path
= pandoc_path
26 def texify(self
, html
: Tag
) -> bytes:
27 return subprocess
.run([self
._pandoc
_path
, '--from=html', '--to=latex'],
29 stdout
=subprocess
.PIPE
,
33 class TexifierError(Exception):
37 class DirectTexifier(Texifier
):
38 def _texify_children(self
, html
: Tag
) -> bytes:
40 for c
in html
.children
:
41 if isinstance(c
, str):
42 out
+= c
.encode('UTF-8')
43 elif isinstance(c
, Tag
):
44 out
+= self
.texify(c
).strip()
46 raise TexifierError(f
"Unsupported PageElement: {type(c)}")
47 return re
.sub(b
'[ \n]+', b
' ', out
).strip() + b
'\n'
49 def texify(self
, html
: Tag
) -> bytes:
51 return b
'\\emph{' + self._texify_children(html).strip() + b'}\n'
52 if html
.name
== 'a' and 'href' in html
.attrs
:
53 return b
'\\href{' + html
.attrs
['href'].encode(
54 'UTF-8') + b
'}{' + self._texify_children(html).strip() + b'}\n'
55 return self
._texify
_children
(html
)
58 class TexifierVerificationError(Exception):
62 class VerifyingTexifier(Texifier
):
63 def __init__(self
, a
: Texifier
, b
: Texifier
) -> None:
67 def texify(self
, html
: Tag
) -> bytes:
68 aout
= self
._a
.texify(html
)
69 bout
= self
._b
.texify(html
)
71 raise TexifierVerificationError(aout
, bout
)