]> git.scottworley.com Git - paperdoorknob/blame - domfilter.py
Support strikethrough
[paperdoorknob] / domfilter.py
CommitLineData
8be20b9d
SW
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
8from typing import Any, Callable, List, Tuple
9
10from bs4.element import Tag
11
12
24e86634
SW
13def _changeTag(tag: Tag, new_name: str) -> Tag:
14 tag.name = new_name
15 return tag
16
17
8be20b9d
SW
18DOMFilters: List[Tuple[str, Callable[[Tag], Any]]] = [
19 ("NoEdit", lambda x: [eb.decompose() for eb in x.find_all("div", class_="post-edit-box")]),
20 ("NoFooter", lambda x: [foot.decompose() for foot in x.find_all("div", class_="post-footer")]),
24e86634
SW
21 ("Strike", lambda x: [_changeTag(span, 's')
22 for span in x.find_all("span", style="text-decoration: line-through;")]),
8be20b9d
SW
23]
24
25
26class DOMFilterError(Exception):
27 pass
28
29
30def ApplyDOMFilters(filter_list: str, tag: Tag) -> None:
31 for filter_name in filter_list.split(','):
32 filters = [f for (name, f) in DOMFilters if name == filter_name]
33 if len(filters) == 0:
34 raise DOMFilterError(f"Unknown DOM filter: {filter_name}")
35 if len(filters) > 1:
36 raise DOMFilterError(
37 f"Multiple DOM filters with the same name!?: {filter_name}")
38 filters[0](tag)