]> git.scottworley.com Git - paperdoorknob/blob - domfilter.py
22f96a13b23c51c02229b5a3eaf5740d37bf8303
[paperdoorknob] / domfilter.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 from typing import Any, Callable, List, Tuple
9
10 from bs4.element import Tag
11
12
13 DOMFilters: List[Tuple[str, Callable[[Tag], Any]]] = [
14 ("NoEdit", lambda x: [eb.decompose() for eb in x.find_all("div", class_="post-edit-box")]),
15 ("NoFooter", lambda x: [foot.decompose() for foot in x.find_all("div", class_="post-footer")]),
16 ]
17
18
19 class DOMFilterError(Exception):
20 pass
21
22
23 def ApplyDOMFilters(filter_list: str, tag: Tag) -> None:
24 for filter_name in filter_list.split(','):
25 filters = [f for (name, f) in DOMFilters if name == filter_name]
26 if len(filters) == 0:
27 raise DOMFilterError(f"Unknown DOM filter: {filter_name}")
28 if len(filters) > 1:
29 raise DOMFilterError(
30 f"Multiple DOM filters with the same name!?: {filter_name}")
31 filters[0](tag)