]> git.scottworley.com Git - paperdoorknob/blobdiff - htmlfilter.py
Strip all  
[paperdoorknob] / htmlfilter.py
diff --git a/htmlfilter.py b/htmlfilter.py
new file mode 100644 (file)
index 0000000..9bc9b6f
--- /dev/null
@@ -0,0 +1,31 @@
+# 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
+
+
+HTMLFilters: List[Tuple[str, Callable[[bytes], bytes]]] = [
+    ("NoNBSP", lambda x: re.sub(b"( |\xc2\xa0)", b" ", x)),
+]
+
+
+class HTMLFilterError(Exception):
+    pass
+
+
+def ApplyHTMLFilters(filter_list: str, data: bytes) -> bytes:
+    for filter_name in filter_list.split(','):
+        filters = [f for (name, f) in HTMLFilters if name == filter_name]
+        if len(filters) == 0:
+            raise HTMLFilterError(f"Unknown HTML filter: {filter_name}")
+        if len(filters) > 1:
+            raise HTMLFilterError(
+                f"Multiple HTML filters with the same name!?: {filter_name}")
+        data = filters[0](data)
+    return data