]> git.scottworley.com Git - paperdoorknob/blobdiff - texfilter.py
texfilter to work around \emph nesting issue
[paperdoorknob] / texfilter.py
diff --git a/texfilter.py b/texfilter.py
new file mode 100644 (file)
index 0000000..8103c19
--- /dev/null
@@ -0,0 +1,32 @@
+# 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.
+
+import re
+
+from typing import Callable, List, Tuple
+
+
+TexFilters: List[Tuple[str, Callable[[bytes], bytes]]] = [
+    # Work around `Extra }, or forgotten \endgroup.`
+    ("FixBareEmph", lambda x: re.sub(
+        b'(^|\n)(\\\\emph{)', b'\\1\\\\hspace{0pt}\\2', x)),
+]
+
+
+class TexFilterError(Exception):
+    pass
+
+
+def ApplyTexFilters(filter_list: str, data: bytes) -> bytes:
+    for filter_name in filter_list.split(','):
+        filters = [f for (name, f) in TexFilters if name == filter_name]
+        if len(filters) == 0:
+            raise TexFilterError(f"Unknown Tex filter: {filter_name}")
+        if len(filters) > 1:
+            raise TexFilterError(
+                f"Multiple Tex filters with the same name!?: {filter_name}")
+        data = filters[0](data)
+    return data