# 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)