X-Git-Url: http://git.scottworley.com/paperdoorknob/blobdiff_plain/929db57622c6b8756ed341d7ae2862126a7c638f..8be20b9d36c49271ed392038750dfba981c283b6:/domfilter.py?ds=sidebyside diff --git a/domfilter.py b/domfilter.py new file mode 100644 index 0000000..22f96a1 --- /dev/null +++ b/domfilter.py @@ -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. + + +from typing import Any, Callable, List, Tuple + +from bs4.element import Tag + + +DOMFilters: List[Tuple[str, Callable[[Tag], Any]]] = [ + ("NoEdit", lambda x: [eb.decompose() for eb in x.find_all("div", class_="post-edit-box")]), + ("NoFooter", lambda x: [foot.decompose() for foot in x.find_all("div", class_="post-footer")]), +] + + +class DOMFilterError(Exception): + pass + + +def ApplyDOMFilters(filter_list: str, tag: Tag) -> None: + for filter_name in filter_list.split(','): + filters = [f for (name, f) in DOMFilters if name == filter_name] + if len(filters) == 0: + raise DOMFilterError(f"Unknown DOM filter: {filter_name}") + if len(filters) > 1: + raise DOMFilterError( + f"Multiple DOM filters with the same name!?: {filter_name}") + filters[0](tag)