]> git.scottworley.com Git - paperdoorknob/blob - texfilter.py
Apply bare-\emph fix to \st too
[paperdoorknob] / texfilter.py
1 # paperdoorknob: Print glowfic
2 #
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.
6
7 import re
8
9 from typing import Callable, List, Tuple
10
11
12 TexFilters: List[Tuple[str, Callable[[bytes], bytes]]] = [
13 # Work around `Extra }, or forgotten \endgroup.`
14 ("FixBareNesting", lambda x: re.sub(
15 b'(^|\n)(\\\\(emph|st){)', b'\\1\\\\hspace{0pt}\\2', x)),
16 ]
17
18
19 class TexFilterError(Exception):
20 pass
21
22
23 def ApplyTexFilters(filter_list: str, data: bytes) -> bytes:
24 for filter_name in filter_list.split(','):
25 filters = [f for (name, f) in TexFilters if name == filter_name]
26 if len(filters) == 0:
27 raise TexFilterError(f"Unknown Tex filter: {filter_name}")
28 if len(filters) > 1:
29 raise TexFilterError(
30 f"Multiple Tex filters with the same name!?: {filter_name}")
31 data = filters[0](data)
32 return data