From 92d85d31bf058ea6a94f7e0ac9caab82c68f9ef0 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Sun, 23 Jul 2023 23:54:03 +0200 Subject: [PATCH] fix: do not require bleach module by default Signed-off-by: Christopher Arndt --- README.md | 7 +++++-- matrixchat-notify.py | 30 +++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 85d4182..6d65a47 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,9 @@ steps: 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. + 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`) 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). -[`DEFAULT_ALLOWED_ATTRS`]: ./matrixchat-notify.py#L29 -[`DEFAULT_ALLOWED_TAGS`]: ./matrixchat-notify.py#L35 +[`DEFAULT_ALLOWED_ATTRS`]: ./matrixchat-notify.py#L27 +[`DEFAULT_ALLOWED_TAGS`]: ./matrixchat-notify.py#L34 [allowed attributes]: https://bleach.readthedocs.io/en/latest/clean.html#allowed-attributes-attributes [drone.io]: https://drone.io/ [list of extensions]: https://python-markdown.github.io/extensions/ diff --git a/matrixchat-notify.py b/matrixchat-notify.py index 7e24740..65b6e4d 100755 --- a/matrixchat-notify.py +++ b/matrixchat-notify.py @@ -20,36 +20,47 @@ from distutils.util import strtobool from os.path import exists from string import Template -import bleach from nio import AsyncClient, LoginResponse PROG = "matrixchat-notify" CONFIG_FILENAME = f"{PROG}-config.json" -DEFAULT_ALLOWED_ATTRS = bleach.ALLOWED_ATTRIBUTES.copy() -DEFAULT_ALLOWED_ATTRS.update( - { - "*": ["class"], - "img": ["alt", "src"], - } -) -DEFAULT_ALLOWED_TAGS = bleach.ALLOWED_TAGS | { +DEFAULT_ALLOWED_ATTRS = { + "*": ["class"], + "a": ["href", "title"], + "abbr": ["title"], + "acronym": ["title"], + "img": ["alt", "src"], +} +DEFAULT_ALLOWED_TAGS = { + "a", + "abbr", + "acronym", + "b", + "blockquote", + "code", "dd", "div", "dl", "dt", + "em", "h1", "h2", "h3", "h4", "h5", "h6", + "i", + "li", + "ol", "p", "span", + "strong", "table", "td", "th", "thead", "tr", + "ul", } DEFAULT_HOMESERVER = "https://matrix.org" DEFAULT_MARKDOWN_EXTENSIONS = "admonition, extra, sane_lists, smarty" @@ -171,6 +182,7 @@ def render_message(config): def render_markdown(message, config): + import bleach import markdown allowed_attrs = config.get("allowed_attrs", DEFAULT_ALLOWED_ATTRS)