]>
git.scottworley.com Git - paperdoorknob/blob - domfilter.py
e1095f0f856842d3d8fa9819dd36d57c4d1fda92
1 # paperdoorknob: Print glowfic
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.
8 from typing
import Any
, Callable
, List
, Tuple
10 from bs4
.element
import Tag
13 def _changeTag(tag
: Tag
, new_name
: str) -> Tag
:
18 DOMFilters
: 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")]),
21 ("Strike", lambda x
: [_changeTag(span
, 's')
22 for span
in x
.find_all("span", style
="text-decoration: line-through;")]),
26 class DOMFilterError(Exception):
30 def 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
]
34 raise DOMFilterError(f
"Unknown DOM filter: {filter_name}")
37 f
"Multiple DOM filters with the same name!?: {filter_name}")