]> git.scottworley.com Git - paperdoorknob/blob - htmlfilter.py
Handle Unicode characters ≈ and ◁
[paperdoorknob] / htmlfilter.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
8 import re
9
10 from typing import Callable, List, Tuple
11
12
13 HTMLFilters: List[Tuple[str, Callable[[bytes], bytes]]] = [
14 ("NoNBSP", lambda x: re.sub(b"( |\xc2\xa0)", b" ", x)),
15 ]
16
17
18 class HTMLFilterError(Exception):
19 pass
20
21
22 def ApplyHTMLFilters(filter_list: str, data: bytes) -> bytes:
23 for filter_name in filter_list.split(','):
24 filters = [f for (name, f) in HTMLFilters if name == filter_name]
25 if len(filters) == 0:
26 raise HTMLFilterError(f"Unknown HTML filter: {filter_name}")
27 if len(filters) > 1:
28 raise HTMLFilterError(
29 f"Multiple HTML filters with the same name!?: {filter_name}")
30 data = filters[0](data)
31 return data