fix: do not require bleach module by default

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2023-07-23 23:54:03 +02:00
parent 1e374c61e6
commit 92d85d31bf
2 changed files with 26 additions and 11 deletions

View File

@ -70,6 +70,9 @@ steps:
substtution is considered to be in Markdown format and will be rendered to substtution is considered to be in Markdown format and will be rendered to
HTML and sent as a formatted message with `org.matrix.custom.html` format. HTML and sent as a formatted message with `org.matrix.custom.html` format.
Using this feature requires the `markdown` and `bleach` Python modules to
be available (the plugin's docker image has them installed).
* `markdown_extensions` *(default:* `admonition, extra, sane_lists, smarty`) * `markdown_extensions` *(default:* `admonition, extra, sane_lists, smarty`)
Comma-separated list of enabled Markdown extensions. See this Comma-separated list of enabled Markdown extensions. See this
@ -107,8 +110,8 @@ steps:
ID of user on homeserver to send message as (ID, not username). ID of user on homeserver to send message as (ID, not username).
[`DEFAULT_ALLOWED_ATTRS`]: ./matrixchat-notify.py#L29 [`DEFAULT_ALLOWED_ATTRS`]: ./matrixchat-notify.py#L27
[`DEFAULT_ALLOWED_TAGS`]: ./matrixchat-notify.py#L35 [`DEFAULT_ALLOWED_TAGS`]: ./matrixchat-notify.py#L34
[allowed attributes]: https://bleach.readthedocs.io/en/latest/clean.html#allowed-attributes-attributes [allowed attributes]: https://bleach.readthedocs.io/en/latest/clean.html#allowed-attributes-attributes
[drone.io]: https://drone.io/ [drone.io]: https://drone.io/
[list of extensions]: https://python-markdown.github.io/extensions/ [list of extensions]: https://python-markdown.github.io/extensions/

View File

@ -20,36 +20,47 @@ from distutils.util import strtobool
from os.path import exists from os.path import exists
from string import Template from string import Template
import bleach
from nio import AsyncClient, LoginResponse from nio import AsyncClient, LoginResponse
PROG = "matrixchat-notify" PROG = "matrixchat-notify"
CONFIG_FILENAME = f"{PROG}-config.json" CONFIG_FILENAME = f"{PROG}-config.json"
DEFAULT_ALLOWED_ATTRS = bleach.ALLOWED_ATTRIBUTES.copy() DEFAULT_ALLOWED_ATTRS = {
DEFAULT_ALLOWED_ATTRS.update(
{
"*": ["class"], "*": ["class"],
"a": ["href", "title"],
"abbr": ["title"],
"acronym": ["title"],
"img": ["alt", "src"], "img": ["alt", "src"],
} }
) DEFAULT_ALLOWED_TAGS = {
DEFAULT_ALLOWED_TAGS = bleach.ALLOWED_TAGS | { "a",
"abbr",
"acronym",
"b",
"blockquote",
"code",
"dd", "dd",
"div", "div",
"dl", "dl",
"dt", "dt",
"em",
"h1", "h1",
"h2", "h2",
"h3", "h3",
"h4", "h4",
"h5", "h5",
"h6", "h6",
"i",
"li",
"ol",
"p", "p",
"span", "span",
"strong",
"table", "table",
"td", "td",
"th", "th",
"thead", "thead",
"tr", "tr",
"ul",
} }
DEFAULT_HOMESERVER = "https://matrix.org" DEFAULT_HOMESERVER = "https://matrix.org"
DEFAULT_MARKDOWN_EXTENSIONS = "admonition, extra, sane_lists, smarty" DEFAULT_MARKDOWN_EXTENSIONS = "admonition, extra, sane_lists, smarty"
@ -171,6 +182,7 @@ def render_message(config):
def render_markdown(message, config): def render_markdown(message, config):
import bleach
import markdown import markdown
allowed_attrs = config.get("allowed_attrs", DEFAULT_ALLOWED_ATTRS) allowed_attrs = config.get("allowed_attrs", DEFAULT_ALLOWED_ATTRS)