]> git.scottworley.com Git - paperdoorknob/blame - texfilter.py
Handle Unicode characters ≈ and ◁
[paperdoorknob] / texfilter.py
CommitLineData
131deef1
SW
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
7import re
8
9from typing import Callable, List, Tuple
10
11
12TexFilters: List[Tuple[str, Callable[[bytes], bytes]]] = [
13 # Work around `Extra }, or forgotten \endgroup.`
87fb552d
SW
14 ("FixBareNesting", lambda x: re.sub(
15 b'(^|\n)(\\\\(emph|st){)', b'\\1\\\\hspace{0pt}\\2', x)),
30b145cc
SW
16 ("UnicodeApprox", lambda x: re.sub('≈'.encode('utf-8'), b'$\\approx$', x)),
17 ("UnicodeTriangle", lambda x: re.sub('◁'.encode('utf-8'), b'$\\lhd$', x)),
131deef1
SW
18]
19
20
21class TexFilterError(Exception):
22 pass
23
24
25def ApplyTexFilters(filter_list: str, data: bytes) -> bytes:
26 for filter_name in filter_list.split(','):
27 filters = [f for (name, f) in TexFilters if name == filter_name]
28 if len(filters) == 0:
29 raise TexFilterError(f"Unknown Tex filter: {filter_name}")
30 if len(filters) > 1:
31 raise TexFilterError(
32 f"Multiple Tex filters with the same name!?: {filter_name}")
33 data = filters[0](data)
34 return data